Skip to content

fix(core): purge JS event listeners when source webview is destroyed (fix #15583)#15604

Open
tenderdeve wants to merge 4 commits into
tauri-apps:devfrom
tenderdeve:fix/15583-purge-js-listeners-on-webview-close
Open

fix(core): purge JS event listeners when source webview is destroyed (fix #15583)#15604
tenderdeve wants to merge 4 commits into
tauri-apps:devfrom
tenderdeve:fix/15583-purge-js-listeners-on-webview-close

Conversation

@tenderdeve

Copy link
Copy Markdown

Closes #15583.

JS event listeners registered from a webview are stored in Listeners::js_event_listeners, keyed by the source webview label. When the webview is destroyed the manager removes its handle from the store but leaves the listener entries behind. Since the webview's JavaScript runtime is gone, those callbacks can never be delivered — the entries just leak until the app exits, and apps are forced to call unlisten() from onCloseRequested before closing a window just to keep the backend map clean.

Add Listeners::remove_webview_events(label) and call it from on_webview_close and on_window_close (for each webview of the closing window), so a destroyed webview's JS listeners are dropped alongside its handle.

Added a unit test (js_listeners_removed_on_webview_close) covering register → close → gone.

Note: @Legend-Master pointed out the Rust-side handlers map may leak similarly. That one is keyed by EventTarget rather than the owning webview, so purging it correctly has to account for Window vs Webview targets and multi-webview windows. Left out of this PR to keep it focused on the reported JS-listener leak — happy to follow up if you'd like it handled here.

Tested: cargo test -p tauri --lib event::listener (5 passing), fmt + clippy clean.

…ix tauri-apps#15583)

JS event listeners registered from a webview are stored in the backend
Listeners::js_event_listeners map keyed by the source webview label. When
the webview was destroyed the manager removed its handle but left the
listener entries behind. Their JS runtime no longer exists, so the
callbacks can never be delivered and the metadata leaked until app exit,
forcing apps to manually unlisten() before closing a window.

Add Listeners::remove_webview_events and call it from on_webview_close and
on_window_close so a destroyed webview's listeners are dropped with it.
@tenderdeve tenderdeve requested a review from a team as a code owner June 29, 2026 10:07
@github-actions

github-actions Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Package Changes Through 0947c2f

There are 14 changes which include tauri-bundler with minor, tauri-cli with minor, @tauri-apps/cli with minor, tauri-utils with minor, tauri with minor, tauri-build with minor, tauri-macos-sign with minor, tauri-runtime-wry with minor, tauri-runtime with minor, tauri-codegen with minor, tauri-macros with minor, tauri-plugin with minor, tauri-driver with minor, @tauri-apps/api with minor

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
@tauri-apps/api 2.11.1 2.12.0
tauri-utils 2.9.3 2.10.0
tauri-macos-sign 2.3.4 2.4.0
tauri-bundler 2.9.4 2.10.0
tauri-runtime 2.11.3 2.12.0
tauri-runtime-wry 2.11.4 2.12.0
tauri-codegen 2.6.3 2.7.0
tauri-macros 2.6.3 2.7.0
tauri-plugin 2.6.3 2.7.0
tauri-build 2.6.3 2.7.0
tauri 2.11.5 2.12.0
@tauri-apps/cli 2.11.4 2.12.0
tauri-cli 2.11.4 2.12.0
tauri-driver 2.0.6 2.1.0

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@Legend-Master Legend-Master left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good otherwise

Comment thread crates/tauri/src/event/listener.rs Outdated
…teners

Clarify that this only purges JS-side listeners, matching the
js_event_listeners map it operates on.

Addresses @Legend-Master's review feedback.
@tenderdeve

Copy link
Copy Markdown
Author

@Legend-Master updated in fdf05c0 — renamed remove_webview_eventsremove_webview_js_listeners across all call sites.

@wyhaya

wyhaya commented Jul 3, 2026

Copy link
Copy Markdown

Can this RP be merged?

Legend-Master
Legend-Master previously approved these changes Jul 3, 2026

@Legend-Master Legend-Master left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks.

You'll need to run cargo fmt and sign your commits for me to merge this

@Legend-Master Legend-Master added the status: waiting Waiting on author label Jul 5, 2026
@tenderdeve

Copy link
Copy Markdown
Author

@Legend-Master the approval got dismissed when i pushed a small rustfmt fixup (the rename pushed one call site over the line limit, fmt just split it - 0947c2f). ci's green again, mind re-approving when you get a sec?

@wyhaya should be good to merge once it's re-approved 👍

@Legend-Master

Copy link
Copy Markdown
Contributor

and sign your commits for me to merge this

Just so you know there's 2 requests here from the last review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting Waiting on author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] JS event listeners are not removed when the source Webview is destroyed

3 participants