Skip to content

Unify asynchronous PowerShell cmdlet lifecycle#4

Open
PrzemyslawKlys wants to merge 23 commits into
masterfrom
codex/canonical-async-pscmdlet
Open

Unify asynchronous PowerShell cmdlet lifecycle#4
PrzemyslawKlys wants to merge 23 commits into
masterfrom
codex/canonical-async-pscmdlet

Conversation

@PrzemyslawKlys

@PrzemyslawKlys PrzemyslawKlys commented Jul 25, 2026

Copy link
Copy Markdown
Member

Summary - synchronize the repo-local AsyncPSCmdlet with 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 with Task.Wait, Task.Result, or Task.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 EndProcessing overrides 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 runtime Total values.

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated
Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.Pipeline.cs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +229 to +230
while (queuedAtEntry-- > 0 && outPipe.TryTake(out var item))
PumpItem(item);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment on lines +417 to +418
while (outPipe.TryTake(out var pumpBoundItem))
PumpItem(pumpBoundItem);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread PowerShellBinaryDemo/AsyncPSCmdlet.cs Outdated
}
finally
{
Volatile.Write(ref _asyncLifecycleCompleted, 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +279 to +287
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment on lines +474 to +476
CompleteAddingIfNeeded(outPipe);
if (blockTask.IsCompleted)
DisposePipeOnce();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant