module scheme import arena fn static_cleanup_require(ok bool, message string) { if !ok { panic(message) } } fn static_cleanup_no_growth(storage &arena.Arena, before int, site int) { after := storage.used() if after != before { panic('cleanup site ${site} allocated ${after - before} bytes') } } @[heap] struct StaticCleanupFailure { identity int } fn (_ &StaticCleanupFailure) msg() string { return 'prepared host flush failure' } fn (problem &StaticCleanupFailure) code() int { return problem.identity } fn static_cleanup_same_error(err IError, problem &StaticCleanupFailure) { if err is StaticCleanupFailure { static_cleanup_require(voidptr(err) == voidptr(problem), 'cleanup replaced the prepared error') static_cleanup_require(err.code() == problem.identity, 'cleanup changed the error code') } else { panic('cleanup lost the prepared host error type') } } // Fixed storage and a prepared error keep the close allocation oracle about // cleanup, rather than allocations made by a growing sink or error factory. @[heap] struct StaticCleanupHost { input string problem &StaticCleanupFailure mut: offset int reads int bytes [64]u8 used int writes int write_limit int fail_write_at int flushes int fail_flush bool } fn (mut host StaticCleanupHost) read(mut destination []u8) !int { host.reads++ count := int_min(destination.len, host.input.len - host.offset) for i in 0 .. count { destination[i] = host.input[host.offset + i] } host.offset += count return count } fn (mut host StaticCleanupHost) write(bytes []u8) !int { host.writes++ if host.writes == host.fail_write_at { return host.problem } count := if host.write_limit > 0 { int_min(host.write_limit, bytes.len) } else { bytes.len } if count > host.bytes.len - host.used { panic('test sink capacity exceeded') } for i in 0 .. count { host.bytes[host.used + i] = bytes[i] } host.used += count return count } fn (mut host StaticCleanupHost) flush() ! { host.flushes++ if host.fail_flush { return host.problem } } fn static_cleanup_bytes(host &StaticCleanupHost, expected string) { static_cleanup_require(host.used == expected.len, 'wrong number of committed output bytes') for i in 0 .. expected.len { static_cleanup_require(host.bytes[i] == expected[i], 'output prefix was lost or duplicated') } } fn static_cleanup_put(mut vm VM, port Value, text string) ! { port_put_string(mut vm, [port, string_value(text)])! } fn static_cleanup_detached(group &CombinedTranscodedPort, port Value) { static_cleanup_require(port.input_pointer().closed && port.output_pointer().closed, 'facade stayed open') static_cleanup_require(!port.input_pointer().busy && !port.output_pointer().busy, 'facade stayed busy') static_cleanup_require(port.input_pointer().source is ClosedPort, 'facade retained its reader') static_cleanup_require(port.output_pointer().sink is ClosedPort, 'facade retained its writer') static_cleanup_require(group.input.raw.source is ClosedPort, 'raw input retained the host') static_cleanup_require(group.output.raw.sink is ClosedPort, 'raw output retained the host') static_cleanup_require(group.input.raw.buffer.len == 0, 'raw input buffer survived final close') static_cleanup_require(group.output.pending.len == 0, 'pending suffix survived final close') } fn test_static_cleanup_successful_one_way_transcoders_do_not_allocate() { mut storage := arena.new() storage.push() defer { storage.pop() storage.free() } mut vm := new() problem := &StaticCleanupFailure{ identity: 101 } mut host := &StaticCleanupHost{ input: 'λR', problem: problem } binary_input := vm.input_port(host, binary: true, read_size: 1) or { panic(err) } input := vm.transcoded_port(binary_input, transcoder_value(.utf8, .none, .raise)) or { panic(err) } static_cleanup_require((vm.peek_char(input) or { panic(err) }).whole() == `λ`, 'input did not decode Unicode') source := input.input_pointer().source if source is TranscodedInput { before := storage.used() vm.close_input_port(input) or { panic(err) } static_cleanup_no_growth(&storage, before, 1) static_cleanup_require(source.raw.closed && source.raw.buffer.len == 0, 'raw input storage was not released') static_cleanup_require(source.raw.source is ClosedPort, 'raw input retained the host reader') } else { panic('missing one-way input adapter') } static_cleanup_require(input.input_pointer().buffer.len == 0, 'facade input retained its buffer') static_cleanup_require(host.reads > 0 && host.flushes == 0, 'input close touched output') binary_output := vm.output_port(host, binary: true) or { panic(err) } output := vm.transcoded_port(binary_output, transcoder_value(.utf8, .none, .raise)) or { panic(err) } static_cleanup_put(mut vm, output, 'alphaλ') or { panic(err) } sink := output.output_pointer().sink if sink is TranscodedOutput { before := storage.used() vm.close_output_port(output) or { panic(err) } static_cleanup_no_growth(&storage, before, 2) static_cleanup_require(sink.raw.closed && sink.pending.len == 0, 'raw output storage was not released') static_cleanup_require(sink.raw.sink is ClosedPort, 'raw output retained the host writer') } else { panic('missing one-way output adapter') } static_cleanup_bytes(host, 'alphaλ') static_cleanup_require(host.flushes == 1, 'successful close did not flush exactly once') before_repeat := storage.used() vm.close_input_port(input) or { panic(err) } vm.close_output_port(output) or { panic(err) } static_cleanup_no_growth(&storage, before_repeat, 3) static_cleanup_require(host.flushes == 1, 'repeated close flushed again') } fn test_static_cleanup_one_way_flush_error_detaches_without_restarting_close() { mut storage := arena.new() storage.push() defer { storage.pop() storage.free() } mut vm := new() problem := &StaticCleanupFailure{ identity: 202 } mut host := &StaticCleanupHost{ problem: problem, fail_flush: true } binary := vm.output_port(host, binary: true) or { panic(err) } output := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) } static_cleanup_put(mut vm, output, 'saved') or { panic(err) } sink := output.output_pointer().sink if sink is TranscodedOutput { before := storage.used() if _ := vm.close_output_port(output) { panic('host flush failure was discarded') } else { static_cleanup_same_error(err, problem) } static_cleanup_no_growth(&storage, before, 4) static_cleanup_require(sink.raw.sink is ClosedPort, 'failed one-way close retained its host writer') static_cleanup_require(sink.pending.len == 0, 'failed one-way close retained pending bytes') } else { panic('missing one-way output adapter') } static_cleanup_require(output.output_pointer().closed && !output.output_pointer().busy, 'failed close stayed open or busy') static_cleanup_require(output.output_pointer().sink is ClosedPort, 'failed close retained its adapter') before_repeat := storage.used() vm.close_output_port(output) or { panic(err) } static_cleanup_no_growth(&storage, before_repeat, 5) static_cleanup_require(host.flushes == 1, 'failed close was restarted') static_cleanup_bytes(host, 'saved') } fn test_static_cleanup_combined_finalizer_releases_prepared_storage_without_allocating() { mut storage := arena.new() storage.push() defer { storage.pop() storage.free() } mut vm := new() problem := &StaticCleanupFailure{ identity: 505 } mut host := &StaticCleanupHost{ input: 'λRX', problem: problem } binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err) } port := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) } mut group := combined_transcoded_resource(port) static_cleanup_put(mut vm, port, 'sentλ') or { panic(err) } static_cleanup_require((vm.peek_char(port) or { panic(err) }).whole() == `λ`, 'input did not prepare a live buffer') // Public duplex dispatch currently allocates a temporary interface view. // Sampling the captured finalizer pins the cleanup boundary independently. before := storage.used() group.close_all() or { panic(err) } static_cleanup_no_growth(&storage, before, 13) static_cleanup_detached(group, port) static_cleanup_bytes(host, 'sentλ') static_cleanup_require(host.flushes == 1, 'finalizer did not flush exactly once') before_repeat := storage.used() vm.close_port(port) or { panic(err) } static_cleanup_no_growth(&storage, before_repeat, 14) static_cleanup_require(host.flushes == 1, 'public repeated close restarted finalization') // Closing borrowed capabilities must leave the underlying host usable. host.write([u8(`!`)]) or { panic(err) } static_cleanup_bytes(host, 'sentλ!') } fn test_static_cleanup_combined_flush_error_retains_raw_sink_until_final_retry() { mut storage := arena.new() storage.push() defer { storage.pop() storage.free() } mut vm := new() problem := &StaticCleanupFailure{ identity: 303 } mut host := &StaticCleanupHost{ input: 'RX', problem: problem, fail_flush: true } binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err) } port := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) } group := combined_transcoded_resource(port) static_cleanup_put(mut vm, port, 'hello') or { panic(err) } static_cleanup_require((vm.peek_char(port) or { panic(err) }).whole() == `R`, 'input did not warm') if _ := vm.close_output_port(port) { panic('combined flush failure was discarded') } else { static_cleanup_same_error(err, problem) } static_cleanup_require(port.output_pointer().closed && !port.input_pointer().closed, 'failed half-close closed the wrong facets') static_cleanup_require(port.output_pointer().sink is ClosedPort, 'closed TX facade retained its adapter') static_cleanup_require(group.output.raw.sink is StaticCleanupHost, 'failed half-close discarded the retry capability') static_cleanup_require(group.output.pending.len == 0, 'flush error reintroduced acknowledged bytes') static_cleanup_require(host.flushes == 1 && host.writes == 1, 'unexpected host activity on half-close') static_cleanup_require((vm.read_char(port) or { panic(err) }).whole() == `R`, 'RX stopped working after failed TX close') static_cleanup_require(host.flushes == 1, 'RX retried TX flush') host.fail_flush = false vm.close_input_port(port) or { panic(err) } static_cleanup_detached(group, port) static_cleanup_require(host.flushes == 2 && host.writes == 1, 'final cleanup did not retry only the failed flush') static_cleanup_bytes(host, 'hello') before_repeat := storage.used() vm.close_port(port) or { panic(err) } static_cleanup_no_growth(&storage, before_repeat, 8) static_cleanup_require(host.flushes == 2, 'final cleanup restarted after close') } fn test_static_cleanup_combined_pending_suffix_is_drained_once_without_close_allocations() { mut storage := arena.new() storage.push() defer { storage.pop() storage.free() } mut vm := new() problem := &StaticCleanupFailure{ identity: 404 } mut host := &StaticCleanupHost{ input: 'λRX', problem: problem, write_limit: 2, fail_write_at: 2 } binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err) } port := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) } mut group := combined_transcoded_resource(port) if _ := static_cleanup_put(mut vm, port, 'abcde') { panic('prepared partial write did not fail') } else { static_cleanup_same_error(err, problem) } static_cleanup_bytes(host, 'ab') static_cleanup_require(group.output.pending.len == 3 && group.output.pending_offset == 0, 'unsent suffix was not retained') host.fail_write_at = 3 if _ := vm.close_output_port(port) { panic('pending-suffix flush did not fail') } else { static_cleanup_same_error(err, problem) } static_cleanup_require(group.output.raw.sink is StaticCleanupHost, 'failed half-close discarded the pending writer') static_cleanup_require(group.output.pending.len == 3 && group.output.pending_offset == 0, 'failed half-close lost its suffix') static_cleanup_require((vm.read_char(port) or { panic(err) }).whole() == `λ`, 'independent RX stopped after TX error') static_cleanup_require(host.writes == 3, 'RX retried pending TX') host.fail_write_at = 0 before_final := storage.used() // This is the finalizer called by the public final-half close. Resolve its // resource before sampling, excluding the separate interface-view lookup. group.close_all() or { panic(err) } static_cleanup_no_growth(&storage, before_final, 10) static_cleanup_detached(group, port) static_cleanup_bytes(host, 'abcde') static_cleanup_require(host.writes == 5 && host.flushes == 1, 'pending suffix was not drained exactly once') } fn test_static_cleanup_independent_arenas_keep_closed_stubs_and_output_snapshots_valid() { mut short := arena.new() short.push() mut first := new() first_input := first.open_input_string('short') or { panic(err) } first_output := first.open_output_string() or { panic(err) } first.close_input_port(first_input) or { panic(err) } first.close_output_port(first_output) or { panic(err) } short.pop() mut survivor := arena.new() survivor.push() mut vm := new() input := vm.open_input_string('survivor') or { panic(err) } output := vm.open_output_string() or { panic(err) } static_cleanup_put(mut vm, output, 'alphaλ') or { panic(err) } snapshot := vm.get_output_string(output) or { panic(err) } static_cleanup_put(mut vm, output, '/tail') or { panic(err) } before := survivor.used() vm.close_input_port(input) or { panic(err) } vm.close_output_port(output) or { panic(err) } static_cleanup_no_growth(&survivor, before, 11) complete := vm.get_output_string(output) or { panic(err) } survivor.pop() // No value or interface from short is inspected after this free. The second // VM and its own port/snapshot storage all remain alive in survivor. short.free() survivor.push() static_cleanup_require((snapshot.as_string() or { panic(err) }) == 'alphaλ', 'earlier snapshot changed after append/close') static_cleanup_require((complete.as_string() or { panic(err) }) == 'alphaλ/tail', 'completed snapshot lost output') vm.define('retained-snapshot', snapshot) or { panic(err) } vm.eval('(string-set! retained-snapshot 0 #\\X)') or { panic(err) } static_cleanup_require((snapshot.as_string() or { panic(err) }) == 'Xlphaλ', 'snapshot mutation did not apply') static_cleanup_require((complete.as_string() or { panic(err) }) == 'alphaλ/tail', 'independent snapshot was aliased') latest := vm.get_output_string(output) or { panic(err) } static_cleanup_require((latest.as_string() or { panic(err) }) == 'alphaλ/tail', 'snapshot mutation changed the closed collector') mut reader := input.input_pointer().source mut writer := output.output_pointer().sink mut bytes := [u8(0)] if _ := reader.read(mut bytes) { panic('survivor closed reader succeeded') } else { static_cleanup_require(err.msg() == 'input port is closed', 'survivor reader lost its stub') } if _ := writer.write(bytes) { panic('survivor closed writer succeeded') } else { static_cleanup_require(err.msg() == 'output port is closed', 'survivor writer lost its stub') } before_repeat := survivor.used() vm.close_input_port(input) or { panic(err) } vm.close_output_port(output) or { panic(err) } static_cleanup_no_growth(&survivor, before_repeat, 12) survivor.pop() survivor.free() }