Skip to content

fix(module): prevent double-free when initializing native module exports#689

Merged
buke merged 2 commits intomainfrom
bugfexed/issues-688
Jan 3, 2026
Merged

fix(module): prevent double-free when initializing native module exports#689
buke merged 2 commits intomainfrom
bugfexed/issues-688

Conversation

@buke
Copy link
Owner

@buke buke commented Jan 3, 2026

QuickJS JS_SetModuleExport takes ownership of the JSValue passed in. We were passing Go-owned values directly, which could later be freed again on the Go side, leading to intermittent ref_count assertion failures during JS_FreeRuntime (issue #688).

After handing an export value to JS_SetModuleExport, invalidate the Go Value so subsequent Free() becomes a no-op. Also release the duplicated module private value and propagate export init failures.

Add a regression stress test that runs the minimal repro scenario 50 times.

QuickJS JS_SetModuleExport takes ownership of the JSValue passed in. We were
passing Go-owned values directly, which could later be freed again on the Go
side, leading to intermittent ref_count assertion failures during JS_FreeRuntime
(issue #688).

After handing an export value to JS_SetModuleExport, invalidate the Go Value so
subsequent Free() becomes a no-op. Also release the duplicated module private
value and propagate export init failures.

Add a regression stress test that runs the minimal repro scenario 50 times.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @buke, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical memory management bug where native module exports could lead to double-free errors and runtime crashes in QuickJS. The changes ensure proper ownership transfer of JavaScript values between Go and QuickJS, preventing resource mismanagement, and include a robust regression test to safeguard against future recurrences of this issue.

Highlights

  • Double-Free Prevention: Modified the handling of JS_SetModuleExport to prevent double-free issues (issue Module builder sometime have reference counting issue #688) by invalidating Go-owned JSValue references after ownership is transferred to QuickJS.
  • Error Propagation: Ensured that module export initialization failures are properly propagated by returning an error code from the goModuleInitProxy function.
  • Resource Management: Added explicit freeing of the module's private value (privateValue) to prevent potential resource leaks.
  • Regression Test: Introduced a new stress test (TestModuleBuilder_RuntimeClosePanic_Minimal) that runs the minimal reproduction scenario 50 times to verify the fix for runtime panics during module cleanup.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codecov
Copy link

codecov bot commented Jan 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (1e076ef) to head (df4f908).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #689   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           11        11           
  Lines         2343      2349    +6     
=========================================
+ Hits          2343      2349    +6     
Files with missing lines Coverage Δ
bridge.go 100.00% <100.00%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1e076ef...df4f908. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses a double-free vulnerability by invalidating the Go-side Value after its ownership is transferred to QuickJS's JS_SetModuleExport. The changes also improve error propagation and add a valuable regression stress test to prevent this issue from recurring. My review includes one critical suggestion to prevent a resource leak on the error path within the module initialization proxy.

Add an error-branch test that forces a mismatch between declared module exports
and init-time exports, making JS_SetModuleExport return < 0 and exercising the
previously uncovered rc<0 path.

Also free the JSValue returned by JS_GetModulePrivateValue in
getContextAndModuleBuilder, and make module init failures throw a JS exception
with a clear error message.
@buke buke merged commit b8d50de into main Jan 3, 2026
8 checks passed
@buke buke deleted the bugfexed/issues-688 branch January 3, 2026 05:35
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.

1 participant