From 37677d81d92880a3e836e33e9ea64706b2f9a14b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 21:43:45 -0800 Subject: [PATCH 1/8] chore(quest): claim quest/m1/ffi-speed/kotlin-close.md Quest-Claim: 9d71441bd7ccc1ac From 1b281a6ec4602c9acdb4f6f5ba4e5eee9b67041e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 22:59:03 -0800 Subject: [PATCH 2/8] perf(ffi): Kotlin's checked close in one JNI crossing Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 7 ++- ffi/README.md | 4 +- ffi/runtime/rt.go | 4 +- ffi/runtime/rt.kt | 27 ++++++++--- ffi/src/kotlin.rs | 6 +-- ffi/tests/api.kt | 27 +++++++++++ ffi/tests/hold.rs | 76 +++++++++++++++++++++++++++++- quest/m1/ffi-proofs/loom-hold.md | 22 +++++++++ quest/m1/ffi-speed/kotlin-close.md | 25 ---------- 9 files changed, 158 insertions(+), 40 deletions(-) create mode 100644 quest/m1/ffi-proofs/loom-hold.md delete mode 100644 quest/m1/ffi-speed/kotlin-close.md diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 88531c952..e996b7432 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -550,8 +550,11 @@ frame path: that finds its P's nursery. Since kixel-ai/rust2#880, each Kotlin thread holds one too, and a close no longer links and unlinks a phantom reference under a lock: Kotlin's object return fell from 124 to 118 ns median wall (from 112 to 94 at best) and from 56 to 38 on 4 - threads at once. What it has left is mostly a checked close's two JNI crossings, its drop - and then its free ([TypeScript](#the-nursery) has the web's). + threads at once. Since kixel-ai/rust2#906, a checked close no call holds drops and frees in + one JNI crossing, as Go's and Dart's do: measured 2026-11-19 on **fedora** at a load + average of 11 to 23, 8 rounds alternating with main at `1fd44bdd`, Kotlin's object return + fell from 100 to 81 ns median wall (from 92 to 80 at best) and from 190 to 160 CPU + ([TypeScript](#the-nursery) has the web's). 3. **A woken read.** C's 427 ns is Rust's 329 plus a future's crossings. Kotlin's 1.6 us and Dart's 1.8 us add their event loops; Go's 5.1 us and Swift's 5.7 us wall (16.6 us CPU) are a goroutine or `Task` and a cross-thread wake per read, and Python's 6.9 us is diff --git a/ffi/README.md b/ffi/README.md index a5462979f..d6b2274d2 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -619,7 +619,9 @@ the rest cross encoded. until it returns, so the handle is freed only once no call holds it; a call after `close()` throws. Of a checked type, `close()` drops the value through Rust's own `_drop`, as Rust's `drop` would, and throws instead while a call borrows the value, as - Rust refuses that drop, leaving the value to a later `close()`. Of any other type, + Rust refuses that drop, leaving the value to a later `close()`; one no call holds, as + soon after its return, drops and frees in one crossing, and a `Drop` that calls back into + Kotlin finds it closed. Of any other type, `close()` frees the handle, and throws while a call holds it. A returned object waits in a nursery of the last 64 its thread returned, where `close()` frees it without the GC's registration, a phantom reference linked into a list under a lock. One never closed is diff --git a/ffi/runtime/rt.go b/ffi/runtime/rt.go index 0b8e148ac..1746ccd6f 100644 --- a/ffi/runtime/rt.go +++ b/ffi/runtime/rt.go @@ -6,7 +6,7 @@ // cgo makes its own, in ffi_cgo.go; it reaches this through Go's types alone. // The holds a handle counts, one for its owner until closed and one per call, shifted -// past a bit set once closed, one set while an unchecked Close decides, and one set once +// past a bit set once closed, one set while a Close that may yet fail decides, and one set once // calls contended for them, after which they hold it in slots_: in one word, so a call // reads them all at once, and the count is below held_ only once no hold is left. const ( @@ -409,7 +409,7 @@ func (h *Handle_) Moved() { func (h *Handle_) abandon() { for { n := h.state.Load() - // An unchecked Close is deciding: wait for its outcome, which may leave h open. + // A Close is deciding: wait for its outcome, which may leave h open. if n&closing_ != 0 { runtime.Gosched() continue diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index 765a72f2b..a70cff8a8 100644 --- a/ffi/runtime/rt.kt +++ b/ffi/runtime/rt.kt @@ -485,7 +485,7 @@ class __Handle( private val drop: __Close? = null, ) { // The holds, one for the owner until closed and one per call, shifted past a bit set - // once closed, one set while an unchecked [close] decides, and one set once calls + // once closed, one set while a [close] that may yet fail decides, and one set once calls // contended for them, after which they hold it in [SLOTS]: one word, so a call reads // them all at once, and below [HELD] only once no hold is left. @kotlin.concurrent.Volatile @kotlin.jvm.JvmField internal var state = HELD @@ -509,7 +509,7 @@ class __Handle( * finds it closed and frees nothing: [CLOSED], claimed once, decides the one free. */ internal fun evict(owner: Any) { - // An unchecked close still deciding may leave it open. + // A close still deciding may leave it open. if (state and (CLOSED or CLOSING) == CLOSED) return cleanup = __Collected(owner, this) } @@ -566,13 +566,28 @@ class __Handle( /** Close it as [__Handle] says; again, it does nothing. */ fun close() { if (drop != null) { + // Held by no call, as soon after its return: claimed closed so none starts, then + // the value dropped and the handle freed in one crossing. A call meanwhile throws + // as closed, as one racing any close may, and a `Drop` that calls back into Kotlin + // finds it closed rather than waiting on itself. + if (STATE.compareAndSet(this, HELD, CLOSED or CLOSING)) { + try { + drop.close(raw, true) + } catch (e: Throwable) { + state = HELD + throw e + } + cleanup?.stop() + state = CLOSED + return + } val slot = try { hold() } catch (_: IllegalStateException) { return } try { - drop.close(raw) + drop.close(raw, false) } finally { release(slot) } @@ -628,7 +643,7 @@ class __Handle( fun abandon() { while (true) { val n = state - // An unchecked close is deciding: wait for its outcome, which may leave it open. + // A close is deciding: wait for its outcome, which may leave it open. if (n and CLOSING != 0L) { java.lang.Thread.yield() continue @@ -833,9 +848,9 @@ fun interface __Drop { fun drop(raw: Long) } -/** Drops the value of a raw handle, leaving the handle. */ +/** Drops the value of a raw handle, then frees the handle too if [free] and the drop passed. */ fun interface __Close { - fun close(raw: Long) + fun close(raw: Long, free: Boolean) } /** diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index edbedfd49..fdd791551 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -2047,14 +2047,14 @@ impl<'a> Gen<'a> { true => { self.native( &format!("{bare}_drop"), - &[("jlong", "Long")], + &[("jlong", "Long"), ("jboolean", "Boolean")], ("()", "Unit"), &format!( - "let mut status = 0;\n\t\tcrate::{c}_drop(p0 as usize as _, &mut status);\n\t\tif status != 0 {{\n\t\t\treturn rt::refuse(env, status, \"{c}_drop\");\n\t\t}}" + "let mut status = 0;\n\t\tcrate::{c}_drop(p0 as usize as _, &mut status);\n\t\tif status != 0 {{\n\t\t\treturn rt::refuse(env, status, \"{c}_drop\");\n\t\t}}\n\t\tif p1 {{\n\t\t\tcrate::{c}_free(p0 as usize as _);\n\t\t}}" ), ); format!( - "\tinternal val handle = _Native.__Handle(this, raw, if (lent) null else _Native.__Drop {{ _Native.{bare}_free(it) }}, \"{name}\", if (lent) null else _Native.__Close {{ _Native.{bare}_drop(it) }})\n\n\t/** Drop the value now, as Rust's drop does, and free the handle once no call holds it: a call after this throws, and one still borrowing the value makes this throw. */\n\toverride fun close() = handle.close()\n" + "\tinternal val handle = _Native.__Handle(this, raw, if (lent) null else _Native.__Drop {{ _Native.{bare}_free(it) }}, \"{name}\", if (lent) null else _Native.__Close {{ p, free -> _Native.{bare}_drop(p, free) }})\n\n\t/** Drop the value now, as Rust's drop does, and free the handle once no call holds it: a call after this throws, and one still borrowing the value makes this throw. */\n\toverride fun close() = handle.close()\n" ) } false => format!( diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 6c89f5181..d5b0f5f63 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -839,6 +839,33 @@ fun owned() { check(dropped() == before + 1u, "closing again frees nothing") check(runCatching { token.idGet }.exceptionOrNull()?.message == "Token is closed", "a call after close throws") + // A close no call holds drops and frees in one crossing: refused under a Rust borrow, it + // leaves the object open, and a close from inside its own drop finds it closed. + val ledger = Ledger.new() + val draft = ledger.edit() + val refused = runCatching { ledger.close() }.exceptionOrNull() + check(refused is IllegalStateException && refused.message == "api_Ledger_drop: a handle is already borrowed", "a close under a borrow throws: $refused") + draft.close() + check(ledger.published() == 0u, "a refused close leaves the object open") + ledger.close() + check(runCatching { ledger.published() }.exceptionOrNull()?.message == "Ledger is closed", "a close after a refused one closes") + lateinit var journal: Journal + var during: String? = null + var closes = 0 + val nested = object : Log, AutoCloseable { + override fun log(level: UByte, line: String): Boolean = true + + override fun close() { + closes++ + during = runCatching { journal.write("x") }.exceptionOrNull()?.message + journal.close() + } + } + journal = Journal.new(nested) + journal.close() + check(closes == 1, "a close from inside its own drop closes nothing more") + check(during == "Journal is closed", "an object is closed in Kotlin while Rust drops it: $during") + // The file serves wasm32 too: what only wasm32 has throws on the host's library. check(pointerBits(platform()) == 64u && pointerBits(Platform.Native(64u)) == 64u, "the host is native") check(isFamily(Family.NATIVE), "and of the native family") diff --git a/ffi/tests/hold.rs b/ffi/tests/hold.rs index 74e2ff31c..2f575ad84 100644 --- a/ffi/tests/hold.rs +++ b/ffi/tests/hold.rs @@ -16,7 +16,7 @@ use loom::thread; const CLOSED: usize = 1; /// Set in the hold count once calls contended for it, after which they go to the table. const BIAS: usize = 2; -/// Set beside [`CLOSED`] while an unchecked close decides, which may yet leave it open. +/// Set beside [`CLOSED`] while a close decides, which may yet leave it open. const CLOSING: usize = 4; /// One hold in the count, past the bits above, so the count is below it only once no hold /// is left. @@ -166,6 +166,40 @@ impl Hold { Closed::Now } + /// Close a checked handle no call holds in one crossing, as Go's and Kotlin's do: claimed + /// closed so no call starts, then the value dropped, which `refused` has Rust refuse as + /// under a borrow, reopening it, and the handle freed. `None` while a hold or a close + /// stands, when the binding holds it for a drop alone and then gives up the owner's hold. + fn drop_free(&self, refused: bool) -> Option { + if self.state.compare_exchange(HELD, CLOSED | CLOSING, SeqCst, SeqCst).is_err() { + return None; + } + if refused { + self.state.store(HELD, SeqCst); + return Some(false); + } + self.free(); + self.state.store(CLOSED, SeqCst); + Some(true) + } + + /// Close a checked handle as a binding does: in one crossing if no call holds it, else + /// held for the drop, then giving up the owner's hold. Whether it closed it. + fn checked_close(&self, column: usize, refused: bool) -> bool { + if let Some(closed) = self.drop_free(refused) { + return closed; + } + let Some(slot) = self.hold(column) else { + return true; + }; + self.release(slot); + if refused { + return false; + } + self.abandon(); + true + } + /// No hold is left, and the handle was freed once, or not at all while still open. fn settled(&self, closed: bool) { assert!(self.slots.iter().all(|slot| slot.load(SeqCst) == 0)); @@ -304,6 +338,46 @@ fn closes_against_biased_call() { }); } +/// A checked close against calls on the count, which bias it as they meet, and a GC giving +/// up the owner's hold: a call starting during the one-crossing drop finds it closed, and +/// the handle is freed once, which no read races. +#[test] +fn checked_close_against_calls() { + model(|| { + let hold = Hold::new(2, false); + let (other, collector) = (hold.clone(), hold.clone()); + // A call already holding it, besides those starting: loom misses a close between a + // hold's load and its compare-and-swap, so the model holds one up front. + let running = hold.hold(1).unwrap(); + let calls = thread::spawn(move || { + assert_eq!(other.read(), 7); + other.release(running); + call(&other, 1); + call(&other, 1) + }); + let collected = thread::spawn(move || collector.abandon()); + assert!(hold.checked_close(0, false)); + calls.join().unwrap(); + collected.join().unwrap(); + hold.settled(true); + }); +} + +/// A checked close Rust refuses, against a call: it leaves the handle open, unfreed, and +/// usable, whichever path it took. +#[test] +fn checked_close_refused() { + model(|| { + let hold = Hold::new(2, false); + let other = hold.clone(); + let held = thread::spawn(move || call(&other, 1)); + assert!(!hold.checked_close(0, true)); + held.join().unwrap(); + assert!(call(&hold, 0)); + hold.settled(false); + }); +} + /// Calls that never meet a close leave the handle open and unfreed. #[test] fn calls_alone() { diff --git a/quest/m1/ffi-proofs/loom-hold.md b/quest/m1/ffi-proofs/loom-hold.md new file mode 100644 index 000000000..12667d02f --- /dev/null +++ b/quest/m1/ffi-proofs/loom-hold.md @@ -0,0 +1,22 @@ +# [S] Hold model's missed interleavings + +## Goal + +`ffi/tests/hold.rs`, the loom model of the hold counts Go, Kotlin, and Swift share, explores +a close or move landing between a call's hold and its release on the count, and fails on a +mutant that frees a handle a call holds there. + +## Plan + +Found in [#906](https://github.com/kixel-ai/rust2/pull/906): loom 0.7.3 never ran another +thread's load between a thread's `load` then `compare_exchange` and its later `fetch_sub`. +A probe, one thread doing `load`, `compare_exchange(n, n + 8)`, `fetch_sub(8)` against +another's `load`, passed `assert_eq!(n, 8)` in every execution, while the same probe without +the leading `load` failed it. So the model's calls on the count, whose fast path is that +`load` and `compare_exchange`, never meet a close mid-call, and a checked close that skipped +its `state == HELD` claim passed every model until #906 held one call up front. + +Find why (loom's DPOR with SeqCst, a preemption bound, or the probe), then make every model +reach those interleavings: a newer loom, an explicit scheduling point, or calls held up front +as #906's does. Check with mutants of `hold`, `close`, `abandon`, and `drop_free` that each +model fails. diff --git a/quest/m1/ffi-speed/kotlin-close.md b/quest/m1/ffi-speed/kotlin-close.md deleted file mode 100644 index 842a6c3b1..000000000 --- a/quest/m1/ffi-speed/kotlin-close.md +++ /dev/null @@ -1,25 +0,0 @@ -# [S] Kotlin's checked close in one crossing - -## Goal - -A Kotlin checked object's `close()` that no call holds drops its value and frees its -handle in one JNI crossing, as Go's and Dart's have since -[#838](https://github.com/kixel-ai/rust2/pull/838), and the binding benchmark reports the -object-return row before and after on HotSpot. - -## Plan - -Decided with the maintainer on 2026-11-19, in the pick of [#880](https://github.com/kixel-ai/rust2/pull/880): ✅ kept (rejected: dropping it). - -Found while profiling [#880](https://github.com/kixel-ai/rust2/pull/880)'s nursery under -async-profiler: once the nursery removed the phantom reference, a checked close's two JNI -crossings, `_drop` and then `_free` (`ffi/runtime/rt.kt`, `__Handle.close`), -held as many samples as `track.info()`'s own call. The object return is 118 ns median wall -against C's 31. - -Follow Go's design: claim the handle closed so no call starts, then drop and free in one -glue entry, failing closed rather than waiting for calls that start during the drop, since -a `Drop` that calls back into Kotlin on the same object would wait on itself. A close that -fails under a borrow reopens the handle. - -Public API: none. From 34a733fd2b3879512ee6995f0b46c9abc394560d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 22:59:51 -0800 Subject: [PATCH 3/8] style: fmt hold model --- ffi/tests/hold.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ffi/tests/hold.rs b/ffi/tests/hold.rs index 2f575ad84..33b6e0788 100644 --- a/ffi/tests/hold.rs +++ b/ffi/tests/hold.rs @@ -171,7 +171,11 @@ impl Hold { /// under a borrow, reopening it, and the handle freed. `None` while a hold or a close /// stands, when the binding holds it for a drop alone and then gives up the owner's hold. fn drop_free(&self, refused: bool) -> Option { - if self.state.compare_exchange(HELD, CLOSED | CLOSING, SeqCst, SeqCst).is_err() { + if self + .state + .compare_exchange(HELD, CLOSED | CLOSING, SeqCst, SeqCst) + .is_err() + { return None; } if refused { From 22f531a6d903f18a3fc4db02f043b5dcf65d0ae4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 23:19:00 -0800 Subject: [PATCH 4/8] fix(ffi): a Kotlin close waits out another's one-crossing drop, and keeps its owner reachable Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 4 +-- ffi/runtime/rt.kt | 80 ++++++++++++++++++++++++++++++++--------------- ffi/src/kotlin.rs | 4 +-- ffi/tests/hold.rs | 39 ++++++++++++++++++----- 4 files changed, 91 insertions(+), 36 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index d6b2274d2..b2ba67dbc 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -620,8 +620,8 @@ the rest cross encoded. `close()` throws. Of a checked type, `close()` drops the value through Rust's own `_drop`, as Rust's `drop` would, and throws instead while a call borrows the value, as Rust refuses that drop, leaving the value to a later `close()`; one no call holds, as - soon after its return, drops and frees in one crossing, and a `Drop` that calls back into - Kotlin finds it closed. Of any other type, + soon after its return, takes the value through `_drop` and frees the handle in the same + crossing, and a `Drop` that calls back into Kotlin finds it closed. Of any other type, `close()` frees the handle, and throws while a call holds it. A returned object waits in a nursery of the last 64 its thread returned, where `close()` frees it without the GC's registration, a phantom reference linked into a list under a lock. One never closed is diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index a70cff8a8..bbeef74cb 100644 --- a/ffi/runtime/rt.kt +++ b/ffi/runtime/rt.kt @@ -499,6 +499,10 @@ class __Handle( // Plain: a free that misses an eviction's write leaves a cleanup that finds it closed. private var cleanup: __Collected? = null + // The thread whose close drops the value in one crossing, while it does. Plain: only that + // thread finds itself here, as it writes and clears it while [CLOSING] is set. + private var closer: java.lang.Thread? = null + init { if (free != null) __Nursery.push(owner, this) } @@ -563,35 +567,17 @@ class __Handle( freed(STATE.addAndGet(this, -HELD)) } - /** Close it as [__Handle] says; again, it does nothing. */ - fun close() { + /** + * Close it as [__Handle] says, [owner] reachable until it returns, so a cleanup of the + * owner never waits out a drop; again, it does nothing. + */ + fun close(owner: Any) { if (drop != null) { - // Held by no call, as soon after its return: claimed closed so none starts, then - // the value dropped and the handle freed in one crossing. A call meanwhile throws - // as closed, as one racing any close may, and a `Drop` that calls back into Kotlin - // finds it closed rather than waiting on itself. - if (STATE.compareAndSet(this, HELD, CLOSED or CLOSING)) { - try { - drop.close(raw, true) - } catch (e: Throwable) { - state = HELD - throw e - } - cleanup?.stop() - state = CLOSED - return - } - val slot = try { - hold() - } catch (_: IllegalStateException) { - return - } try { - drop.close(raw, false) + return closeChecked(drop) } finally { - release(slot) + __Keep.fence(owner) } - return abandon() } while (true) { val n = state @@ -615,6 +601,50 @@ class __Handle( freed(CLOSED) } + // [close] of a handle whose value Rust checks. + private fun closeChecked(drop: __Close) { + // Held by no call, as soon after its return: claimed closed so none starts, then the + // value dropped and the handle freed in one crossing. A call meanwhile throws as + // closed, as one racing any close may, and a `Drop` that calls back into Kotlin finds + // it closed rather than waiting on itself. + if (STATE.compareAndSet(this, HELD, CLOSED or CLOSING)) { + closer = java.lang.Thread.currentThread() + try { + drop.close(raw, true) + } catch (e: Throwable) { + closer = null + state = HELD + throw e + } + closer = null + cleanup?.stop() + state = CLOSED + return + } + while (true) { + val n = state + // Another close is deciding: wait for its outcome, which may leave it open, unless + // this is that close's drop calling back. + if (n and CLOSING != 0L) { + if (closer === java.lang.Thread.currentThread()) return + java.lang.Thread.yield() + continue + } + if (n and CLOSED != 0L) return + val slot = try { + hold() + } catch (_: IllegalStateException) { + continue + } + try { + drop.close(raw, false) + } finally { + release(slot) + } + return abandon() + } + } + // Whether a call holds it in the table. private fun listed(): Boolean { if (state and BIAS == 0L) return false diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index fdd791551..e61e30bf8 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -2054,11 +2054,11 @@ impl<'a> Gen<'a> { ), ); format!( - "\tinternal val handle = _Native.__Handle(this, raw, if (lent) null else _Native.__Drop {{ _Native.{bare}_free(it) }}, \"{name}\", if (lent) null else _Native.__Close {{ p, free -> _Native.{bare}_drop(p, free) }})\n\n\t/** Drop the value now, as Rust's drop does, and free the handle once no call holds it: a call after this throws, and one still borrowing the value makes this throw. */\n\toverride fun close() = handle.close()\n" + "\tinternal val handle = _Native.__Handle(this, raw, if (lent) null else _Native.__Drop {{ _Native.{bare}_free(it) }}, \"{name}\", if (lent) null else _Native.__Close {{ p, free -> _Native.{bare}_drop(p, free) }})\n\n\t/** Drop the value now, as Rust's drop does, and free the handle once no call holds it: a call after this throws, and one still borrowing the value makes this throw. */\n\toverride fun close() = handle.close(this)\n" ) } false => format!( - "\tinternal val handle = _Native.__Handle(this, raw, if (lent) null else _Native.__Drop {{ _Native.{bare}_free(it) }}, \"{name}\")\n\n\t/** Free the handle now: a call after this throws, and one still running makes this throw. */\n\toverride fun close() = handle.close()\n" + "\tinternal val handle = _Native.__Handle(this, raw, if (lent) null else _Native.__Drop {{ _Native.{bare}_free(it) }}, \"{name}\")\n\n\t/** Free the handle now: a call after this throws, and one still running makes this throw. */\n\toverride fun close() = handle.close(this)\n" ), }]; let mut statics = Vec::new(); diff --git a/ffi/tests/hold.rs b/ffi/tests/hold.rs index 33b6e0788..07b1f1493 100644 --- a/ffi/tests/hold.rs +++ b/ffi/tests/hold.rs @@ -193,15 +193,26 @@ impl Hold { if let Some(closed) = self.drop_free(refused) { return closed; } - let Some(slot) = self.hold(column) else { + loop { + let n = self.state.load(SeqCst); + // Another close is deciding, which may leave it open: wait for its outcome. + if n & CLOSING != 0 { + thread::yield_now(); + continue; + } + if n & CLOSED != 0 { + return true; + } + let Some(slot) = self.hold(column) else { + continue; + }; + self.release(slot); + if refused { + return false; + } + self.abandon(); return true; - }; - self.release(slot); - if refused { - return false; } - self.abandon(); - true } /// No hold is left, and the handle was freed once, or not at all while still open. @@ -382,6 +393,20 @@ fn checked_close_refused() { }); } +/// Two checked closes Rust refuses: neither reports the handle closed while the other's +/// refused drop reopens it. +#[test] +fn checked_closes_refused() { + model(|| { + let hold = Hold::new(2, false); + let other = hold.clone(); + let first = thread::spawn(move || other.checked_close(1, true)); + assert!(!hold.checked_close(0, true)); + assert!(!first.join().unwrap()); + hold.settled(false); + }); +} + /// Calls that never meet a close leave the handle open and unfreed. #[test] fn calls_alone() { From f2cfd6d23c4b63ce34b4283450e9e3dbff072e30 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 23:38:02 -0800 Subject: [PATCH 5/8] docs: the benchmark after review fixes, and Go's close follow-up Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 7 ++++--- quest/m1/ffi-speed/go-close-race.md | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 quest/m1/ffi-speed/go-close-race.md diff --git a/bench/ffi/README.md b/bench/ffi/README.md index e996b7432..ff3297b7e 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -551,9 +551,10 @@ frame path: a close no longer links and unlinks a phantom reference under a lock: Kotlin's object return fell from 124 to 118 ns median wall (from 112 to 94 at best) and from 56 to 38 on 4 threads at once. Since kixel-ai/rust2#906, a checked close no call holds drops and frees in - one JNI crossing, as Go's and Dart's do: measured 2026-11-19 on **fedora** at a load - average of 11 to 23, 8 rounds alternating with main at `1fd44bdd`, Kotlin's object return - fell from 100 to 81 ns median wall (from 92 to 80 at best) and from 190 to 160 CPU + one JNI crossing, as Go's and Dart's do: measured 2026-11-20 on **fedora** at a load + average of 11 to 30, 8 rounds alternating with main at `1fd44bdd`, Kotlin's object return + fell from 93 to 77 ns wall at best, and from 132 to 83 median, as half of main's rounds + ran at 169 to 199 and none of this one's past 92; and from 260 to 170 median CPU ([TypeScript](#the-nursery) has the web's). 3. **A woken read.** C's 427 ns is Rust's 329 plus a future's crossings. Kotlin's 1.6 us and Dart's 1.8 us add their event loops; Go's 5.1 us and Swift's 5.7 us wall (16.6 us CPU) diff --git a/quest/m1/ffi-speed/go-close-race.md b/quest/m1/ffi-speed/go-close-race.md new file mode 100644 index 000000000..941610319 --- /dev/null +++ b/quest/m1/ffi-speed/go-close-race.md @@ -0,0 +1,21 @@ +# [S] Go's Close waits out a one-crossing drop + +## Goal + +A Go `Close()` that meets another goroutine's one-crossing drop of a checked object returns +only once that drop's outcome stands: nil once the handle is closed, `rt.ErrBorrowed` once +Rust refused the drop and the handle reopened. A `Drop` that calls back into Go and closes +the same object still returns at once. + +## Plan + +Found in review of [#906](https://github.com/kixel-ai/rust2/pull/906), which fixed the same +gap in Kotlin. In `ffi/runtime/rt.go`, `Handle_.Close`'s slow path returns nil when `Hold` +finds the handle closed, though the closed bit may be the `closed_|closing_` claim of a drop +Rust then refuses, which restores `held_`. + +Kotlin tells the drop's own callback from another thread by the closing thread. Go has no +goroutine identity, so the callback needs another mark: such as a flag the cgo callback +entry sets for its goroutine's duration, or the drop's glue passing the handle being dropped +to callbacks it runs. Model it as `checked_closes_refused` in `ffi/tests/hold.rs` models +Kotlin's. From 728b664e2a48720a120bde55dd43aeff69a54a41 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 23:47:57 -0800 Subject: [PATCH 6/8] fix(ffi): a Kotlin close meeting another's drop returns at once again Waiting it out hangs a Drop that hands the close to a joined thread. Co-Authored-By: Claude Opus 5.5 --- ffi/runtime/rt.kt | 43 +++++++++-------------------- ffi/tests/api.kt | 10 +++++++ ffi/tests/hold.rs | 39 +++++--------------------- quest/m1/ffi-speed/close-race.md | 27 ++++++++++++++++++ quest/m1/ffi-speed/go-close-race.md | 21 -------------- 5 files changed, 57 insertions(+), 83 deletions(-) create mode 100644 quest/m1/ffi-speed/close-race.md delete mode 100644 quest/m1/ffi-speed/go-close-race.md diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index bbeef74cb..33855b5f4 100644 --- a/ffi/runtime/rt.kt +++ b/ffi/runtime/rt.kt @@ -499,10 +499,6 @@ class __Handle( // Plain: a free that misses an eviction's write leaves a cleanup that finds it closed. private var cleanup: __Collected? = null - // The thread whose close drops the value in one crossing, while it does. Plain: only that - // thread finds itself here, as it writes and clears it while [CLOSING] is set. - private var closer: java.lang.Thread? = null - init { if (free != null) __Nursery.push(owner, this) } @@ -605,44 +601,31 @@ class __Handle( private fun closeChecked(drop: __Close) { // Held by no call, as soon after its return: claimed closed so none starts, then the // value dropped and the handle freed in one crossing. A call meanwhile throws as - // closed, as one racing any close may, and a `Drop` that calls back into Kotlin finds - // it closed rather than waiting on itself. + // closed, as one racing any close may, and a close meanwhile, such as one a `Drop` + // calls back into Kotlin to make, from any thread, returns at once rather than wait + // on a drop that may wait on it. if (STATE.compareAndSet(this, HELD, CLOSED or CLOSING)) { - closer = java.lang.Thread.currentThread() try { drop.close(raw, true) } catch (e: Throwable) { - closer = null state = HELD throw e } - closer = null cleanup?.stop() state = CLOSED return } - while (true) { - val n = state - // Another close is deciding: wait for its outcome, which may leave it open, unless - // this is that close's drop calling back. - if (n and CLOSING != 0L) { - if (closer === java.lang.Thread.currentThread()) return - java.lang.Thread.yield() - continue - } - if (n and CLOSED != 0L) return - val slot = try { - hold() - } catch (_: IllegalStateException) { - continue - } - try { - drop.close(raw, false) - } finally { - release(slot) - } - return abandon() + val slot = try { + hold() + } catch (_: IllegalStateException) { + return } + try { + drop.close(raw, false) + } finally { + release(slot) + } + abandon() } // Whether a call holds it in the table. diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index d5b0f5f63..64b1e5b0a 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -865,6 +865,16 @@ fun owned() { journal.close() check(closes == 1, "a close from inside its own drop closes nothing more") check(during == "Journal is closed", "an object is closed in Kotlin while Rust drops it: $during") + // Closed again from another thread its drop waits on, which returns rather than wait. + lateinit var handed: Journal + val joined = object : Log, AutoCloseable { + override fun log(level: UByte, line: String): Boolean = true + + override fun close() = thread { handed.close() }.join() + } + handed = Journal.new(joined) + handed.close() + check(runCatching { handed.write("x") }.exceptionOrNull()?.message == "Journal is closed", "a close its drop hands to another thread returns") // The file serves wasm32 too: what only wasm32 has throws on the host's library. check(pointerBits(platform()) == 64u && pointerBits(Platform.Native(64u)) == 64u, "the host is native") diff --git a/ffi/tests/hold.rs b/ffi/tests/hold.rs index 07b1f1493..33b6e0788 100644 --- a/ffi/tests/hold.rs +++ b/ffi/tests/hold.rs @@ -193,26 +193,15 @@ impl Hold { if let Some(closed) = self.drop_free(refused) { return closed; } - loop { - let n = self.state.load(SeqCst); - // Another close is deciding, which may leave it open: wait for its outcome. - if n & CLOSING != 0 { - thread::yield_now(); - continue; - } - if n & CLOSED != 0 { - return true; - } - let Some(slot) = self.hold(column) else { - continue; - }; - self.release(slot); - if refused { - return false; - } - self.abandon(); + let Some(slot) = self.hold(column) else { return true; + }; + self.release(slot); + if refused { + return false; } + self.abandon(); + true } /// No hold is left, and the handle was freed once, or not at all while still open. @@ -393,20 +382,6 @@ fn checked_close_refused() { }); } -/// Two checked closes Rust refuses: neither reports the handle closed while the other's -/// refused drop reopens it. -#[test] -fn checked_closes_refused() { - model(|| { - let hold = Hold::new(2, false); - let other = hold.clone(); - let first = thread::spawn(move || other.checked_close(1, true)); - assert!(!hold.checked_close(0, true)); - assert!(!first.join().unwrap()); - hold.settled(false); - }); -} - /// Calls that never meet a close leave the handle open and unfreed. #[test] fn calls_alone() { diff --git a/quest/m1/ffi-speed/close-race.md b/quest/m1/ffi-speed/close-race.md new file mode 100644 index 000000000..7628f9285 --- /dev/null +++ b/quest/m1/ffi-speed/close-race.md @@ -0,0 +1,27 @@ +# [M] A close racing a refused one-crossing drop + +## Goal + +In Go and Kotlin, a `close()` that meets another thread's one-crossing drop of a checked +object reports that drop's outcome: it returns once the handle is closed, and fails as +borrowed once Rust refused the drop and the handle reopened. A `Drop` whose callback closes +the same object, on its own thread or on one it then joins, still returns at once. + +## Plan + +Found in review of [#906](https://github.com/kixel-ai/rust2/pull/906). Since +[#838](https://github.com/kixel-ai/rust2/pull/838) (Go) and #906 (Kotlin), a checked close no +call holds claims the handle `CLOSED | CLOSING`, then drops and frees it in one crossing, +restoring it open if Rust refuses the drop under a borrow of its own, such as a lent +`Draft`. A second close meanwhile returns at once, though the first may yet be refused. +Waiting it out instead, as #906 first did, hangs when the `Drop`'s callback hands the +close to another thread and joins it, since the deciding close cannot finish meanwhile. + +The second close can wait only until Rust commits the drop: Rust refuses before it takes +the value, and runs any callback only after. Recommended: the glue tells the binding when +it commits, before the destructor runs, such as by clearing `CLOSING` in the handle's state +through a JNI field write in Kotlin and a store through a pointer in Go; waiting closers +then return once it clears. That splits `rt::Object::close` into its check and commit and +the destructor. Measure the object-return row before and after, since the one crossing +was the point. Model a refused drop against a second close, and a drop handing its close +to a joined thread, in `ffi/tests/hold.rs`. diff --git a/quest/m1/ffi-speed/go-close-race.md b/quest/m1/ffi-speed/go-close-race.md deleted file mode 100644 index 941610319..000000000 --- a/quest/m1/ffi-speed/go-close-race.md +++ /dev/null @@ -1,21 +0,0 @@ -# [S] Go's Close waits out a one-crossing drop - -## Goal - -A Go `Close()` that meets another goroutine's one-crossing drop of a checked object returns -only once that drop's outcome stands: nil once the handle is closed, `rt.ErrBorrowed` once -Rust refused the drop and the handle reopened. A `Drop` that calls back into Go and closes -the same object still returns at once. - -## Plan - -Found in review of [#906](https://github.com/kixel-ai/rust2/pull/906), which fixed the same -gap in Kotlin. In `ffi/runtime/rt.go`, `Handle_.Close`'s slow path returns nil when `Hold` -finds the handle closed, though the closed bit may be the `closed_|closing_` claim of a drop -Rust then refuses, which restores `held_`. - -Kotlin tells the drop's own callback from another thread by the closing thread. Go has no -goroutine identity, so the callback needs another mark: such as a flag the cgo callback -entry sets for its goroutine's duration, or the drop's glue passing the handle being dropped -to callbacks it runs. Model it as `checked_closes_refused` in `ffi/tests/hold.rs` models -Kotlin's. From 1f4f778ece8e15b86c1a99270758e4104f7654c7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 10:25:41 -0800 Subject: [PATCH 7/8] Record #906's pick answers in its follow-ups Co-Authored-By: Claude Opus 5.5 --- quest/m1/ffi-proofs/loom-hold.md | 2 +- quest/m1/ffi-speed/close-race.md | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/quest/m1/ffi-proofs/loom-hold.md b/quest/m1/ffi-proofs/loom-hold.md index 12667d02f..f4a3138d0 100644 --- a/quest/m1/ffi-proofs/loom-hold.md +++ b/quest/m1/ffi-proofs/loom-hold.md @@ -8,7 +8,7 @@ mutant that frees a handle a call holds there. ## Plan -Found in [#906](https://github.com/kixel-ai/rust2/pull/906): loom 0.7.3 never ran another +Found in [#906](https://github.com/kixel-ai/rust2/pull/906), whose pick kept this quest: loom 0.7.3 never ran another thread's load between a thread's `load` then `compare_exchange` and its later `fetch_sub`. A probe, one thread doing `load`, `compare_exchange(n, n + 8)`, `fetch_sub(8)` against another's `load`, passed `assert_eq!(n, 8)` in every execution, while the same probe without diff --git a/quest/m1/ffi-speed/close-race.md b/quest/m1/ffi-speed/close-race.md index 7628f9285..1426b5c41 100644 --- a/quest/m1/ffi-speed/close-race.md +++ b/quest/m1/ffi-speed/close-race.md @@ -9,7 +9,8 @@ the same object, on its own thread or on one it then joins, still returns at onc ## Plan -Found in review of [#906](https://github.com/kixel-ai/rust2/pull/906). Since +Found in review of [#906](https://github.com/kixel-ai/rust2/pull/906), whose pick kept this +quest and accepted that PR's close returning at once, as Go's does, until this one lands. Since [#838](https://github.com/kixel-ai/rust2/pull/838) (Go) and #906 (Kotlin), a checked close no call holds claims the handle `CLOSED | CLOSING`, then drops and frees it in one crossing, restoring it open if Rust refuses the drop under a borrow of its own, such as a lent From 1d5a666b396494d8d23db682c4fcf1b7fac4937b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 18:15:52 -0800 Subject: [PATCH 8/8] chore(quest): move loom-hold to a top-level m1 quest, its epic being finished Co-Authored-By: Claude Opus 5.5 --- quest/m1/{ffi-proofs/loom-hold.md => ffi-loom-hold.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename quest/m1/{ffi-proofs/loom-hold.md => ffi-loom-hold.md} (100%) diff --git a/quest/m1/ffi-proofs/loom-hold.md b/quest/m1/ffi-loom-hold.md similarity index 100% rename from quest/m1/ffi-proofs/loom-hold.md rename to quest/m1/ffi-loom-hold.md