module scheme import reader // Only committed exits containing custom ports need this cold state. Entries // and diagnostic sites are immutable; continuation capture detaches progress. @[heap] struct ProcessExitCleanup { request ExitRequest entries []Value bridge_sites []BridgeSite context u64 wind &WindFrame = unsafe { nil } parameters &ParameterBinding = unsafe { nil } parameter_base &ParameterBinding = unsafe { nil } mut: index int in_flight bool lease &CustomPortLease = unsafe { nil } } const process_exit_cleanup_bridge_code = [ Instruction{ op: .process_exit_finish, a: 1, origin: reader.no_node } Instruction{ op: .process_exit_finish, a: 2, origin: reader.no_node } Instruction{ op: .jump, a: 0, origin: reader.no_node } ]! const process_exit_cleanup_bridge = Function{ code: unsafe { nil } slot_count: 1 instructions: unsafe { static_bridge_instructions(&process_exit_cleanup_bridge_code[0], process_exit_cleanup_bridge_code.len) } } fn process_exit_cleanup_state(env &Environment) &ProcessExitCleanup { return unsafe { &ProcessExitCleanup(env.slots[0].pointer()) } } fn (mut m Machine) begin_process_exit_cleanup(request ExitRequest, index int) { state := &ProcessExitCleanup{ request: request entries: copy_values(m.stack, 0, m.stack.len) bridge_sites: m.bridge_sites.clone() context: m.logical_context wind: m.wind parameters: m.parameters parameter_base: m.parameter_base index: index } m.truncate(0) m.function = &process_exit_cleanup_bridge m.env = &Environment{ slots: [payload_value(.port_operation, state)] } m.ip = 0 m.base = 0 } fn (mut m Machine) writable_process_exit_cleanup() &ProcessExitCleanup { mut state := process_exit_cleanup_state(m.env) if m.env.escaped { mut scope := m.detach_bridge() state = &ProcessExitCleanup{ ...state, entries: unsafe { state.entries }, bridge_sites: unsafe { state.bridge_sites } } scope.slots[0] = payload_value(.port_operation, state) } return state } fn (mut m Machine) step_process_exit_cleanup(mut vm VM) ! { mut state := m.writable_process_exit_cleanup() for state.index < state.entries.len { target := state.entries[state.index].pair_pointer().car if isnil(custom_port_resource(target)) { vm.close_port(target) or { if is_control_transfer(err) { return err } } state.index++ continue } operation := vm.prepare_custom_port_operation(.close_any, target, [target])! state.lease = operation.lease state.in_flight = true // Retain this queue as a caller of the existing close bridge. Callback // continuations now include the collector and remaining exit resources. m.call_custom_port_bridge(mut vm, &custom_port_io_bridges[int(PortIoOperation.close_any)], operation, m.stack.len, false)! return } vm.options.exit_handler(mut vm, state.request)! return state.request } fn (mut m Machine) collect_process_exit_cleanup() { m.pop_value() // A closer may return any number of values. mut state := m.writable_process_exit_cleanup() state.in_flight = false state.lease = unsafe { nil } state.index++ } // Called only after an ordinary failure, before an error bridge can erase the // queue's frame. The queue is a root exit continuation, so its collector owns // an empty operand/frame prefix. No borrowed frame or operand slice escapes. fn (m &Machine) process_exit_cleanup_recovery(owner &Owner) &Continuation { if !isnil(m.close_failure) && !isnil(m.close_failure.exit_recovery) { return m.close_failure.exit_recovery } mut scope := unsafe { &Environment(nil) } if voidptr(m.function) == voidptr(&process_exit_cleanup_bridge) { scope = m.env } else { for index := m.frames.len - 1; index >= 0; index-- { if voidptr(m.frames[index].function) == voidptr(&process_exit_cleanup_bridge) { scope = m.frames[index].env break } } } if isnil(scope) { return unsafe { nil } } state := process_exit_cleanup_state(scope) // An error in the host hook, or before the close lease entered, is not a // callback close error and must not retry the hook or skip an open resource. if !state.in_flight || isnil(state.lease) || !state.lease.close_started { return unsafe { nil } } escape_environment(scope) return &Continuation{ owner: owner context: state.context wind: state.wind function: &process_exit_cleanup_bridge env: scope ip: 1 parameters: state.parameters parameter_base: state.parameter_base bridge_sites: unsafe { state.bridge_sites } } } fn (mut m Machine) recover_process_exit_cleanup(mut vm VM, saved &Continuation) !bool { // Step exhaustion is an ordinary diagnostic, but cannot grant a new budget // to its own cleanup. Cancellation/arena controls propagate before here. if vm.options.max_steps > 0 && m.steps >= vm.options.max_steps { return false } recovery := if isnil(saved) { m.process_exit_cleanup_recovery(vm.owner) } else { saved } if isnil(recovery) { return false } m.reserve_restore_buffers(vm, recovery) or { return m.abandon_process_exit_cleanup(mut vm, err) } m.parameters = recovery.parameters m.cleanup_custom_port_leases() m.cleanup_library_context() vm.cleanup_file_context(m.context) // Ordinary errors abandon user winders. Restore only the queue collector, // after releasing internal leases; never reenter the failed writer/closer. m.restore(recovery, unspecified()) return true } // Only the final error return abandons a committed queue. Recoverable close // errors must keep its outer native ports open for the remaining callbacks. // Cross-entry transfers are still winding and can be canceled by the parent. fn (m &Machine) abandon_process_exit_cleanup(mut vm VM, err IError) IError { if err is ContinuationTransfer { return err } if !isnil(vm.system) && !isnil(vm.system.exit_function) { vm.close_pending_exit_files(m, true) } return err }