Skip to content

Protect test hook callback with mutex for TSan safety#61

Merged
bghgary merged 2 commits intomicrosoft:masterfrom
bghgary:fix/tsan-callback-race
Apr 16, 2026
Merged

Protect test hook callback with mutex for TSan safety#61
bghgary merged 2 commits intomicrosoft:masterfrom
bghgary:fix/tsan-callback-race

Conversation

@bghgary
Copy link
Copy Markdown
Contributor

@bghgary bghgary commented Apr 16, 2026

[Created by Copilot on behalf of @bghgary]

Protect the beforeWaitCallback test hook with a mutex for TSan safety.

Problem

The beforeWaitCallback global in blocking_concurrent_queue was unprotected — one thread could be reading/invoking it while another called set_before_wait_callback. TSan flags this as a data race.

Fix

  • Add callbackMutex to guard beforeWaitCallback access
  • set_before_wait_callback: acquires lock, moves new callback in
  • invoke_before_wait_callback (new detail helper): copies callback under the lock, invokes after releasing — avoids potential deadlocks if the callback interacts with other locks
  • Both internal_pop and internal_drain call the helper instead of inlining the pattern

bghgary and others added 2 commits April 16, 2026 09:03
The beforeWaitCallback global std::function was accessed from multiple
threads without synchronization. Add a dedicated mutex and use
copy-under-lock, invoke-after-unlock pattern to avoid holding the
lock during arbitrary callback execution.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 16, 2026 16:03
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a ThreadSanitizer (TSan) data race in the blocking_concurrent_queue test hook by synchronizing access to the global beforeWaitCallback used in ARCANA_TEST_HOOKS builds.

Changes:

  • Added a dedicated callbackMutex to guard reads/writes of beforeWaitCallback.
  • Introduced detail::invoke_before_wait_callback() to copy the callback under lock and invoke it after releasing the callback lock.
  • Updated internal_pop and internal_drain to call the new helper instead of invoking the global callback directly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@bghgary bghgary enabled auto-merge (squash) April 16, 2026 16:10
@bghgary bghgary merged commit 0b9f9b6 into microsoft:master Apr 16, 2026
19 checks passed
bghgary added a commit to bghgary/JsRuntimeHost that referenced this pull request Apr 16, 2026
- Add tsan_suppressions.txt to suppress JavaScriptCore/WTF/bmalloc
  internal data races on Ubuntu (not fixable by us)
- Wire suppressions into linux.yml for TSan jobs via TSAN_OPTIONS
- Point arcana.cpp at upstream (microsoft/arcana.cpp#61 merged)
- Update UrlLib fork SHA (post-rebase, pending BabylonJS/UrlLib#27)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

4 participants