Skip to content

fix(prompt_cache): make radix trie iterative to prevent stack overflow#63

Merged
inureyes merged 1 commit into
mainfrom
fix/prompt-cache-trie-iterative
May 21, 2026
Merged

fix(prompt_cache): make radix trie iterative to prevent stack overflow#63
inureyes merged 1 commit into
mainfrom
fix/prompt-cache-trie-iterative

Conversation

@inureyes
Copy link
Copy Markdown
Member

Summary

The radix trie backing the prompt cache was only partially iterative: pop_prefixes and Drop already avoided recursion, but insert_into, remove_from, and dfs (reached through for_each_candidate) still recursed one stack frame per trie node. On an adversarially deep prompt trie — one node per token, which defeats path compression because every sibling branch is unique — that recursion overflows the stack.

This is a real denial-of-service vector, not just a test artifact. The same recursive paths run on ~2 MiB Tokio worker stacks in production via PromptCacheStore insert/eviction and the per-request longest-prefix candidate walk, so a crafted prompt sequence can crash a worker. It also aborted the entire cargo test --lib run (the recursive insert overflowed even the test's 32 MiB build thread), which is why unrelated sibling tests appeared to fail under the full run yet passed when rerun individually.

Changes

Rewrites the three remaining recursive operations to run in O(1) stack:

  • insert_into — descend while recording the path, mutate at the terminal node, then repair subtree_count bottom-up along the recorded path.
  • remove_from — descend recording (node, descend-key) ancestors, then prune empty children, merge a lone empty non-root node, and repair counts bottom-up; the now-unused is_root parameter is dropped.
  • dfs — walks an explicit heap-allocated stack instead of the call stack (traversal order stays unspecified, as the for_each_candidate contract already permits).

Testing

  • pop_prefixes_deep_branching_chain_does_not_overflow now builds and exercises the whole trie (insert, longest-prefix lookup + candidate walk, remove, pop_prefixes, and Drop) directly on a 2 MiB Tokio-worker-equivalent stack with N=4096 (~4× the depth that overflows a 2 MiB stack), replacing the prior 32 MiB build-thread workaround.
  • cargo test --features metal,accelerate --lib prompt_cache::trie → 21 passed, 0 failed.
  • cargo clippy --features metal,accelerate --lib --tests -- -D warnings → clean.
  • cargo fmt --all -- --check → clean.

The deep-branching regression test pop_prefixes_deep_branching_chain_does_not_overflow aborted the entire `cargo test --lib` run with a stack overflow. The trie's pop_prefixes and Drop were already iterative, but insert_into, remove_from, and dfs (reached via for_each_candidate) still recursed one stack frame per trie node, so the trie was only partially iterative.

On an adversarially deep prompt trie — one node per token, which defeats path compression because every sibling branch is unique — the recursive insert overflowed even the test's 32 MiB build thread in the full debug build and aborted the process. That is why sibling tests appeared to fail under the full run yet passed when rerun individually. The same recursion also executes on ~2 MiB Tokio worker stacks in production through PromptCacheStore insert and eviction and the per-request longest-prefix candidate walk, so a crafted prompt sequence is a real stack-overflow denial-of-service vector, not just a test artifact.

Rewrite all three remaining recursive operations to run in O(1) stack:

- insert_into: descend while recording the path, mutate at the terminal node, then repair subtree_count bottom-up along the recorded path.
- remove_from: descend recording (node, descend-key) ancestors, then prune empty children, merge a lone empty non-root node, and repair counts bottom-up; the now-unused is_root parameter is dropped.
- dfs: walk an explicit heap-allocated stack instead of the call stack (traversal order remains unspecified, as the for_each_candidate contract already allows).

The regression test now builds and exercises the whole trie — insert, longest-prefix lookup plus candidate walk, remove, pop_prefixes, and the final Drop — directly on a 2 MiB stack that matches a Tokio worker, with N=4096 (about 4x the depth that overflows a 2 MiB stack). This replaces the previous 32 MiB build-thread workaround: if any path is still recursive the spawned thread overflows and aborts, its join() returns Err, and the test fails.
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions type:security Security vulnerability or fix priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) labels May 21, 2026
@inureyes inureyes merged commit f6c37ac into main May 21, 2026
4 checks passed
@inureyes inureyes deleted the fix/prompt-cache-trie-iterative branch May 21, 2026 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High priority type:bug Bug fixes, error corrections, or issue resolutions type:security Security vulnerability or fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant