fix: use shared_ptr in ThreadedAsyncOperation to prevent SIGBUS on macOS#21625
Open
fix: use shared_ptr in ThreadedAsyncOperation to prevent SIGBUS on macOS#21625
Conversation
310a821 to
1f5c0c4
Compare
This was referenced Mar 16, 2026
TSFN BlockingCall (napi_tsfn_blocking) only blocks on queue insertion, NOT on callback completion. The callback runs asynchronously on the JS thread, so `delete this` on the worker thread raced with the callback reading member fields — confirmed via ASAN. Fix: manage ThreadedAsyncOperation via shared_ptr (enable_shared_from_this). Both the worker thread lambda and the TSFN callback capture a shared_ptr, so the object lives until both are done. Verified clean under ASAN with 1000 concurrent operations.
1f5c0c4 to
8e2d14f
Compare
ludamad
added a commit
that referenced
this pull request
Mar 16, 2026
Reverts #21138 on v4. ThreadedAsyncOperation has a use-after-free that causes SIGBUS on macOS and silent memory corruption on Linux. Restoring AsyncOperation (libuv pool) with the original deadlock-prevention semaphore (UV_THREADPOOL_SIZE / 2) until a proper fix lands on next (#21625). [Post mortem](https://gist.github.com/ludamad/443afe321853389a08693c4ff73676f7)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes use-after-free in
ThreadedAsyncOperation(#21138) that causes SIGBUS on macOS and silent memory corruption on Linux. v4 is handled by reverting: #21630.Root cause: TSFN
BlockingCall(napi_tsfn_blocking) only blocks on queue insertion, NOT on callback completion. The callback runs asynchronously on the JS main thread, sodelete thison the worker thread raced with the callback reading member fields. macOS's magazine malloc aggressively unmaps freed pages, turning this into a consistent SIGBUS. Linux glibc keeps pages mapped, so the race is silent.Fix: manage
ThreadedAsyncOperationviashared_ptr(enable_shared_from_this). Both the worker thread lambda and the TSFN callback capture ashared_ptr, so the object lives until both are done. Verified clean under ASAN with 1000+ concurrent operations (heap-use-after-free confirmed on buggy code, clean on fix).Full post mortem