Repository navigation
fix(native): prevent malloc deadlocks in signal handling - #2336
Merged
Merged
Conversation
A crash while the crashed thread holds the macOS malloc lock aborted
the process inside the native crash handler, before the daemon could
send the report:
_os_unfair_lock_recursive_abort
_xzm_xzone_malloc_freelist_outlined
dyld::ThreadLocalVariables::instantiateVariable
_tlv_get_addr
sentry__scope_save_active_trace
The scope skips its thread-locals and locks on the thread that entered
the signal handler state. The inproc, breakpad and crashpad handlers
enter it, but the native handler did not.
Ref: getsentry/sentry-cocoa#8271
The first thread-local access on a thread allocates on macOS. Clang
hoisted the inlined `g_scope_access` access above the
`sentry__block_for_signal_handler()` check, so crash handling still
called `_tlv_get_addr` with the malloc lock held:
_os_unfair_lock_recursive_abort
_tlv_get_addr
sentry__scope_save_active_trace
Read the thread-locals through non-inlined accessors that cannot be
hoisted above the check.
Ref: getsentry/sentry-cocoa#8271
libc `vsnprintf` allocates while formatting doubles, which aborted
crash handling with the malloc lock held:
_os_unfair_lock_recursive_abort
__Balloc_D2A
__dtoa
sentry__snprintf_c
sentry__jsonwriter_write_double
Use the stack-only `stbsp_vsnprintf` that Linux already uses.
Ref: getsentry/sentry-cocoa#8271
`sentry_test_malloc_lock` builds the integration fixture with a `crash-malloc-locked` scenario, which locks the allocator on a new thread and crashes there. It locks the default malloc zone on macOS and the process heap on Windows, and replaces `malloc` with a blocking version on glibc. Other configurations exit with 77 to skip. Ref: getsentry/sentry-cocoa#8271
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## v1 #2336 +/- ##
==========================================
- Coverage 75.87% 75.87% -0.01%
==========================================
Files 105 105
Lines 31254 31264 +10
Branches 5830 5830
==========================================
+ Hits 23715 23721 +6
- Misses 6089 6097 +8
+ Partials 1450 1446 -4 🚀 New features to boost your workflow:
|
mujacica
approved these changes
Nov 24, 2026
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 57a41b4. Configure here.
`HeapLock` is recursive for its owner thread, so the crashed thread could still allocate in the exception filter, and the test could not catch allocations in the crash handler on Windows. Drop the Windows lock from the fixture and skip the test there. Ref: #2336 (comment)
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.

Fix potential deadlocks when crashing with the system allocator lock held, e.g. on heap corruption inside
mallocorfree, by removing allocations from the crash path:sentry__enter_signal_handler()in the native signal handler, so the scope skips its locks and thread-locals there.sentry__block_for_signal_handler(), and the first access on a thread allocates on macOS (_tlv_get_addr).stbsp_vsnprintfon Apple platforms. libcvsnprintfallocates (__Balloc_D2A).Add
test_native_crash_malloc_locked, which crashes on a new thread with the allocator locked:force_lockon the default malloc zone on macOS,HeapLockon Windows, and a blockingmallocinterposer on glibc. It skips on Windows, where the crashed thread can re-enter the heap lock.See also: