Skip to content

Handle cross-thread slab frees#782

Open
SS-42 wants to merge 1 commit into
DeusData:mainfrom
SS-42:fix/slab-cross-thread-free
Open

Handle cross-thread slab frees#782
SS-42 wants to merge 1 commit into
DeusData:mainfrom
SS-42:fix/slab-cross-thread-free

Conversation

@SS-42

@SS-42 SS-42 commented Jul 2, 2026

Copy link
Copy Markdown

Summary

This fixes slab allocator ownership for tree-sitter allocations that are freed on a different parser thread than the one that allocated them.

The allocator now keeps a global slab-page registry. Each page still has an owner thread for reuse, but cross-thread frees are recognized as slab pointers instead of falling through to plain free(). Reclaim/destroy detaches pages with no live chunks and retires pages that still have live chunks, freeing them when the final chunk is returned.

The slab payload is explicitly aligned to max_align_t; ASan/UBSan caught misaligned slab chunks after the page metadata changed.

Observed environment: macOS 26.5.2 (25F84), Darwin 25.5.0 arm64, Apple clang 21.0.0. The crash signature was observed through macOS malloc diagnostics, but the allocator ownership bug is not macOS-specific.

Test plan

  • make -f Makefile.cbm build/c/test-runner CC=clang CXX=clang++
  • ASAN_OPTIONS=detect_leaks=0 ./build/c/test-runner mem
  • sanitizer log grep for runtime error, SUMMARY: UndefinedBehaviorSanitizer, and ERROR: AddressSanitizer
  • ASAN_OPTIONS=detect_leaks=0 ./build/c/test-runner pipeline
  • sanitizer log grep for runtime error, SUMMARY: UndefinedBehaviorSanitizer, and ERROR: AddressSanitizer
  • make -f Makefile.cbm build/c/codebase-memory-mcp CC=clang CXX=clang++
  • ./build/c/codebase-memory-mcp --version

@SS-42 SS-42 requested a review from DeusData as a code owner July 2, 2026 18:42
@SS-42 SS-42 force-pushed the fix/slab-cross-thread-free branch 2 times, most recently from 2ae8c34 to 5f891fe Compare July 2, 2026 19:04
Signed-off-by: SS-42 <noreply@incogni.to>
@SS-42 SS-42 force-pushed the fix/slab-cross-thread-free branch from 5f891fe to b391b01 Compare July 2, 2026 19:22
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