Skip to content

Fix wasm null JS event handlers before release#3432

Merged
JoTurk merged 7 commits into
pion:mainfrom
alinz:alinz/fix-wasm
Jun 12, 2026
Merged

Fix wasm null JS event handlers before release#3432
JoTurk merged 7 commits into
pion:mainfrom
alinz:alinz/fix-wasm

Conversation

@alinz

@alinz alinz commented May 26, 2026

Copy link
Copy Markdown
Contributor

I ran into a frustrating bug when using pion/webrtc in a WASM app:
after closing a peer connection, every subsequent operation would
blow up with syscall/js: call to released function in the browser
console. Took a while to track down.

Comment thread datachannel_js.go Outdated
Comment on lines 191 to 198
if d.onCloseHandler != nil {
d.underlying.Set("onclose", js.Null())
d.onCloseHandler.Release()
}
if d.onClosingHandler != nil {
d.underlying.Set("onclosing", js.Null())
d.onClosingHandler.Release()
}

@JoTurk JoTurk May 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hello, clearing onClose and onError this early will prevent us from receiving the close event later. We'll need to fix this before this is merged, thank you.

@JoTurk

JoTurk commented May 26, 2026

Copy link
Copy Markdown
Member

Seems like the CI is stuck for some reason, That's weird.

@JoTurk

JoTurk commented May 26, 2026

Copy link
Copy Markdown
Member

It's also stuck for other PRs pion/mdns#275 seems like a github issue...

@codecov

codecov Bot commented May 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.47%. Comparing base (ec1dd73) to head (6ce5296).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3432      +/-   ##
==========================================
- Coverage   85.50%   85.47%   -0.04%     
==========================================
  Files          81       81              
  Lines        9857     9857              
==========================================
- Hits         8428     8425       -3     
- Misses       1010     1012       +2     
- Partials      419      420       +1     
Flag Coverage Δ
go 85.47% <ø> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 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.

@alinz alinz changed the title fix(wasm): null JS event handlers before Release() to prevent 'call t… fix(wasm): null JS event handlers before Release() May 26, 2026
@alinz alinz requested a review from JoTurk May 26, 2026 16:54
@alinz alinz marked this pull request as draft May 26, 2026 22:01
@alinz alinz marked this pull request as ready for review May 26, 2026 22:01
@alinz alinz changed the title fix(wasm): null JS event handlers before Release() fix(wasm): null JS event handlers before Release May 26, 2026
Comment thread datachannel_js.go Outdated
Comment on lines 187 to 189
if d.onCloseHandler != nil {
d.onCloseHandler.Release()
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should defer nullifying and releasing onclose/onerror after close is triggered,.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback, It got a little bit more complicated than I thought, but please have a look at my implementation here

@alinz alinz requested a review from JoTurk May 27, 2026 14:29
Comment thread datachannel_js.go Outdated
Comment on lines +217 to +235
wrapperHandler = js.FuncOf(func(this js.Value, args []js.Value) any {
if closeFunc != nil {
go closeFunc()
}
d.underlying.Set("onclose", js.Null())
wrapperHandler.Release()
if oldErrorHandler != nil {
d.underlying.Set("onerror", js.Null())
oldErrorHandler.Release()
}
return js.Undefined()
})
d.underlying.Set("onclose", wrapperHandler)
if oldCloseHandler != nil {
oldCloseHandler.Release()
}
d.onCloseHandler = nil
d.onCloseFunc = nil
d.onErrorHandler = nil

@JoTurk JoTurk May 28, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think before we merge this we just need to make sure we don't override the previous close if the user calls close twice or more for whatever reason.

Thank you.

@alinz alinz changed the title fix(wasm): null JS event handlers before Release Fix(wasm): null JS event handlers before release May 28, 2026
@alinz alinz requested a review from JoTurk May 28, 2026 11:17

@JoTurk JoTurk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you and sorry for the back and forth.

Comment thread datachannel_js.go
// close() and the onclose event), and the user-provided OnClose callback
// must still be invoked. Install the wrapper only once so repeated Close()
// calls don't override the original wrapper before it fires.
if !d.closeWrapperInstalled {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: we need to avoid installing the wrapper if already closed., but we can do this later. I think.

@alinz alinz changed the title Fix(wasm): null JS event handlers before release fix(wasm): Null JS event handlers before release Jun 5, 2026
@alinz alinz changed the title fix(wasm): Null JS event handlers before release Fix wasm null JS event handlers before release Jun 5, 2026
@alinz

alinz commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

My apologies @JoTurk , but the Lint Meta data keeps failing, and I don't have any clue how to solve this.

@JoTurk

JoTurk commented Jun 12, 2026

Copy link
Copy Markdown
Member

My apologies @JoTurk , but the Lint Meta data keeps failing, and I don't have any clue how to solve this.

Thank you for your fixes. the CI is failing because we have lint rules for commit messages, but it doesn't matter I'm going to squash merge it.

Thank you again.

@JoTurk JoTurk merged commit 3a57b45 into pion:main Jun 12, 2026
28 of 32 checks passed
@alinz alinz deleted the alinz/fix-wasm branch June 12, 2026 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants