Skip to content

kj-rs: make bridged KJ promise futures !Unpin - #8761

Merged
nathanwhit merged 1 commit into
mainfrom
nathanwhit/kj-rs-promise-future-not-unpin
Nov 24, 2026
Merged

nathanwhit merged 1 commit into
mainfrom
nathanwhit/kj-rs-promise-future-not-unpin

Conversation

@nathanwhit

Copy link
Copy Markdown
Member

Polling the future of a C++ promise constructs the awaiter inside it. That awaiter points to the future's waker slot and is linked into the event loop. Every field of PromiseAwaiter was Unpin, so the future was too, and safe code could poll it with Pin::new() and then move it, leaving those pointers dangling.

PromiseAwaiter now holds a PhantomPinned, so the future is !Unpin, and a compile test checks it. Nothing in the tree relied on it being Unpin.

@nathanwhit
nathanwhit requested review from a team as code owners November 21, 2026 02:39

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 2e25989d.

This review, of 2fba01c6

✅ Approved — A small, correct soundness fix with a compile test that checks it; no issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

PromiseAwaiter now holds a PhantomPinned, so PromiseFuture and the impl Future returned by generated bridge code are !Unpin.

  • Pinned access: goes through unchecked pin APIs, none of which needed Unpin.
  • Callers: no caller in the tree depends on these futures being Unpin.
  • Compile test: it is picked up by the glob in compile-tests/BUILD.bazel and only type-checked. Auto traits leak through opaque types, so the expected E0277 is produced.

📝 Documentation: ✅ LGTM

The new comments describe the current design, which is what the repository's comment guidelines ask for.


Reviewed commit: 2fba01c6

@guybedford guybedford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I checked the compile test fails against main's awaiter.rs, so it does guard the fix.

Comment thread src/rust/cxx/kj-rs/awaiter.rs Resolved
Polling the future of a C++ promise constructs the awaiter inside it,
which points to the future's waker slot and is linked into the event
loop. Every field of `PromiseAwaiter` was `Unpin`, so the future was,
and safe code could poll it with `Pin::new()` and then move it, leaving
those pointers dangling.

`PromiseAwaiter` now holds a `PhantomPinned`, so the future is
`!Unpin`, and a compile test checks it. Nothing in the tree relied on
it being `Unpin`.

The `Unpin` bound on its `Data` and on `KjPromise::Data` is dropped:
`data` is never pin-projected, only read through a shared reference.
@nathanwhit
nathanwhit force-pushed the nathanwhit/kj-rs-promise-future-not-unpin branch from 2fba01c to 2e25989 Compare November 24, 2026 01:39

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — A small, correct soundness fix. Dropping the Data: Unpin bounds is safe because data is never pin-projected, and the compile test still checks that the bridged futures are !Unpin. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

This change tightens memory safety and removes no checks. Because a polled bridged future is now !Unpin, safe code can no longer move it and leave the awaiter's waker pointer and its event-loop link dangling. Removing the Data: Unpin bounds only widens which types can be used for Data, and the awaiter cannot move either way.


📋 Code Quality: ✅ LGTM

The commit since the last review drops the Data: Unpin bounds from PromiseAwaiter and KjPromise::Data, as suggested in the discussion. The change is sound.

  • Pin handling: data is never pin-projected. PromiseFuture::poll reads it through Pin's Deref, which does not need Unpin.
  • Unchecked pin APIs: get_awaiter pins awaiter with Pin::new_unchecked and reaches option_waker through Pin::into_inner_unchecked. Neither depends on Data: Unpin.
  • !Unpin for every Data: PhantomPinned makes PromiseAwaiter<Data> !Unpin whatever Data is. That carries through to PromiseFuture and to the opaque impl Future returned by the generated bridge code.
  • Compile test: promise-future-is-not-unpin.rs still checks this.

📝 Documentation: ✅ LGTM

The new comment on _pinned explains why the struct must not move. It describes the code as it is now, which is what the repository's comment guidelines ask for.


Reviewed commit: 2e25989d

@nathanwhit
nathanwhit merged commit 7698490 into main Nov 24, 2026
36 of 38 checks passed
@nathanwhit
nathanwhit deleted the nathanwhit/kj-rs-promise-future-not-unpin branch November 24, 2026 02:29
danlapid added a commit that referenced this pull request Nov 25, 2026
main's #8831 catches panics in the futures kj-rs hands to C++, as
this branch did, but logs a panic in a future's destructor rather
than aborting. The merge takes main's helpers and policy, keeps this
branch's lent poll waker, and drops this branch's own panic helper,
its panic tests (main's cover the same cases) and the death test
that asserted the abort.

main's #8761 gives PromiseAwaiter the same PhantomPinned field this
branch added; the merge keeps one.

ThreadLogger, new on main, crosses threads in a KjOwn, so it opts
into DisposableOnAnyThread like the other kj::heap() types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants