Skip to content

fix(logging): make logging state constant-initialized - #8

Merged
jpnurmi merged 3 commits into
getsentryfrom
fix/logging-constinit-early-static-init
Aug 3, 2026
Merged

jpnurmi merged 3 commits into
getsentryfrom
fix/logging-constinit-early-static-init

Conversation

@mujacica

@mujacica mujacica commented Jul 30, 2026 •

Copy link
Copy Markdown

Problem

On Windows, sentry_init() crashes with an access violation when it runs from a C++ static initializer that executes before the compiler-generated dynamic initializers in .CRT$XCU.

Report: getsentry/sentry-native#1930.

This regressed after the file-logging change (#6) added the lock; it worked before that.

Fix

Remove the dynamic initializer from g_log_file so its state is valid from image load, before any initializer runs

mujacica and others added 2 commits July 30, 2026 15:21
InitLogging() acquires a base::Lock stored in the namespace-scope global
g_log_file, whose constructor sets up the underlying CRITICAL_SECTION on
Windows. If sentry_init() (and therefore InitLogging()) runs from a static
initializer that executes before this translation unit's dynamic initializers
-- e.g. an initializer emitted into .CRT$XCL via #pragma init_seg(lib) or into
.CRT$XCA* -- the CRITICAL_SECTION is still zero-filled and EnterCriticalSection
faults with an access violation before main() is reached.

Remove the dynamic initializer from g_log_file so it is safe to touch at any
point after CRT startup:

- Replace base::Lock with a small LogLock that is constant-initializable:
  SRWLOCK (SRWLOCK_INIT) on Windows and std::mutex (constexpr constructor)
  elsewhere. A zero/constant-initialized SRWLOCK is a valid, unlocked lock, so
  no constructor needs to have run.
- Hold the log path in std::optional<base::FilePath> (constexpr default) so the
  struct no longer embeds a non-constant-initializable std::wstring.
- Declare g_log_file constinit where the language provides it; the
  initialization is already constant under C++17.

The lazy-open behavior, the silent close-on-exit handle, and the enabled flag
are unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@jpnurmi
jpnurmi merged commit 2f5c168 into getsentry Aug 3, 2026
11 checks passed
@jpnurmi
jpnurmi deleted the fix/logging-constinit-early-static-init branch August 3, 2026 13:47
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
sentry-native 0.16.2

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.

- [ ] `resource` blocks have been checked for updates.
<details>
  <summary>release notes</summary>
  <pre>**Features**:

- Add `sentry_scope_remove_fingerprint` to remove a fingerprint set on a scope, matching the global `sentry_remove_fingerprint`. ([#1932](getsentry/sentry-native#1932))
- Add `sentry_options_set_before_send_feedback` to filter or enrich user feedback. Feedback does not go through `before_send`. ([#1923](getsentry/sentry-native#1923))
- Unix: Add `SENTRY_LINK_CURL` to control whether the curl transport links `libcurl` directly, loads it dynamically at runtime, or uses the default behavior. ([#1954](getsentry/sentry-native#1954))

**Fixes**:

- Name `sentry-logs` and `sentry-metrics` telemetry batcher threads so they can be identified in debuggers, profilers, and crash reports. ([#1937](getsentry/sentry-native#1937))
- Linux: close descriptor `0` on failed module file mapping when that descriptor was returned by `open`, avoiding a file descriptor leak. ([#1924](getsentry/sentry-native#1924))
- `sentry_attachment_set_filename`, `sentry_attachment_set_type`, and `sentry_attachment_set_content_type` now flush the scope, so changes applied after `sentry_attach_file`/`sentry_attach_bytes` also apply to hard-crash events instead of only to normal events. ([#1934](getsentry/sentry-native#1934))
- Crashpad: fix a crash when calling `sentry_init` before C++ dynamic initializers have run. ([#1930](getsentry/sentry-native#1930), [mini_chromium#8](getsentry/mini_chromium#8))
- Reduce the size of native-generated minidumps on Windows ([#1929](https://github.com/getsentry/sentry-native/pull/1929))</pre>
  <p>View the full release notes at <a href="https://github.com/getsentry/sentry-native/releases/tag/0.16.2">https://github.com/getsentry/sentry-native/releases/tag/0.16.2</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!16247
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.

3 participants