Skip to content

Fix JSClosure leak - #240

Merged
kateinoigakukun merged 4 commits into
mainfrom
katei/fix-closure-leak
Apr 12, 2024
Merged

Fix JSClosure leak#240
kateinoigakukun merged 4 commits into
mainfrom
katei/fix-closure-leak

Conversation

@kateinoigakukun

Copy link
Copy Markdown
Member
letc1=JSClosure{ _ in.undefined }
consume c1

did not release the JSClosure itself and it leaked the underlying
JavaScript closure too because JSClosure -> JS closure thunk ->
Closure registry entry -> JSClosure reference cycle was not broken
when using FinalizationRegistry. (Without FR, it was broken by manual
release call.)

Note that weakening the reference does not violates the contract that
function reference should be unique because holding a weak reference does
deinit but not deallocate the object, so ObjectIdentifier is not reused
until the weak reference in the registry is removed.

```swift
let c1 = JSClosure { _ in .undefined }
consume c1
```
did not release the `JSClosure` itself and it leaked the underlying
JavaScript closure too because `JSClosure` -> JS closure thunk ->
Closure registry entry -> `JSClosure` reference cycle was not broken
when using FinalizationRegistry. (Without FR, it was broken by manual
`release` call.)
Note that weakening the reference does not violates the contract that
function reference should be unique because holding a weak reference does
deinit but not deallocate the object, so ObjectIdentifier is not reused
until the weak reference in the registry is removed.
The test suite was not properly releasing the closures but they have not
been revealed as a problem because those closures were leaked
conservatively.
…tract
This additional information will help developers to find the root cause
@github-actions

github-actionsBot commented Apr 7, 2024

Copy link
Copy Markdown

Time Change: +325ms (3%)

Total Time: 9,697ms

Test nameDurationChange
Serialization/JavaScript function call through Wasm import23ms+2ms (6%)🔍
Serialization/JavaScript function call through Wasm import with int16ms+2ms (9%)🔍
Serialization/JavaScript function call from Swift103ms+7ms (7%)🔍
Serialization/JavaScript Number to Swift Int331ms+32ms (9%)🔍
View Unchanged
Test nameDurationChange
Serialization/Swift Int to JavaScript with assignment334ms+17ms (4%)
Serialization/Swift Int to JavaScript with call993ms+32ms (3%)
Serialization/Swift String to JavaScript with assignment391ms+8ms (2%)
Serialization/Swift String to JavaScript with call1,048ms+30ms (2%)
Serialization/JavaScript String to Swift String3,821ms+158ms (4%)
Object heap/Increment and decrement RC2,625ms+37ms (1%)
View Baselines
Test nameDuration
Serialization/Call JavaScript function directly3ms
Serialization/Assign JavaScript number directly3ms
Serialization/Call with JavaScript number directly3ms
Serialization/Write JavaScript string directly3ms
Serialization/Call with JavaScript string directly3ms

@kateinoigakukun
kateinoigakukun requested a review from a teamApril 7, 2024 06:57
@kateinoigakukun

Copy link
Copy Markdown
MemberAuthor

Tested with a random large-scale application and no major regression found

@kateinoigakukun
kateinoigakukun merged commit d9a4a9f into mainApr 12, 2024
@kateinoigakukun
kateinoigakukun deleted the katei/fix-closure-leak branch April 12, 2024 09:02
kateinoigakukun added a commit that referenced this pull request Apr 12, 2024
kateinoigakukun added a commit that referenced this pull request Apr 12, 2024
Sign up for freeto 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

@kateinoigakukun