Fix VM reuse_stack lost across stub VM boundary on IO suspension

Root cause: when perform fires inside a VM closure chain (call_closure_reuse),
the caller frames are saved to reuse_stack on the ACTIVE VM. But the
_cek_io_suspend_hook and _cek_eval_lambda_ref create a NEW stub VM for the
VmSuspended exception. On resume, resume_vm runs on the STUB VM which has
an empty reuse_stack — the caller frames are orphaned on the original VM.

Fix: transfer reuse_stack from _active_vm to the stub VM before raising
VmSuspended. This ensures resume_vm -> restore_reuse can find and restore
the caller's frames after async resume via _driveAsync/setTimeout.

Also restore step_limit/step_count refs dropped by bootstrap.py regeneration.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-17 13:31:55 +00:00
parent bceccccedb
commit c0b001d3c2
4 changed files with 1921 additions and 2004 deletions

View File

@@ -10,7 +10,9 @@ open Sx_runtime
let trampoline_fn : (value -> value) ref = ref (fun v -> v)
let trampoline v = !trampoline_fn v
(* Step limit for timeout protection *)
let step_limit : int ref = ref 0
let step_count : int ref = ref 0
(* === Mutable globals — backing refs for transpiler's !_ref / _ref := === *)
let _strict_ref = ref (Bool false)

View File

@@ -1029,6 +1029,13 @@ let () = _cek_io_suspend_hook := Some (fun suspended_state ->
let request = Sx_ref.cek_io_request suspended_state in
let vm = create !_default_vm_globals in
vm.pending_cek <- Some suspended_state;
(* Transfer reuse_stack from the active VM so resume_vm can restore
caller frames saved by call_closure_reuse during the suspension chain. *)
(match !_active_vm with
| Some active when active.reuse_stack <> [] ->
vm.reuse_stack <- active.reuse_stack;
active.reuse_stack <- []
| _ -> ());
raise (VmSuspended (request, vm)))
let () = _cek_eval_lambda_ref := (fun f args ->
@@ -1040,6 +1047,12 @@ let () = _cek_eval_lambda_ref := (fun f args ->
resume_vm will: cek_resume → push result → run (no-op, no frames) → pop *)
let vm = create (Hashtbl.create 0) in
vm.pending_cek <- Some final;
(* Transfer reuse_stack from active VM *)
(match !_active_vm with
| Some active when active.reuse_stack <> [] ->
vm.reuse_stack <- active.reuse_stack;
active.reuse_stack <- []
| _ -> ());
raise (VmSuspended (Sx_runtime.get_val final (String "request"), vm))
| _ -> Sx_ref.cek_value final)

File diff suppressed because one or more lines are too long

View File

@@ -1792,7 +1792,7 @@
blake2_js_for_wasm_create: blake2_js_for_wasm_create};
}
(globalThis))
({"link":[["runtime-0db9b496",0],["prelude-d7e4b000",0],["stdlib-23ce0836",[]],["re-9a0de245",[2]],["sx-7c87b1f0",[2,3]],["jsoo_runtime-f96b44a8",[2]],["js_of_ocaml-651f6707",[2,5]],["dune__exe__Sx_browser-def18509",[2,4,6]],["std_exit-10fb8830",[2]],["start-f808dbe1",0]],"generated":(b=>{var
({"link":[["runtime-0db9b496",0],["prelude-d7e4b000",0],["stdlib-23ce0836",[]],["re-9a0de245",[2]],["sx-a2e416eb",[2,3]],["jsoo_runtime-f96b44a8",[2]],["js_of_ocaml-651f6707",[2,5]],["dune__exe__Sx_browser-7cf00582",[2,4,6]],["std_exit-10fb8830",[2]],["start-f808dbe1",0]],"generated":(b=>{var
c=b,a=b?.module?.export||b;return{"env":{"caml_ba_kind_of_typed_array":()=>{throw new
Error("caml_ba_kind_of_typed_array not implemented")},"caml_exn_with_js_backtrace":()=>{throw new
Error("caml_exn_with_js_backtrace not implemented")},"caml_int64_create_lo_mi_hi":()=>{throw new