Skip to content

fix: 8 MB release stack (#223) + atomic telemetry call_count (#179)#236

Merged
justrach merged 3 commits intomainfrom
fix/223-rosetta-stack-size-telemetry-race
Apr 11, 2026
Merged

fix: 8 MB release stack (#223) + atomic telemetry call_count (#179)#236
justrach merged 3 commits intomainfrom
fix/223-rosetta-stack-size-telemetry-race

Conversation

@justrach
Copy link
Copy Markdown
Owner

Summary

  • darwin-x86_64: startup crash under Rosetta on Apple Silicon #223 — Rosetta crash: ReleaseFast mainImpl only uses ~190 KB of stack (verified via otool disassembly). The 64 MB trampoline thread was only needed in Debug builds where Zig merges branch frames into a large frame. Reduce to 8 MB for non-Debug builds — still 40× the actual need, well within Rosetta 2's limits. Fixes the KERN_PROTECTION_FAILURE / SIGBUS / SIGILL crash on codedb-darwin-x86_64 under Rosetta.

  • Bugs + improvements #179 (telemetry data race): call_count was a plain u32 incremented after write_lock was released, making concurrent record() calls undefined behaviour. Changed to std.atomic.Value(u32) with fetchAdd(.monotonic) — flush/syncToCloud stay outside the lock since they're slow I/O ops.

Test plan

  • zig build test passes
  • Cross-compile zig build -Doptimize=ReleaseFast -Dtarget=x86_64-macos succeeds
  • otool confirms main() stack frame is 80 bytes (trampoline overhead only), mainImpl frame is ~190 KB — both well under 8 MB
  • Verify codedb-darwin-x86_64 --version succeeds under Rosetta on Apple Silicon

🤖 Generated with Claude Code

justrach and others added 3 commits April 11, 2026 09:59
ReleaseFast mainImpl only uses ~190 KB of stack (vs the ~33 MB in Debug
builds). Reduce the trampoline thread stack to 8 MB in non-Debug modes
so the 64 MB allocation no longer triggers Rosetta 2's protection fault
on x86_64-macos (KERN_PROTECTION_FAILURE / SIGBUS / SIGILL).

Fix the telemetry data race: call_count was a plain u32 mutated after
write_lock was released. Change it to std.atomic.Value(u32) with a
fetchAdd so concurrent record() calls have no undefined behaviour.

Co-Authored-By: trilokagent <275208033+trilokagent@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
AnyTrigramIndex was refactored to a tagged union (heap/mmap/mmap_overlay)
but approxIndexSizeBytes still accessed .index and .file_trigrams as
direct fields, causing a compilation error. Switch on the union tag and
iterate only for the .heap variant; mmap variants have no HashMap to walk.

Co-Authored-By: trilokagent <275208033+trilokagent@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@justrach justrach force-pushed the fix/223-rosetta-stack-size-telemetry-race branch from 64db291 to 7b3644c Compare April 11, 2026 02:03
@justrach justrach merged commit f69a043 into main Apr 11, 2026
@justrach justrach deleted the fix/223-rosetta-stack-size-telemetry-race branch April 11, 2026 02:03
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