Skip to content

Fix gcdump false success after collection errors - #6051

Merged
hoyosjs merged 7 commits into
dotnet:mainfrom
hoyosjs:juhoyosa/investigate-gcdump-crash
Sep 24, 2026
Merged

hoyosjs merged 7 commits into
dotnet:mainfrom
hoyosjs:juhoyosa/investigate-gcdump-crash

Conversation

@hoyosjs

@hoyosjs hoyosjs commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

Fixes #6048

Summary

  • Keep EventPipe completion state local to each live collection and file conversion.
  • Report success only after heap graph conversion completes.
  • Document that the live collection log writer must support concurrent writes.
  • Add regression coverage for a post-GCStop logging failure returning false.

Root cause

The failure in #6048 has two parts. Steeltoe calls EventPipeDotNetHeapDumper directly and supplies a plain StreamWriter, while the method logs concurrently from the EventPipe reader and session stop tasks. That caller-provided writer is not thread-safe, which explains the torn log line and StreamWriter.Flush exception. The shipped CLI uses synchronized Console.Out, so diagnostics continues to require callers of the direct API to provide a writer that supports concurrent writes.

After the logging exception, graph conversion is skipped. The previous implementation nevertheless returned the process-wide dumpComplete flag set when GCStop was observed. The caller therefore received true for an unconverted graph and failed with RootIndex not set. The process-wide flags also allowed separate or concurrent operations to affect each other.

This change gives each operation its own completion state and separates GC completion from operation success. true is returned only after ConvertHeapDataToGraph() completes.

Steeltoe must separately use a synchronized writer for direct in-process gcdump calls.

Reproduction

A real self-gcdump test injects a logging failure after GCStop and verifies that the operation returns false because graph conversion did not complete. The targeted test passes in Debug and Release.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 432587bf-ff0a-4257-ad8a-fc58a1a8c6a9
Serialize writes to caller-provided output writers, keep completion state local to each collection, and report success only after graph conversion finishes.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 432587bf-ff0a-4257-ad8a-fc58a1a8c6a9
Copilot AI lite review requested due to automatic review settings September 18, 2026 23:29
@hoyosjs
hoyosjs requested a review from a team as a code owner September 18, 2026 23:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The test project is missing an xUnit package reference and cannot compile the tests.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
What changed in this PR

Fixes an occasional gcdump crash caused by concurrent logging and stale completion state.

Changes:

  • Serializes TextWriter access.
  • Uses per-invocation completion state.
  • Adds self-gcdump regression tests.
File Summary
src/​Tools/​dotnet-gcdump/​DotNetHeapDump/​EventPipeDotNetHeapDumper.cs Fixes logging races and completion-state handling.
src/​tests/​dotnet-gcdump/​EventPipeDotNetHeapDumperTests.cs Adds regression coverage.
src/​tests/​dotnet-gcdump/​DotnetGCDump.UnitTests.csproj Adds the gcdump test project.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Require the concurrency test to observe both shutdown and rundown logging, and make shutdown fault injection independent of TextWriter overload forwarding.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 432587bf-ff0a-4257-ad8a-fc58a1a8c6a9
@bart-vmware

Copy link
Copy Markdown
Contributor

@steveisok, @lateralusX, @max-charlamb Any thoughts on the performance impact of the TextWriter thread-safely? See #6051 (comment).

Require callers to provide a writer that supports concurrent writes, retain resettable static collection state, and keep success gated on completed graph conversion.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 432587bf-ff0a-4257-ad8a-fc58a1a8c6a9
@hoyosjs hoyosjs changed the title Fix occasional crash during gcdump collection Fix gcdump false success after collection errors Sep 22, 2026
@hoyosjs

hoyosjs commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

I'm still trying to decide if this change makes sense to take with the synchronized textwriter. As pointed out in #5508 (comment) - gcdump is not an API, but rather a tool. Pushing the things to statics does help with testability and the error handling part too. If steeltoe decides to use that textwriter implementation, all will work. However - as we move towards tools being vertical RID-specific packages and then potentially AOTd, then this way of consuming it will for sure break.

Expose dotnet-gcdump internals to its unit tests and seed the retained static completion fields so both entry points deterministically verify their reset behavior.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 432587bf-ff0a-4257-ad8a-fc58a1a8c6a9
Ensure the false-success regression test reaches the simulated shutdown logging exception instead of passing on an unrelated EventPipe setup failure.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 432587bf-ff0a-4257-ad8a-fc58a1a8c6a9
@bart-vmware

Copy link
Copy Markdown
Contributor

I'm still trying to decide if this change makes sense to take with the synchronized textwriter. As pointed out in #5508 (comment) - gcdump is not an API, but rather a tool. Pushing the things to statics does help with testability and the error handling part too. If steeltoe decides to use that textwriter implementation, all will work. However - as we move towards tools being vertical RID-specific packages and then potentially AOTd, then this way of consuming it will for sure break.

Thanks for sharing your concerns. What Steeltoe offers is feature parity with the Java Spring Boot Heap Dump Actuator for .NET apps. Spring Boot Actuators are consumed by UIs such as the Apps Manager for Elastic Application Runtime, Spring Boot Admin and Ostara. Such tools provide unified operational management for polyglot enterprise apps. Steeltoe later added the GC-dump type, to make it easier to analyze dumps cross-platform (Windows/Linux/macOS).

I'm well aware that Steeltoe uses the code/package from this repository in a way it was never designed for. And any support for making our lives easier should not be interpreted as a precedent that libraries are supported. On the other hand, we are actively testing the code and report back issues, so I hope there's some value in that for the team?

Like I said, Steeltoe can take care of the TextWriter thread-safety requirement and check for NodeIndex.Invalid. Keeping the fields static is fine too, but please offer a public Reset method (or make the fields public), so we can revert the added reflection when updating to the next release. And a doc-comment on the public method that takes the TextWriter parameter, stating it needs to be thread-safe, would be nice too.

Long term, do you have any suggestions for how we could take .NET heap/gc/thread dumps via other APIs? We don't have the deep runtime knowledge you guys do, so any ideas are appreciated.

Keep EventPipe collection completion state isolated to each live or file conversion operation while retaining the post-conversion success check.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 432587bf-ff0a-4257-ad8a-fc58a1a8c6a9
@hoyosjs

hoyosjs commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

@bart-vmware I am not saying by any means that this is not something valuable - or that the changes I suggested are not gonna make it in. The static state and the conversion error fixes are now in this PR and I think this is what makes sense to take in. I see your PR adds the proposed TextWriter with a Lock - so this and that should be sufficient.

As for the APIs - we'd probably need to design whatever needs to happen and ship it as a library instead of a tool. We'd just prioritize that just as we prioritize other work. For now, I hope that this at least unblocks y'all's use cases.

@bart-vmware

Copy link
Copy Markdown
Contributor

@hoyosjs Thanks, LGTM. This means we can remove the check for NodeIndex.Invalid in Steeltoe once we've updated to this, right?

@hoyosjs
hoyosjs enabled auto-merge (squash) September 24, 2026 03:08
@hoyosjs
hoyosjs merged commit 2a16a62 into dotnet:main Sep 24, 2026
23 checks passed
@hoyosjs
hoyosjs deleted the juhoyosa/investigate-gcdump-crash branch September 25, 2026 03:21
hoyosjs added a commit that referenced this pull request Sep 30, 2026
Fixes #6048

## Summary

- Keep EventPipe completion state local to each live collection and file
conversion.
- Report success only after heap graph conversion completes.
- Document that the live collection log writer must support concurrent
writes.
- Add regression coverage for a post-GCStop logging failure returning
false.

## Root cause

The failure in #6048 has two parts. Steeltoe calls
`EventPipeDotNetHeapDumper` directly and supplies a plain
`StreamWriter`, while the method logs concurrently from the EventPipe
reader and session stop tasks. That caller-provided writer is not
thread-safe, which explains the torn log line and `StreamWriter.Flush`
exception. The shipped CLI uses synchronized `Console.Out`, so
diagnostics continues to require callers of the direct API to provide a
writer that supports concurrent writes.

After the logging exception, graph conversion is skipped. The previous
implementation nevertheless returned the process-wide `dumpComplete`
flag set when `GCStop` was observed. The caller therefore received
`true` for an unconverted graph and failed with `RootIndex not set`. The
process-wide flags also allowed separate or concurrent operations to
affect each other.

This change gives each operation its own completion state and separates
GC completion from operation success. `true` is returned only after
`ConvertHeapDataToGraph()` completes.

Steeltoe must separately use a synchronized writer for direct in-process
gcdump calls.

## Reproduction

A real self-gcdump test injects a logging failure after `GCStop` and
verifies that the operation returns `false` because graph conversion did
not complete. The targeted test passes in Debug and Release.

---------

Copilot-Session: 432587bf-ff0a-4257-ad8a-fc58a1a8c6a9

(cherry picked from commit 2a16a62)

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 1bf039ff-37e1-40b6-af8c-125192e61855
hoyosjs added a commit that referenced this pull request Sep 30, 2026
#6078)

Backport the Linux `/proc` process-discovery fix for #6040 and the
gcdump and dotnet-trace changes needed on `release/stable`:

- #5886: non-lossy EventPipe buffering for gcdump and `dotnet-trace
collect --buffering-mode Block`.
- #6051: report gcdump success only after heap conversion completes.
- #5970: remove the `collect-linux` preview banner.
- #6071: update `record-trace` and add `collect-linux --max-memory`.

Non-lossy buffering requires a .NET 11+ target runtime; gcdump warns and
falls back to lossy collection for older runtimes. Linux-specific
behavior has not been exercised locally on Windows.
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Occasional crash during gcdump

7 participants