src: fix perfetto session reader teardown race - #65611
Open
MarshallOfSound wants to merge 1 commit into
Open
Conversation
MarshallOfSound
force-pushed
the
src-perfetto-reader-teardown
branch
from
August 28, 2026 07:46
f55e86d to
5848e98
Compare
PerfettoSessionReader::Deleter issues a final ReadTrace() and then Stop()s the session. Perfetto delivers the read data and the stop notification as independent tasks on its own thread, so the stop could win, close the uv handles and delete the reader while a ReadTraceCallback bound to the raw pointer was still queued. That callback then locked a destroyed mutex and signalled a closed uv_async_t. Only tear the reader down once the owner has released it, the session has stopped and no read is in flight, and have both Perfetto-thread callbacks update their flag and signal under chunks_mutex_ so the loop thread cannot free the reader in between. Refs: nodejs#64565
MarshallOfSound
force-pushed
the
src-perfetto-reader-teardown
branch
from
August 28, 2026 07:47
5848e98 to
aeccc1d
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #65611 +/- ##
==========================================
- Coverage 90.07% 90.06% -0.01%
==========================================
Files 751 751
Lines 254916 254916
Branches 48133 48121 -12
==========================================
- Hits 229605 229586 -19
- Misses 16496 16497 +1
- Partials 8815 8833 +18 🚀 New features to boost your workflow:
|
legendecas
reviewed
Aug 28, 2026
| std::list<std::vector<char>> chunks_to_write; | ||
| { | ||
| Mutex::ScopedLock lock(reader->chunks_mutex_); | ||
| std::swap(chunks_to_write, reader->pending_chunks_); |
Member
There was a problem hiding this comment.
I think a simple fix could be:
// Shutdown requested and no read outstanding.
bool should_tear_down = false;
{
Mutex::ScopedLock lock(reader->chunks_mutex_);
std::swap(chunks_to_write, reader->pending_chunks_);
should_tear_down =
reader->stop_requested_ && !reader->read_in_progress_;
}and later check it
if (!should_tear_down || reader->handles_pending_close_ != 0) return;
... close handles.With the Mutex::ScopedLock changes in both ReadTraceCallback and SessionStopCallback.
This does not need any additional mutexes or flags.
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.
PerfettoSessionReader::Deleterdoes a finalReadTrace()thenStop(). Perfetto runs the read callback and the stop callback as separate tasks on its own thread, so the stop can win: the loop thread closes the handles and deletes the reader while aReadTraceCallbackbound to the rawthisis still queued, which then locks a destroyed mutex and signals a closeduv_async_t.This defers teardown until the owner has released the reader, the session has stopped, and no read is in flight, and makes both Perfetto-thread callbacks flip their flag and signal under
chunks_mutex_so the loop thread can't free the reader in between.Found with ASan (
trace_events.createTracing().enable()+ process exit, intermittentlyabort()inuv_mutex_lockfromReadTraceCallback).Refs: #64565