diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 6104f1194..6a875d62e 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -621,8 +621,12 @@ 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-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) 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 8240ec21b..419955252 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -674,7 +674,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, 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.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..33855b5f4 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) } @@ -563,20 +563,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) { - val slot = try { - hold() - } catch (_: IllegalStateException) { - return - } try { - drop.close(raw) + return closeChecked(drop) } finally { - release(slot) + __Keep.fence(owner) } - return abandon() } while (true) { val n = state @@ -600,6 +597,37 @@ 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 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)) { + 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) + } finally { + release(slot) + } + abandon() + } + // Whether a call holds it in the table. private fun listed(): Boolean { if (state and BIAS == 0L) return false @@ -628,7 +656,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 +861,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 daf6aa745..0250edc95 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -2196,18 +2196,18 @@ 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::{u}_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::{u}_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::{u}_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(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/api.kt b/ffi/tests/api.kt index d5839fc83..f252b9bf1 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -943,6 +943,43 @@ 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") + // 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(platform() is Platform.Native && 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..33b6e0788 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,44 @@ 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 +342,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-loom-hold.md b/quest/m1/ffi-loom-hold.md new file mode 100644 index 000000000..f4a3138d0 --- /dev/null +++ b/quest/m1/ffi-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), 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 +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/close-race.md b/quest/m1/ffi-speed/close-race.md new file mode 100644 index 000000000..1426b5c41 --- /dev/null +++ b/quest/m1/ffi-speed/close-race.md @@ -0,0 +1,28 @@ +# [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), 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 +`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/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.