Skip to content

[release/17.0] Keep captured output when ProcessCaptureRunner's post-exit drain bound expires - #22047

Closed
Mitch Denny (mitchdenny) wants to merge 1 commit into
mitchdenny-backport-17-0-48-pr21571from
mitchdenny-backport-17-0-49-pr21556
Closed

Mitch Denny (mitchdenny) wants to merge 1 commit into
mitchdenny-backport-17-0-48-pr21571from
mitchdenny-backport-17-0-49-pr21556

Conversation

@mitchdenny

Copy link
Copy Markdown
Member

Description

Backport of #21556 to release/17.0 (cherry-picked from 1664086 with no modifications).

Layer 49 of 55 in the release/17.0 backport stack; stacked on #22046.

Backport recommendation: Yes — CLI could lose captured process output under load.

Validation: Stable patch IDs match (1bc7687e0725ac8735235468f57cdf45c479e9b3). Builds/tests not run for this unchanged cherry-pick.

Backport of #21556

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No

…d expires (#21556)

## Description

`PeerInstallProbeTests.ProbeAsync_PeerEmitsArrayWithNonObjectFirstElement_FallsBackToVersion("[\"string\"]")`
failed once on Windows with `Peer produced no usable output (and
--version fallback)`. That run was [35003412018 / job
104497909578](https://github.com/microsoft/aspire/actions/runs/35003412018/job/104497909578).
The test took **4.3s**. Passing runs take a median of 0.154s and a max
of 0.51s (n=60, from TRX of 15 recent main Cli-windows runs, 300/300
PeerInstallProbe passes).

### Root cause

`ProcessCaptureRunner.RunAsync` handles a process that exits normally
like this:
- It waits up to a fixed 2s (`s_postExitCaptureWaitBound`) for the
stdout/stderr capture to reach EOF.
- If that bound expires, `SwallowCaptureAsync` returns
**`createEmptyCapture()`**. That throws away everything the reader had
already buffered. Only after that does it cancel the reads.

So a peer that printed `9.0.0` and exited 0 could come back as exit 0
with empty stdout. `ExtractVersionLine` then returns null and the probe
reports "no usable output". EOF arrives late when a descendant still
holds the inherited pipe handle, or when reader continuations are
delayed under CI load. The extra 2s matches the 4.3s failure duration.

The probe's Debug diagnostics, such as "Timed out waiting 2s for process
output capture", were missing from the CI log. The test's
`LoggerFactory` used the default Information minimum level, so they were
filtered out before reaching the xunit provider.

### Fix

When the post-exit drain bound expires, `RunAsync` now cancels the reads
and *then* collects the capture, waiting at most the 250ms post-kill
bound. It no longer abandons the capture. The result carries the real
exit code, the capture, and a new
`ProcessCaptureFailureKind.OutputDrainTimedOut`:
- `PeerInstallProbe` treats `OutputDrainTimedOut` as success.
`ReadCappedAsync` returns partial output on cancellation, so the version
the peer wrote before exiting is kept. The existing parsers still reject
unusable output.
- `GitCommandRunner` and `ConfigurationHelper` reject any non-null
`FailureKind`, so an incomplete drain still fails as before
(`RepositoryUnavailable` / settings write refused). This keeps
`ConfigSourceServiceGitTests.RunProcess_DescendantHoldingOutputCannotBlockShutdown`
passing. The first revision, without the flag, broke that test in CI,
which is why the flag exists.
- The happy path doesn't change. The final `timeoutCts.Cancel()` still
guarantees no capture task outlives the call.

I also changed the test logger factory to
`SetMinimumLevel(LogLevel.Trace)` so future failures include the probe's
Debug logs.

### Regression test and evidence

The new test is
`ProbeAsync_VersionFallbackDescendantHoldsStdoutOpen_KeepsCapturedVersion`
(POSIX only). The fake peer:
- fails `doctor`;
- on `--version`, prints `9.0.0`, leaves a background `sleep 60 &` that
inherits stdout, and exits 0.

This makes the late-EOF condition deterministic. The test runs real
processes through the real `ProcessCaptureRunner`; nothing is mocked.

| | Result |
|---|---|
| Before fix (Linux) | **Fails** after 2.1s. The log shows `Timed out
waiting 2s for process output capture` and the probe returns no version.
|
| After fix (Linux) | **10/10 pass**, about 2.08s each (the expected
drain bound, then cancellation). |
| `PeerInstallProbeTests`, `ConfigSourceService*`, `GitCommandRunner*`,
`ConfigurationHelper*` and `ProcessCaptureRunner*` classes, after fix |
340 total: 339 passed, 1 skipped (Windows-only), 0 failed. These run
real `git` and real descendant processes. |
| New test plus `RunProcess_DescendantHoldingOutputCannotBlockShutdown`,
repeated | 5 × 5 = 25/25 passed |

**Caveat:** the Windows failure left no probe diagnostics, so I can't
prove the late EOF from that one log. One alternative remains possible:
the fake peer's `echo %* | findstr` failing transiently, which exits 127
and produces the same message. The 2s-shaped extra duration points to
the drain path, and the fix closes that path either way.

Fixes #20147

## Checklist
- Is this feature complete?
  - [x] Yes. Ready to ship.
  - [ ] No. Follow-up changes expected.
- Are you including unit tests for the changes and scenario tests if
relevant?
  - [x] Yes
  - [ ] No
- Did you add public API?
  - [ ] Yes
  - [x] No
- Does the change make any security assumptions or guarantees?
  - [ ] Yes
  - [x] No
- Does the change require an update in our Aspire docs?
  - [ ] Yes
  - [x] No

**🤖 Posted by AspireAgent (AI Agent)**

---------

Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
(cherry picked from commit 1664086)
@github-actions

This comment has been minimized.

@mitchdenny

Copy link
Copy Markdown
Member Author

Approved for inclusion in release/17.0. Do not merge yet.

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 22047

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 22047"

@github-actions

This comment has been minimized.

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.

2 participants