Skip to content

feat: add an opt-in memory reserve for the crash handler - #2274

Merged
tustanivsky merged 12 commits into
masterfrom
feat/crash-memory-reserve
Nov 3, 2026
Merged

tustanivsky merged 12 commits into
masterfrom
feat/crash-memory-reserve

Conversation

@tustanivsky

@tustanivsky tustanivsky commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

When an application crashes after it ran out of memory, the crash handler has nothing left to allocate either and fails to capture the crash.

sentry_options_set_crash_memory_reserve() and the SENTRY_CRASH_MEMORY_RESERVE build setting (in KiB) reserve memory in sentry_init(). Its pages are touched so they are backed by memory, and it is released once a backend has decided to handle a crash: before signaling the daemon in the native backend, in the inproc and crashpad handlers, and from a new breakpad filter callback that runs before the minidump is written. Releasing it after the backends' own checks keeps the reserve for signals that are handed back to a managed runtime.

The reserve is disabled by default. Console SDKs can enable it with the build setting; sentry-xbox reserves 8 MB.

The existing oom tests didn't cover this scenario because trigger_oom() doubles the allocation size and crashes on the first failure, which leaves about half of the memory free for the crash handler. The new oom-exhaust fixture command keeps allocating smaller blocks under a Job Object limit until almost no memory is left.

Other Considerations

  • The test covers breakpad only. The size the crash handler needs varies a lot: inproc walks the stack with dbghelp, which loads symbols on 32-bit, so a Debug 32-bit build needed more than 16 MB, while 4 MB was enough on 64-bit.
  • This approach is similar to Unreal Engine, which frees a backup memory pool before reporting an out-of-memory crash (32 MB on Windows, sized from crash-reporting tests on Fortnite). That is partly why these crashes didn't surface with the Unreal SDK.

Related Items

Add the SENTRY_CRASH_MEMORY_RESERVE build setting (in KiB, 0 by default)
and sentry_options_set_crash_memory_reserve() to override it at runtime.
Map and touch the reserve in sentry_init() before the backend starts, so
it is backed by memory when a crash occurs, and release it on close or
when init fails. Windows uses VirtualAlloc and Unix uses mmap. Other
platforms log a warning when a reserve is requested.
Release it once each backend has decided to handle the crash, so that
signals or exceptions that are handed back to a runtime keep it. Breakpad
gets a filter callback to release it before the minidump is written,
which covers unhandled exceptions, invalid parameters, pure calls and
SIGABRT on Windows. The native backend releases it before signaling the
daemon, which may share the application's memory budget.
Add an oom-exhaust fixture command that keeps allocating smaller blocks
under a Job Object limit until almost no memory is left, and check that
inproc and breakpad still capture the crash with a memory reserve. Without
the reserve, breakpad writes an empty minidump.
@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against cd2996e

inproc walks the stack with dbghelp, which loads symbols on 32-bit and
needs more memory than the test reserves in Debug builds.
@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.13514% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.70%. Comparing base (ed74e23) to head (cd2996e).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2274      +/-   ##
==========================================
+ Coverage   75.56%   75.70%   +0.13%     
==========================================
  Files         103      104       +1     
  Lines       28315    28384      +69     
  Branches     5203     5209       +6     
==========================================
+ Hits        21397    21487      +90     
+ Misses       5581     5547      -34     
- Partials     1337     1350      +13     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tustanivsky
tustanivsky marked this pull request as ready for review October 30, 2026 12:44
Comment thread external/libunwindstack-ndk Resolved
Comment thread src/sentry_crash_reserve.c Resolved

@jpnurmi jpnurmi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A small follow-up to the earlier suggestion: the size in the mapping fixed the race but needs a verification check. An overrun from an adjacent mapping could corrupt it and make munmap unmap unrelated memory.

Comment thread src/sentry_crash_reserve.c Resolved
Comment thread src/sentry_crash_reserve.c Resolved
@tustanivsky
tustanivsky merged commit 30708ff into master Nov 3, 2026
70 checks passed
@tustanivsky
tustanivsky deleted the feat/crash-memory-reserve branch November 3, 2026 12:39
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.

2 participants