| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | // Only committed exits containing custom ports need this cold state. Entries |
| 6 | // and diagnostic sites are immutable; continuation capture detaches progress. |
| 7 | @[heap] |
| 8 | struct ProcessExitCleanup { |
| 9 | request ExitRequest |
| 10 | entries []Value |
| 11 | bridge_sites []BridgeSite |
| 12 | context u64 |
| 13 | wind &WindFrame = unsafe { nil } |
| 14 | parameters &ParameterBinding = unsafe { nil } |
| 15 | parameter_base &ParameterBinding = unsafe { nil } |
| 16 | mut: |
| 17 | index int |
| 18 | in_flight bool |
| 19 | lease &CustomPortLease = unsafe { nil } |
| 20 | } |
| 21 | |
| 22 | const process_exit_cleanup_bridge_code = [ |
| 23 | Instruction{ op: .process_exit_finish, a: 1, origin: reader.no_node } |
| 24 | Instruction{ op: .process_exit_finish, a: 2, origin: reader.no_node } |
| 25 | Instruction{ op: .jump, a: 0, origin: reader.no_node } |
| 26 | ]! |
| 27 | |
| 28 | const process_exit_cleanup_bridge = Function{ |
| 29 | code: unsafe { nil } |
| 30 | slot_count: 1 |
| 31 | instructions: unsafe { static_bridge_instructions(&process_exit_cleanup_bridge_code[0], process_exit_cleanup_bridge_code.len) } |
| 32 | } |
| 33 | |
| 34 | fn process_exit_cleanup_state(env &Environment) &ProcessExitCleanup { |
| 35 | return unsafe { &ProcessExitCleanup(env.slots[0].pointer()) } |
| 36 | } |
| 37 | |
| 38 | fn (mut m Machine) begin_process_exit_cleanup(request ExitRequest, index int) { |
| 39 | state := &ProcessExitCleanup{ |
| 40 | request: request |
| 41 | entries: copy_values(m.stack, 0, m.stack.len) |
| 42 | bridge_sites: m.bridge_sites.clone() |
| 43 | context: m.logical_context |
| 44 | wind: m.wind |
| 45 | parameters: m.parameters |
| 46 | parameter_base: m.parameter_base |
| 47 | index: index |
| 48 | } |
| 49 | m.truncate(0) |
| 50 | m.function = &process_exit_cleanup_bridge |
| 51 | m.env = &Environment{ slots: [payload_value(.port_operation, state)] } |
| 52 | m.ip = 0 |
| 53 | m.base = 0 |
| 54 | } |
| 55 | |
| 56 | fn (mut m Machine) writable_process_exit_cleanup() &ProcessExitCleanup { |
| 57 | mut state := process_exit_cleanup_state(m.env) |
| 58 | if m.env.escaped { |
| 59 | mut scope := m.detach_bridge() |
| 60 | state = &ProcessExitCleanup{ ...state, entries: unsafe { state.entries }, bridge_sites: unsafe { state.bridge_sites } } |
| 61 | scope.slots[0] = payload_value(.port_operation, state) |
| 62 | } |
| 63 | return state |
| 64 | } |
| 65 | |
| 66 | fn (mut m Machine) step_process_exit_cleanup(mut vm VM) ! { |
| 67 | mut state := m.writable_process_exit_cleanup() |
| 68 | for state.index < state.entries.len { |
| 69 | target := state.entries[state.index].pair_pointer().car |
| 70 | if isnil(custom_port_resource(target)) { |
| 71 | vm.close_port(target) or { if is_control_transfer(err) { return err } } |
| 72 | state.index++ |
| 73 | continue |
| 74 | } |
| 75 | operation := vm.prepare_custom_port_operation(.close_any, target, [target])! |
| 76 | state.lease = operation.lease |
| 77 | state.in_flight = true |
| 78 | // Retain this queue as a caller of the existing close bridge. Callback |
| 79 | // continuations now include the collector and remaining exit resources. |
| 80 | m.call_custom_port_bridge(mut vm, &custom_port_io_bridges[int(PortIoOperation.close_any)], operation, m.stack.len, false)! |
| 81 | return |
| 82 | } |
| 83 | vm.options.exit_handler(mut vm, state.request)! |
| 84 | return state.request |
| 85 | } |
| 86 | |
| 87 | fn (mut m Machine) collect_process_exit_cleanup() { |
| 88 | m.pop_value() // A closer may return any number of values. |
| 89 | mut state := m.writable_process_exit_cleanup() |
| 90 | state.in_flight = false |
| 91 | state.lease = unsafe { nil } |
| 92 | state.index++ |
| 93 | } |
| 94 | |
| 95 | // Called only after an ordinary failure, before an error bridge can erase the |
| 96 | // queue's frame. The queue is a root exit continuation, so its collector owns |
| 97 | // an empty operand/frame prefix. No borrowed frame or operand slice escapes. |
| 98 | fn (m &Machine) process_exit_cleanup_recovery(owner &Owner) &Continuation { |
| 99 | if !isnil(m.close_failure) && !isnil(m.close_failure.exit_recovery) { |
| 100 | return m.close_failure.exit_recovery |
| 101 | } |
| 102 | mut scope := unsafe { &Environment(nil) } |
| 103 | if voidptr(m.function) == voidptr(&process_exit_cleanup_bridge) { |
| 104 | scope = m.env |
| 105 | } else { |
| 106 | for index := m.frames.len - 1; index >= 0; index-- { |
| 107 | if voidptr(m.frames[index].function) == voidptr(&process_exit_cleanup_bridge) { |
| 108 | scope = m.frames[index].env |
| 109 | break |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | if isnil(scope) { return unsafe { nil } } |
| 114 | state := process_exit_cleanup_state(scope) |
| 115 | // An error in the host hook, or before the close lease entered, is not a |
| 116 | // callback close error and must not retry the hook or skip an open resource. |
| 117 | if !state.in_flight || isnil(state.lease) || !state.lease.close_started { |
| 118 | return unsafe { nil } |
| 119 | } |
| 120 | escape_environment(scope) |
| 121 | return &Continuation{ |
| 122 | owner: owner |
| 123 | context: state.context |
| 124 | wind: state.wind |
| 125 | function: &process_exit_cleanup_bridge |
| 126 | env: scope |
| 127 | ip: 1 |
| 128 | parameters: state.parameters |
| 129 | parameter_base: state.parameter_base |
| 130 | bridge_sites: unsafe { state.bridge_sites } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | fn (mut m Machine) recover_process_exit_cleanup(mut vm VM, saved &Continuation) !bool { |
| 135 | // Step exhaustion is an ordinary diagnostic, but cannot grant a new budget |
| 136 | // to its own cleanup. Cancellation/arena controls propagate before here. |
| 137 | if vm.options.max_steps > 0 && m.steps >= vm.options.max_steps { return false } |
| 138 | recovery := if isnil(saved) { m.process_exit_cleanup_recovery(vm.owner) } else { saved } |
| 139 | if isnil(recovery) { return false } |
| 140 | m.reserve_restore_buffers(vm, recovery) or { |
| 141 | return m.abandon_process_exit_cleanup(mut vm, err) |
| 142 | } |
| 143 | m.parameters = recovery.parameters |
| 144 | m.cleanup_custom_port_leases() |
| 145 | m.cleanup_library_context() |
| 146 | vm.cleanup_file_context(m.context) |
| 147 | // Ordinary errors abandon user winders. Restore only the queue collector, |
| 148 | // after releasing internal leases; never reenter the failed writer/closer. |
| 149 | m.restore(recovery, unspecified()) |
| 150 | return true |
| 151 | } |
| 152 | |
| 153 | // Only the final error return abandons a committed queue. Recoverable close |
| 154 | // errors must keep its outer native ports open for the remaining callbacks. |
| 155 | // Cross-entry transfers are still winding and can be canceled by the parent. |
| 156 | fn (m &Machine) abandon_process_exit_cleanup(mut vm VM, err IError) IError { |
| 157 | if err is ContinuationTransfer { return err } |
| 158 | if !isnil(vm.system) && !isnil(vm.system.exit_function) { |
| 159 | vm.close_pending_exit_files(m, true) |
| 160 | } |
| 161 | return err |
| 162 | } |
| 163 | |