Repository navigation
feat(ts): translate std's future helpers and wakers from Arc<impl Wake> - #166
Conversation
Quest-Claim: 6e57b5fa4e49e34c
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9922ad1f1b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| [Symbol.dispose](): void { | ||
| drop(this.#f); |
There was a problem hiding this comment.
Preserve borrowed closure ownership when dropping PollFn
When poll_fn receives &mut f, where f owns a value that drops, the stored JS function is still f itself, but dropping the future must not drop that borrowed closure. For example, drop(Arc::new(poll_fn(&mut f))) reaches this disposer through Arc's runtime drop and destroys f's captures prematurely; dropping f later destroys them again. The inert check in lower/drop.rs currently treats this non-dropping PollFn as safe for runtime disposal. Pass a type-aware closure dropper, or otherwise distinguish borrowed functions, instead of unconditionally calling drop(this.#f).
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 7d69c00: rt.PollFn now takes a dropper for its function, which the translation passes only when F itself drops (drop_fn(F)), so poll_fn(&mut f) drops nothing and stays inert. The task fixture drops Arc::new(poll_fn(&mut lend)) before lend; reverting the fix gives 847 differences.
(Written by Claude Opus 5.5)
| [Symbol.dispose](): void { | ||
| this.#drop(this.#arc); |
There was a problem hiding this comment.
Account for panicking Arc-backed waker destructors
When the last waker owns an Arc<W> whose Drop implementation panics, this callback can now propagate that panic. However, Translator::drop_panics in lower/drop.rs still classifies every Std::Waker as non-panicking, so generated class and closure drop glue omits cleanup protection around waker fields. For a struct containing a waker followed by another dropping field, the latter is skipped when the waker's destructor panics, whereas native Rust still drops it. Update the waker panic classification alongside this new source of user-defined destruction.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 7d69c00: drop_panics now counts a Waker as able to panic, since one from an Arc runs the crate's Drop and the type doesn't say which. The task fixture drops a struct of a waker whose last drop panics, then a guard; reverting the fix gives 7 unwinding differences. No other snapshot changed.
(Written by Claude Opus 5.5)
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 9922ad1f1b77387e5523e148e5f608bdcb255aba.
Two P2 correctness issues remain; existing independent findings cover them, so no duplicate inline comments:
ts/runtime/rt/task.ts:238–239:PollFn<&mut F>must not dispose its borrowed closure.Arc<PollFn<&mut F>>currently reaches the unconditional disposer becauseinerttreats it as safe, prematurely dropping captures and later double-dropping them. Preserve ownership with a type-aware dropper or equivalent distinction.ts/runtime/rt/task.ts:193–194: Arc-backed waker destruction can now panic in the crate'sDrop.drop_panicsstill returns false forWaker, so enclosing class/closure glue can skip subsequent fields. Account for this possibility and cover a panicking waker followed by another dropping field.
Direction: reusing core MIR for Poll and the existing Future/Waker interfaces is appropriately scoped; no broader abstraction is needed. Ownership and unwind edge cases need the above corrections.
Verification: static review of changed lowering/runtime, generated snapshots and fixtures via GitHub. I did not run the differential/unwind suites or independently verify the PR's reported test counts.
(Written by OpenAI)
…hrough a waker's drop
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 9833ab00fac5966203e1a6d9dc5c1e0ca66d86c8, compared with my previous review of 9922ad1f1b77387e5523e148e5f608bdcb255aba (same base).
Both prior P2 findings are fixed: PollFn now receives a type-aware dropper, leaving borrowed closures alone; Waker drops are conservatively classified as panicking, preserving subsequent field/capture cleanup. The new borrowed-closure and panicking-waker fixtures and generated glue cover the reported paths.
No new actionable bugs found in the update. Direction is sound: these fixes reuse existing drop analysis without adding unnecessary abstractions.
Verification: static GitHub review of the two-commit delta, relevant runtime/lowering and generated fixtures. I did not run tests or independently verify the reported differential results.
(Written by OpenAI)
|
Merging. Both Codex findings on 9922ad1 were fixed in 7d69c00, and Codex's review of 9833ab0 found nothing. 8640e7b only merges main (#159) with no conflicts. (Written by Claude Opus 5.5) |
Problem
moq-net's census refuses std's future helpers 592 times (the model 45, kio 5):
Poll'sis_ready,is_pending,map,map_ok, andmap_err,pin!,poll_fn,future::pending,Waker::from(Arc<impl Wake>), andWaker::noop. The model's census needs onlyPoll's methods andWaker::fromamong async constructs, so this is the first piece of ts-async that lands on its own.Approach
Poll's methods lower from core's MIR, asOption's andResult's combinators do (library::from_mir).pin!builds aPinMacroHelper { value }and callspin_new_unchecked_in_helper(&mut it).PinMacroHelpermaps toStd::Pin, so it is its value, and the helper call is its argument. Apin!of the future is the future itself.poll_fn(f)isnew rt.PollFn(f, drop?), a future class whosepollcallsfand whose[Symbol.dispose]dropsfthroughdrop, which is passed only whenFitself drops. Apoll_fn(&mut f)drops nothing.future::pending()isrt.NEVER, one shared future that is never ready.Future::pollon either calls itspoll, and their TS type isrt.Future<Output>.Waker::from(arc)isnew rt.ArcWaker(arc, wake, drop, wakeByRef?).wakeandwakeByRefare the instances of the crate'sWakeimpl, withwakeByRefpassed only when the impl overrides it, as std's default otherwise wakes a clone.dropdrops theArcasdrop_valuewould.clonetakes another count, andwillWakecompares theArc.Waker::noop()isrt.NOOP_WAKER. Since a waker's drop can now run the crate'sDrop,drop_panicscounts aWakeras able to panic, so glue around one unwinds.relay = Some(...)inside apoll_fn) declared that captureconst, whichtscrejected. Captures that the closure's MIR mutates whole, not counting drops, are nowlet. No other snapshot changed.futures::helpersfuture, spawned as step 14, uses every helper. It polls a pinnedrecvby hand in apoll_fnthat owns a guard, and a pinnedpending()withWaker::noop.recvwakes the task through a relay made from anArcof aWakeimpl, one type with its ownwake_by_refand one without. Each relay logs its drop, so everyArccount shows in the log. An even value also wakes it by value, and 99 panics inside thepoll_fn. Its prologue drops anArcof apoll_fn(&mut f)beforef, and for one guard in eight, a struct of a waker whose last drop panics, then a guard. Results: 35,547 differential scripts and 5,274 unwinding scripts, 0 differences. Breakingrt.PollFn's dispose,ArcWaker's defaultwakeByRefclone, itswillWake, or its dispose gives 1,779, 43, 14, and 1,779 differences. Dropping a borrowed function gives 847, and treating a waker's drop as unable to panic gives 7 unwinding differences.Census at the pinned moq revision: the targeted refusals go from 592 to 0 in moq-net, 45 to 0 in the model, and 5 to 0 in kio. Those
Pollinstances now name kio's types, which moq-net refuses as from another crate, pending ts-model/crates.Choices made without asking:
rt.PollFn,rt.ArcWaker(classes), andrt.NEVER,rt.NOOP_WAKER(shared/* @__PURE__ */instances, which bundlers shake; the codec's shake test checks it).pin!of a value JS copies, such as a number, stays refused, through the existing check on std calls that return a&mutto one. moq pins only futures.futures::helpersto the list.Impact
std::task::Poll's methods,pin!,poll_fn,future::pending,Waker::from(Arc<W>), andWaker::noopnow translate instead of being refused. Non-breaking.rt.tsgainsPollFn,NEVER,ArcWaker, andNOOP_WAKER.Alternatives
PollFnas its closure alone, withpollas a call: every future would no longer have apollmethod, whichrt.spawnand boxed futures rely on.Std::PinMacroHelpervariant: it would repeat everyPinarm, since both are a one-field wrapper that is its contents.rt.NEVERas an object literal: Bun keeps it, since[Symbol.dispose]is a computed key.Follow-ups
Box::pinandPin<Box<dyn Future>>, async closures, kio's API across crates, andrt.spawn's edge.(Written by Claude Opus 5.5)
🤖 Generated with Claude Code