Unify asynchronous PowerShell cmdlet lifecycle#4
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6796ccfc23
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 96829d714f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2676b60b07
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 60de5e59ad
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 458f5aabbb
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 57efa1ce21
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 61ca39947d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5dfdee3761
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d4cad915c9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5f395ff865
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 392dc49316
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b97abfb757
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 67018d894e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2ff9d20c7e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8b6963604c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6e9951cec2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e6167bf45d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: debcc2ee12
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| while (queuedAtEntry-- > 0 && outPipe.TryTake(out var item)) | ||
| PumpItem(item); |
There was a problem hiding this comment.
Drain records added while pumping completed hooks
When an already-completed hook has a queued OutputEnumerate item whose lazy enumeration invokes a bridged Write* method, the pump lease allows that nested record to be appended, but this loop only consumes the count captured before enumeration began. The subsequent cleanup drains the newly appended record without executing it, silently losing valid output; drain until the closed lifecycle queue is empty while retaining the reentrancy guard.
Useful? React with 👍 / 👎.
| while (outPipe.TryTake(out var pumpBoundItem)) | ||
| PumpItem(pumpBoundItem); |
There was a problem hiding this comment.
Observe cancellation while draining post-completion records
If StopProcessing cancels immediately after the pump takes HookCompleted, this unconditional drain continues executing records queued behind the sentinel, such as writes produced while a lazy item was being enumerated. Fresh evidence in this version is that the completed-task branch now calls ThrowIfStopped(), whereas this remaining incomplete-task drain never checks cancellation, so records can still be emitted after a stop request.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6684b61048
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } | ||
| finally | ||
| { | ||
| Volatile.Write(ref _asyncLifecycleCompleted, 1); |
There was a problem hiding this comment.
Preserve pipeline access after base EndProcessing
When a derived cmdlet retains a synchronous EndProcessing override, calls base.EndProcessing(), and then uses an inherited WriteObject/WriteWarning bridge, this flag is set before control returns to the derived method. RunBlockInAsync has already cleared _pipelineThreadId and _currentOutPipe, so this flag disables IsConstructionThreadOutsideAsyncHook and the write reaches the null-pipe return path, silently losing the post-base record. Keep direct pipeline access valid until the complete derived override has returned rather than marking the lifecycle complete inside the base call.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c80fb662d1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| var pumpLease = _pipelinePumpLease.Value; | ||
| var isPumpBound = | ||
| pumpLease is { IsActive: true } && | ||
| item.HookGeneration == pumpLease.Generation; | ||
| lock (_hookAdmissionLock) | ||
| { | ||
| if (item.HookGeneration != 0 && | ||
| item.HookGeneration != Volatile.Read(ref _acceptingHookWritesGeneration) && | ||
| !isPumpBound) |
There was a problem hiding this comment.
Carry pump ownership into context-free captured callbacks
When a hook queues an OutputEnumerate item and completes before its lazy iterator invokes CapturedPipelineStreams from a thread that does not flow ExecutionContext, admission has already closed and this check rejects the record: the captured stream retains the hook generation, but isPumpBound depends exclusively on the flow-local _pipelinePumpLease. The fresh evidence after the earlier 6e9951c fix is that the current implementation removed the cross-thread pump marker without adding pump ownership to captured writers, so the API documented specifically for non-flowing callbacks once again silently loses valid output produced while its originating item is actively being pumped.
Useful? React with 👍 / 👎.
| CompleteAddingIfNeeded(outPipe); | ||
| if (blockTask.IsCompleted) | ||
| DisposePipeOnce(); |
There was a problem hiding this comment.
Clear the published pipe when failure races hook completion
When a pipeline item throws just after blockTask completes, its completion continuation can observe deferPipeDisposal == 0 and finish without calling ClearPipes; this catch then sets the flag but, because the task is already complete, only disposes the collection here. No remaining path clears _currentOutPipe or _pumpQueuedItems, so a derived synchronous lifecycle override that catches the failure and attempts a recovery write after base.ProcessRecord() sees the stale disposed pipe and silently drops the record. Clear the published fields as well when this branch takes ownership of immediate disposal.
Useful? React with 👍 / 👎.
| private readonly int _constructionThreadId = Environment.CurrentManagedThreadId; | ||
| private readonly object _hookAdmissionLock = new(); | ||
| private readonly object _lifecycleLock = new(); | ||
| private static readonly SynchronizationContext HookSynchronizationContext = new AsyncHookSynchronizationContext(); |
There was a problem hiding this comment.
Scope the hook synchronization context to each lifecycle
Because every cmdlet and lifecycle shares this single context instance, a default inline-capable task continuation captured by hook A can run inline when its awaited task is completed during hook B's synchronous prefix: .NET treats the captured context as already current by reference and bypasses Post, so A's post-await code executes reentrantly on B's PowerShell pipeline thread instead of the intended thread pool. This can block or reenter an unrelated runspace/lifecycle; create a distinct AsyncHookSynchronizationContext for each hook invocation so contexts from unrelated hooks never compare equal.
Useful? React with 👍 / 👎.
Summary - synchronize the repo-local
AsyncPSCmdletwith the canonical implementation in EvotecIT/PSPublishModule#630 - start asynchronous hooks on the PowerShell pipeline thread and marshal post-await output, prompts, and errors back to it - add safe terminating errors,ShouldContinue, credential prompts, cancellation, scheduler/context isolation, and extensible disposal - keep the source copy local to this repository; no runtime or package dependency is introduced - preserve repository-specific helpers and extension points where present ## Compatibility Cmdlet names and parameters are unchanged. Asynchronous hooks must remain async end to end and must not block withTask.Wait,Task.Result, orTask.WaitAll. ## Verification Release build across the demo's configured target frameworks; existing warning baseline is unchanged. ## Review follow-through The synchronized source now drops retained logger callbacks after lifecycle closure, includes complete interaction overloads and typed prompt replies, cancels on disposal, and separates pipeline-stop from operation-local cancellation.Final async lifecycle semantics
Synchronous PowerShell calls restore the host synchronization context before queued work is drained or the base API is invoked. Queue admission and hook completion are serialized, preserving records admitted before completion while rejecting stale producers afterward. Reentrant drains preserve FIFO order, including context-free captured callbacks made while a lazy queued item is actively being pumped.
Hook completion wakes the pump immediately. Direct downstream stops cancel work already started with the cmdlet token, cancellation-source disposal waits until cancellation callbacks return, and derived synchronous
EndProcessingoverrides can continue interacting with the pipeline after calling the base implementation. Pipeline-thread ownership uses volatile publication, captured callbacks drop cleanly after stop, and progress snapshots retain optional runtimeTotalvalues.Queued host interactions now atomically arbitrate cancellation against the pipeline claim: canceled unclaimed requests never enter the host, while a claimed prompt keeps its reply observed. Completed and failing synchronous hooks drain all causally reentrant records, and a synchronous pipeline stop cancels the shared token before rethrow.
PSPublishModule remains the canonical source owner. Consumer repositories retain namespace-adjusted local source copies and their repository-specific interfaces, helpers, formatting, and split-file conventions without adding a runtime package dependency.
Final validation
PowerShellBinaryDemo Release build passed at the exact head.