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)