{{ message }}
webassembly: Fix GC tracing of object returned from top-level functions.#19141
Open
dpgeorge wants to merge 1 commit intomicropython:masterfrom
Open
webassembly: Fix GC tracing of object returned from top-level functions.#19141dpgeorge wants to merge 1 commit intomicropython:masterfrom
dpgeorge wants to merge 1 commit intomicropython:masterfrom
Conversation
This commit makes sure that the GC explicitly traces the Python object returned from all top-level functions. Without this, it's possible that a JsProxy that is created as the return object is freed on the Python side (and hence the underlying JavaScript object entry set to `undefined`) just prior to the top-level C function returning. So when it does return and control resumes on the JavaScript side, the JavaScript object corresponding to the JsProxy is gone. For example, the case covered by the test added here has the return value (Python object) from `proxy_c_to_js_call()` reclaimed by the Python GC via `proxy_js_free_obj()` before it can be accessed from the JavaScript side, and so the JavaScript reference in `proxy_js_ref` is `undefined`, Signed-off-by: Damien George <damien@micropython.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
This commit makes sure that the GC explicitly traces the Python object returned from all top-level functions. Without this, it's possible that a JsProxy that is created as the return object is freed on the Python side (and hence the underlying JavaScript object entry set to
undefined) just prior to the top-level C function returning. So when it does return and control resumes on the JavaScript side, the JavaScript object corresponding to the JsProxy is gone.For example, the case covered by the test added here has the return value (Python object) from
proxy_c_to_js_call()reclaimed by the Python GC viaproxy_js_free_obj()before it can be accessed from the JavaScript side, and so the JavaScript reference inproxy_js_refisundefined,Testing
I found this in a web app I'm building, there was some strange behaviour which was traced to the issue fixed here. That web app now behaves correctly with this fix.
Also added a test which triggers the same problem. It'll run in CI.
Trade-offs and Alternatives
There may be other ways to structure the fix, but PR #18021 already fixed a related case, and this PR is extending/improving upon that fix.
Generative AI
I did not use generative AI tools when creating this PR.