cce init crashes (SIGBUS/SIGSEGV) — tree-sitter 0.26.0 core / 0.25.x grammar ABI skew corrupts Node.start_point/end_point
Summary
cce init crashes the whole process with a native signal (SIGBUS / exit 138, or SIGSEGV / exit 139, varies by run) while indexing a real-world monorepo (~4k files, JS/TS/Python/etc). Crash is nondeterministic: which file is being processed when it dies changes between runs, and the signal itself changes between runs. This is a race-condition fingerprint, confirmed via faulthandler + manual depth instrumentation.
Environment
- macOS, arm64 (Apple Silicon)
cce0.4.24 (installed viauv tool install)- Python 3.11.15
- Project: Turbo monorepo, several thousand source files (JS/TS/Python mixed)
Repro
cd <large-mixed-language-monorepo>
cce init
Output stops mid-indexing:
Indexing project...
Embedding 50 chunks (batch 1/235)…
Writing 50 chunks to vector + FTS + graph index (batch 1/235)…
Process exits with code 138 (SIGBUS) or 139 (SIGSEGV) — no Python traceback, no error caught by CCE's own exception handling (it's a native crash, not a Python exception).
Root cause
indexer/pipeline.py:341 creates a single shared chunker = Chunker() for the whole indexing run.
indexer/chunker.py:44-54:
class Chunker:
def __init__(self) -> None:
self._parsers: dict[str, Parser] = {}
def _get_parser(self, language: str) -> Parser | None:
if language not in _LANGUAGES:
return None
if language not in self._parsers:
parser = Parser(_LANGUAGES[language])
self._parsers[language] = parser
return self._parsers[language]
One tree_sitter.Parser instance is cached per language and reused for every file of that language for the lifetime of the indexing run.
indexer/pipeline.py:388-391 and :548-553:
async def _chunk_file(rel_path: str, content: str, language: str):
return await asyncio.to_thread(
chunker.chunk_with_imports, content, rel_path, language
)
...
chunk_tasks = [
_chunk_file(rel_path, content, language)
for (_, rel_path, content, _, language) in to_chunk
]
chunk_results = await asyncio.gather(*chunk_tasks, return_exceptions=True)
Each batch (_BATCH = 50) dispatches up to 50 files concurrently via asyncio.to_thread, which runs them on real OS threads from the default ThreadPoolExecutor. Every file of the same language calls parser.parse() on the same shared Parser object, with no lock around it.
tree_sitter.Parser (and the Tree it produces) is not safe to use concurrently from multiple threads — the C library keeps internal parser state that isn't synchronized. Concurrent parse() calls on one Parser instance race on that state and corrupt memory, which shows up as SIGBUS or SIGSEGV depending on what got clobbered.
Why this reproduces as "somewhere in _walk" but isn't a stack-depth bug
I initially suspected unbounded recursion in chunker.py:67-73 _walk() (no depth guard, runs on a thread with a smaller stack than the main thread). Large minified bundles in the repo (e.g. a 3.2MB Storybook runtime bundle, AST depth 254) made the crash easy to trigger and pointed that way at first.
But after excluding those files via .cceignore, the crash still occurred — on a completely different, small (12KB) JS file. I added depth telemetry directly into _walk (print on depth thresholds 50/100/150/...). The crash reoccurred with zero depth-threshold prints firing, and faulthandler's crash-time stack for the top frame showed only ~6 recursive frames deep — nowhere near a stack-overflow depth. The crashing file also differs between runs with identical input, and the signal number itself differs (138 vs 139) between runs. That's consistent with a data race, not a deterministic algorithmic bug.
Suggested fixes (any one of these)
- Give each worker thread its own
Parserinstance instead of sharing one per language, e.g.threading.local()-backed parser cache inChunker. - Wrap
parser.parse()(and the subsequent tree walk, since theTree/Nodeobjects are backed by the same non-thread-safe C state) in athreading.Lockper language. - Reduce
_BATCHchunking concurrency to serializechunk_with_importscalls per language (defeats some of the parallelism, but is correct).
Given tree_sitter.Parser objects are cheap to construct, option 1 (thread-local parsers) seems like the cleanest fix with no throughput loss.
Impact
This makes cce init unreliable (crashes, sometimes non-reproducibly) on any project large/parallel enough to have multiple same-language files chunked concurrently in a batch — which is the common case for real-world monorepos, not an edge case.
Correction — root cause is NOT thread concurrency
Retracting the original theory. Re-tested with a global lock fully serializing every chunk_with_imports call (single file chunked at a time, no overlap) — crash still occurred (SIGSEGV), same point. That rules out a Parser-instance data race entirely.
Actual root cause: ABI/version skew between tree-sitter core and grammar packages.
Installed:
tree-sitter 0.26.0
tree-sitter-javascript 0.25.0
tree-sitter-python 0.25.0
tree-sitter-typescript 0.23.2
tree-sitter-php 0.24.1
tree-sitter-go 0.25.0
tree-sitter-rust 0.24.2
tree-sitter-java 0.23.5
Minimal repro, zero code-context-engine code, single-threaded:
import tree_sitter_javascript as tsjs
from tree_sitter import Language, Parser
lang = Language(tsjs.language())
p = Parser(lang)
src = open("statusline.js", "rb").read() # the specific file from the repo, 12KB
tree = p.parse(src)
def walk(n):
_ = n.start_point.row; _ = n.end_point.row # <-- this line segfaults
for c in n.children: walk(c)
walk(tree.root_node) # SIGSEGV / SIGBUS here
Bisected precisely:
.childrentraversal alone: fine- byte-slicing (
src[node.start_byte:node.end_byte]): fine - accessing
.start_point/.end_point(thePointstruct) on nodes: crashes, for this specific file's parse tree (not universal — a trivial one-liner JS snippet parses fine, so it's dependent on tree size/shape, consistent with memory corruption rather than a logic bug)
Fix that resolves it: pin tree-sitter to 0.25.0 to match the grammar packages (all still on the 0.25.x ABI). Confirmed the exact same file/walk no longer crashes after downgrading, and a full cce init run over the same repo (chunker.py otherwise untouched, no thread-local patch needed) completes without SIGBUS/SIGSEGV.
Suggested fix for the project: pin tree-sitter<0.26 in pyproject.toml/requirements until the tree-sitter-* grammar packages catch up to the 0.26 ABI, or bump all grammar packages to their 0.26-compatible releases together.
The thread-local-parser patch from the original report is unnecessary for this bug (though arguably still good practice since tree_sitter.Parser reuse across threads is documented as unsafe) — it did not fix the crash, which is what led to this re-investigation.
Huge thanks @sahemaraj — this is one of the best bug reports we've had. The faulthandler + depth instrumentation, the bisect down to a single-threaded zero-CCE-code repro on .start_point, and then catching your own first hypothesis (the parser thread race) and re-investigating to the real ABI cause — that saved us a ton of time and pointed straight at the fix.
Two root causes came out of this, and we've addressed both:
1. tree-sitter ABI skew (the trigger) — uv tool install resolves tree-sitter>=0.22 fresh from PyPI (ignoring uv.lock) and pulls 0.26.0 core against grammar wheels still built on the 0.25.x ABI. Reading Node.start_point/end_point under that mismatch corrupts the heap → nondeterministic SIGSEGV/SIGBUS.
→ Fixed by capping tree-sitter>=0.22,<0.26 in #115. (This also resolves #114, which was the same corruption surfacing downstream at the embed/write step.)
2. The parser thread race you flagged first — you were right that sharing one tree_sitter.Parser per language across up to 50 concurrent asyncio.to_thread workers is unsafe, even though it wasn't what triggered this crash. We've fixed it too, so it can't resurface under a future tree-sitter build: parsers are now cached in threading.local(), giving each worker thread its own Parser.
→ #116.
Workaround until a release goes out:
uv tool install --with 'tree-sitter<0.26' --force code-context-engine
# or in an existing env: pip install 'tree-sitter==0.25.2'
Thanks again for the rigor — leaving this open until both PRs land.