Skip to content

Destroy a KjMaybe's value only when the KjMaybe is dropped - #8453

Merged
guybedford merged 1 commit into
mainfrom
gbedford/kj-maybe-destroy-once
Nov 13, 2026
Merged

guybedford merged 1 commit into
mainfrom
gbedford/kj-maybe-destroy-once

Conversation

@guybedford

Copy link
Copy Markdown
Member

This fixes kj_rs::maybe::MaybeItem::drop_in_place, a safe public method that destroyed the value in a KjMaybe and left the KjMaybe saying it still held one.

KjMaybe<T> mirrors kj::Maybe<T>: a flag (or a niche) plus storage. Drop for KjMaybe called T::drop_in_place(self), a default method on the public MaybeItem trait, which drops the value when is_some() is true and changes nothing else. Since the method was callable from anywhere, calling it and then letting the KjMaybe go out of scope dropped the same String or KjOwn twice. Nothing in workerd calls it other than Drop, and the bridge macro's generated MaybeItem impls don't override it.

  • MaybeItem::drop_in_place is gone; Drop for KjMaybe does the same work inline.

There was no compile-test target for kj-rs, so this adds //src/rust/cxx/kj-rs/compile-tests (the same wd_rust_compile_test harness as jsg-compile-tests) with one snippet checking that the method can no longer be named. maybe-test.rs adds unit tests for what is left: a KjMaybe<String> that holds a heap value is dropped, an empty one is dropped, and converting to Option moves the value out. Under --config=asan the old method followed by a drop reports a double free; the default test binaries use tcmalloc, which does not notice.

Drop for KjMaybe called MaybeItem::drop_in_place, a safe default method
of the public MaybeItem trait. It drops the stored value when the
KjMaybe holds one and leaves the flag, or the niche, saying that it
still does, so calling it and then dropping the KjMaybe dropped the
value twice.

Remove the method and drop the value in Drop for KjMaybe itself. Nothing
else called it, and generated MaybeItem impls do not override it.
@guybedford
guybedford requested review from a team as code owners November 13, 2026 20:18

@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 — This is a small, correct soundness fix: Drop does what the removed method did, inline, and it comes with a compile test and unit tests. 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

The inline Drop matches the old behavior exactly. The method is removed from the public trait, and the compile test makes sure it stays gone. The unit tests cover dropping a full KjMaybe, dropping an empty one, and converting to Option.


📝 Documentation: ✅ LGTM

The comments describe the code as it is now and follow the repository's comment guidelines.


Reviewed commit: 5c65697c

@guybedford
guybedford merged commit ef4aed2 into main Nov 13, 2026
23 of 26 checks passed
@guybedford
guybedford deleted the gbedford/kj-maybe-destroy-once branch November 13, 2026 22:23
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