Skip to content

Keep a DigestStream alive while its sink runs - #8533

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-17-digest-stream-gc
Nov 17, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-17-digest-stream-gc

Conversation

@littledivy

@littledivy littledivy commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

DigestStream's sink lambdas held the stream by raw reference, and the controller refers back to it only through a kj::Weak. Once a writer queued a close and released its lock, nothing kept the stream alive. Resolving the digest in the close sink allocates on the cppgc heap, and under allocation pressure that allocation starts a major GC. The GC then destroyed the stream while DigestStream::close() was still running, and the close read the freed resolver, so workerd crashed with a segfault (a heap-use-after-free under ASan). Fixes #8491.

In this patch each sink captures a jsg::WeakRef to the stream and promotes it with tryAddRef() at the start of every call, keeping the strong Ref until the call returns. A GC inside the call can no longer collect the stream. Holding the Ref only for the call adds no ownership edge, so dropping a stream without closing it still frees it. If the stream is already gone, the sink returns a resolved promise, since nothing can observe the operation. Streams that are still referenced behave as before.

The new gcDuringCloseOfDroppedStream case in the digest suite queues closes on dropped streams under allocation pressure. Without the fix it crashed the C++ configuration in 6 to 8 of 10 runs.

@littledivy
littledivy requested review from a team as code owners November 17, 2026 09:45

@clanky-the-clanker clanky-the-clanker 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.

Superseded by the review of d513a131.

This review, of 1106b7dd

✅ Approved — A small, targeted fix for the use-after-free in DigestStream's sinks, with a regression test; no issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change removes a heap-use-after-free and adds no new attack surface.


📖 AGENTS.md: ✅ LGTM

The test-suite table entry correctly describes the new case.


📋 Code Quality: ✅ LGTM

The WeakRef-plus-tryAddRef() pattern used in the write, abort and close sinks is correct and matches the jsg API.


⚡ Performance: ✅ LGTM

Each sink call now does one extra weak-ref promotion, which costs very little.


📝 Documentation: ✅ LGTM

The new code comment describes the current design and does not narrate the change.


Reviewed commit: 1106b7dd

DigestStream's sink lambdas held the stream by raw reference, and the controller refers back to it only through a `kj::Weak`. Once a writer queued a close and released its lock, nothing kept the stream alive. Resolving the digest in the close sink allocates on the cppgc heap, and under allocation pressure that allocation starts a major GC. The GC then destroyed the stream while `DigestStream::close()` was still running, and the close read the freed resolver, so workerd crashed with a segfault (a heap-use-after-free under ASan). Fixes #8491.

In this patch each sink captures a `jsg::WeakRef` to the stream and promotes it with `tryAddRef()` at the start of every call, keeping the strong `Ref` until the call returns. A GC inside the call can no longer collect the stream. Holding the `Ref` only for the call adds no ownership edge, so dropping a stream without closing it still frees it. If the stream is already gone, the sink returns a resolved promise, since nothing can observe the operation. Streams that are still referenced behave as before.

The new `gcDuringCloseOfDroppedStream` case in the digest suite queues closes on dropped streams under allocation pressure. Without the fix it crashed the C++ configuration in 6 to 8 of 10 runs.
@codecov-commenter

codecov-commenter commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 23.07692% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.38%. Comparing base (5713cd2) to head (d513a13).
⚠️ Report is 20 commits behind head on main.

Files with missing lines Patch % Lines
src/workerd/api/crypto/crypto.c++ 23.07% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8533      +/-   ##
==========================================
- Coverage   66.44%   66.38%   -0.07%     
==========================================
  Files         794      795       +1     
  Lines      202976   203263     +287     
  Branches    31536    31545       +9     
==========================================
+ Hits       134868   134932      +64     
- Misses      49646    49867     +221     
- Partials    18462    18464       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@littledivy
littledivy force-pushed the littledivy/2026-11-17-digest-stream-gc branch from 1106b7d to d513a13 Compare November 17, 2026 10:15

@clanky-the-clanker clanky-the-clanker 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.

✅ Approved — A small, targeted fix for the use-after-free in DigestStream's sinks, with a regression test; no issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change removes a heap-use-after-free and adds no new attack surface.


📖 AGENTS.md: ✅ LGTM

The test-suite table entry correctly describes the new case.


📋 Code Quality: ✅ LGTM

The WeakRef-plus-tryAddRef() pattern used in the write, abort and close sinks is correct and matches the jsg API.


⚡ Performance: ✅ LGTM

Each sink call now does one extra weak-ref promotion, which costs very little.


📝 Documentation: ✅ LGTM

The new code comment describes the current design and does not narrate the change.


Reviewed commit: d513a131 — same changes as 1106b7dd, so this is that commit's review (no new review was run; line numbers are as of 1106b7dd)

@littledivy
littledivy merged commit b49a4bd into main Nov 17, 2026
35 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-17-digest-stream-gc branch November 17, 2026 17:10
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.

🐛 Bug Report — Runtime APIs: crypto.DigestStream: a GC during a queued close crashes workerd with SIGSEGV

3 participants