Skip to content

Reapply "Add initial Rust JSG integration with error support"#5645

Merged
anonrig merged 2 commits intomainfrom
yagiz/introduce-jsg-rust
Dec 11, 2025
Merged

Reapply "Add initial Rust JSG integration with error support"#5645
anonrig merged 2 commits intomainfrom
yagiz/introduce-jsg-rust

Conversation

@anonrig
Copy link
Copy Markdown
Member

@anonrig anonrig commented Dec 4, 2025

This reverts commit 66f5143.


Replaces the ownership mechanism triggering LSAN leak error. Added a test to ensure we don't regress in the future.

Realm object (a Rust-created structure) is stored in v8's embedder data via context_set_realm(). When LSAN run leak checks, it cannot trace through v8's internal embedder data to find references to this memory. From LSAN's perspective, the Realm looks like a leaked allocation because:

  1. The realm* pointer is stored inside v8's opaque embeder data
  2. LSAN's memory scanner doesn't know how to trace v8's internal structure
  3. The actual memory is still reachable and will be properly freed in disposeContext() but LSAN can't verify this.

This false memory leak was easily reproducible through a static TestFixture (mimicking fuzzer pattern) some random work followed by calling __lsan_do_recovarable_leak_check.

To solve this, we either had to ignore the object from LSAN or stored the owned reference in Isolate::Impl and store the reference in the context object using embedder data.

One commit fixes the memory leak and reproduces it, and the last commit converts the Realm data to be owned by the Isolate::Impl

@anonrig anonrig requested review from a team as code owners December 4, 2025 21:25
@anonrig anonrig force-pushed the yagiz/introduce-jsg-rust branch 4 times, most recently from 2637a14 to 9839191 Compare December 5, 2025 18:42
@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 5, 2025

The generated output of @cloudflare/workers-types matches the snapshot in types/generated-snapshot 🎉

@anonrig anonrig force-pushed the yagiz/introduce-jsg-rust branch 2 times, most recently from 4c119db to b6bc021 Compare December 5, 2025 19:50
@anonrig anonrig changed the title [DO NOT MERGE] Reapply "Add initial Rust JSG integration with error support" Reapply "Add initial Rust JSG integration with error support" Dec 5, 2025
@anonrig anonrig requested review from guybedford and jasnell December 5, 2025 21:15
@anonrig anonrig force-pushed the yagiz/introduce-jsg-rust branch from b6bc021 to e43bc5c Compare December 5, 2025 21:16
@anonrig anonrig requested a review from mikea December 5, 2025 21:20
@anonrig anonrig force-pushed the yagiz/introduce-jsg-rust branch from e43bc5c to 7935905 Compare December 5, 2025 21:23
@jasnell
Copy link
Copy Markdown
Collaborator

jasnell commented Dec 5, 2025

Can you expand the PR description a bit more to explain what the issue being fixed is. Without it, it's a bit difficult to review the PR to determine if it and the test adequately address the issue.

@anonrig
Copy link
Copy Markdown
Member Author

anonrig commented Dec 10, 2025

Can you expand the PR description a bit more to explain what the issue being fixed is. Without it, it's a bit difficult to review the PR to determine if it and the test adequately address the issue.

Added a description to the pull-request @jasnell. Let me know if you have any questions.

@anonrig anonrig requested a review from jasnell December 10, 2025 02:43
@anonrig anonrig force-pushed the yagiz/introduce-jsg-rust branch from 7962941 to fc684e3 Compare December 10, 2025 02:45
Copy link
Copy Markdown
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

Just to understand the mental model properly here - how does the realm get disposed for normal isolate disposal from C++?

@anonrig
Copy link
Copy Markdown
Member Author

anonrig commented Dec 10, 2025

Just to understand the mental model properly here - how does the realm get disposed for normal isolate disposal from C++?

The destructor of Isolate::Impl which also owns the isolate destroys it.

@anonrig anonrig force-pushed the yagiz/introduce-jsg-rust branch from fc684e3 to 783a83a Compare December 10, 2025 15:08
@anonrig anonrig requested a review from danlapid December 10, 2025 15:09
@anonrig anonrig force-pushed the yagiz/introduce-jsg-rust branch 2 times, most recently from 183d6a9 to 5661b0e Compare December 10, 2025 15:58
@anonrig
Copy link
Copy Markdown
Member Author

anonrig commented Dec 10, 2025

hey @jasnell @guybedford would you mind reviewing this pull request?

@jasnell
Copy link
Copy Markdown
Collaborator

jasnell commented Dec 10, 2025

I'll let @guybedford and @mikea do the necessary sign off on this. Nothing problematic was apparent on an initial review, however.

@guybedford
Copy link
Copy Markdown
Contributor

guybedford commented Dec 11, 2025

The destructor of Isolate::Impl which also owns the isolate destroys it.

Yes this was clear from the change, but what is not clear to me is how Rust is informed about Realm disposal, and how that happens?

Specifically, when you remove the current disposal function here, what is it that calls that now? Or does Rust not need to be informed?

Copy link
Copy Markdown
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

Talked this through with Yagiz and was able to get a better picture, the main question I still had after the discussion was just how we know we definitely have a lock for Rust's realm disposal drop call, and he had some good checks for that as well.

@anonrig anonrig force-pushed the yagiz/introduce-jsg-rust branch from 5661b0e to 3c55736 Compare December 11, 2025 20:43
@anonrig anonrig force-pushed the yagiz/introduce-jsg-rust branch from 3c55736 to 693b5e7 Compare December 11, 2025 20:43
@anonrig
Copy link
Copy Markdown
Member Author

anonrig commented Dec 11, 2025

Upon recommendation from @guybedford, I've added a debug assertion to Realm::drop

@anonrig anonrig merged commit 73800bc into main Dec 11, 2025
21 of 23 checks passed
@anonrig anonrig deleted the yagiz/introduce-jsg-rust branch December 11, 2025 21:43
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.

5 participants