Repository navigation
Keep a DigestStream alive while its sink runs - #8533
Conversation
There was a problem hiding this comment.
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
1106b7d to
d513a13
Compare
There was a problem hiding this comment.
✅ 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)
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 whileDigestStream::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::WeakRefto the stream and promotes it withtryAddRef()at the start of every call, keeping the strongRefuntil the call returns. A GC inside the call can no longer collect the stream. Holding theRefonly 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
gcDuringCloseOfDroppedStreamcase 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.