Fatal error: exception Stack_overflow from ocamlrun only under production-size inputs
0 reputation · 12 Jan 2025, 13:06 UTC
0 reputation · 12 Jan 2025, 13:06 UTC
An OCaml program compiled to bytecode with ocamlc and run under ocamlrun terminates with Fatal error: exception Stack_overflow when processing production-scale data. The same binary handles the smaller inputs used in local testing without any failure.
The failing code path is a recursive function over a large list (on the order of hundreds of thousands of elements). The function is not written in tail-recursive style, so each recursive call consumes C stack space in the bytecode interpreter. Local test data stays well below the default stack limit, which is why the overflow only surfaces with production input sizes.
The constraint is that the production deployment must keep using the bytecode runtime for now, and the input size cannot be capped. Two options are on the table: rewriting the recursion to be tail-recursive (possibly with an accumulator and a final List.rev), or raising the stack limit available to ocamlrun. Each has portability and memory trade-offs that are not fully clear.
OCAMLRUNPARAM l setting) a reliable production fix, or does it only postpone the overflow for larger inputs?ocamlopt) builds enough that switching the production target would change the failure threshold?A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.