Skip to content

Return a catchable error instead of panicking on pathological encode input - #150

Open
AbhinavMir wants to merge 1 commit into
openai:mainfrom
AbhinavMir:harmony-encode-stackoverflow-catchable
Open

Return a catchable error instead of panicking on pathological encode input#150
AbhinavMir wants to merge 1 commit into
openai:mainfrom
AbhinavMir:harmony-encode-stackoverflow-catchable

Conversation

@AbhinavMir

Copy link
Copy Markdown

#93

fancy_regex's find_iter/find_from_pos return a Result and can fail with
RuntimeError(StackOverflow) when the backtracking engine exceeds its stack
limit, for example on a very long single-character run. CoreBPE::encode_ordinary
and CoreBPE::encode unwrapped those results, turning the error into a panic that
surfaced in Python as an uncatchable pyo3_runtime.PanicException and could abort
host processes.

Add an EncodeError type and propagate the regex error through encode_ordinary,
encode, encode_with_special_tokens, and _encode_unstable_native instead of
unwrapping. Map it to HarmonyError at the PyO3 boundary and to a JsValue error
at the wasm boundary so callers receive a normal catchable exception. Add Rust
and Python regression tests for the long single-character run.
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