Skip to content

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

Open
tustanivsky wants to merge 8 commits into
masterfrom
feat/crash-memory-reserve
Open

tustanivsky wants to merge 8 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 c6643ac

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 83.82353% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.70%. Comparing base (ed74e23) to head (c6643ac).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2274      +/-   ##
==========================================
+ Coverage   75.56%   75.70%   +0.13%     
==========================================
  Files         103      104       +1     
  Lines       28315    28378      +63     
  Branches     5203     5208       +5     
==========================================
+ Hits        21397    21483      +86     
+ Misses       5581     5542      -39     
- Partials     1337     1353      +16     
🚀 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

This branch has not been deployed

No deployments
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.

1 participant