Skip to content

fix: channel callback never cleaned up from window#13136

Merged
lucasfernog merged 16 commits into
tauri-apps:devfrom
Legend-Master:fix-channel-never-cleanup
Apr 12, 2025
Merged

fix: channel callback never cleaned up from window#13136
lucasfernog merged 16 commits into
tauri-apps:devfrom
Legend-Master:fix-channel-never-cleanup

Conversation

@Legend-Master

@Legend-Master Legend-Master commented Apr 4, 2025

Copy link
Copy Markdown
Contributor

Fix #13133

Also:

  • added a feature that allows passing the callback as the parameter of constructor of Channel so you can use it like this new Channel((message) => console.log(message))
  • only go through the fetch process if the data is large enough for it to be faster

@github-actions

github-actions Bot commented Apr 4, 2025

Copy link
Copy Markdown
Contributor

Package Changes Through ebb919b

There are 8 changes which include @tauri-apps/api with minor, tauri with minor, tauri-bundler with minor, tauri-runtime with minor, tauri-runtime-wry with minor, tauri-utils with minor, @tauri-apps/cli with minor, tauri-cli 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.4.1 2.5.0
tauri-utils 2.3.1 2.4.0
tauri-bundler 2.3.1 2.4.0
tauri-runtime 2.5.1 2.6.0
tauri-runtime-wry 2.5.1 2.6.0
tauri-codegen 2.1.1 2.1.2
tauri-macros 2.1.1 2.1.2
tauri-plugin 2.1.1 2.1.2
tauri-build 2.1.1 2.1.2
tauri 2.4.1 2.5.0
@tauri-apps/cli 2.4.1 2.5.0
tauri-cli 2.4.1 2.5.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 marked this pull request as ready for review April 5, 2025 04:37
@Legend-Master Legend-Master requested a review from a team as a code owner April 5, 2025 04:37
@Legend-Master

Copy link
Copy Markdown
Contributor Author

This change will require syncing the version of rust crate tauri and js package @tauri-apps/api, since this is marked as a minor I think we can do this right?

@FabianLars

Copy link
Copy Markdown
Member

hmm, yeah. we've communicated that minor versions are synced a few times (even if we don't fully follow it) so it should be fine. not the first time in 2.x either 🤷

Comment thread crates/tauri/src/ipc/channel.rs Outdated
"window.__TAURI_INTERNALS__.invoke('{FETCH_CHANNEL_DATA_COMMAND}', null, {{ headers: {{ '{CHANNEL_ID_HEADER_NAME}': '{data_id}' }} }}).then((response) => window['_' + {}]({{ message: response, id: {i} }})).catch(console.error)",
callback_id.0
))?;
// Don't go through the fetch process if the payload is small

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.

finally someone who agrees with me on that approach 😂

@Legend-Master Legend-Master Apr 8, 2025

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.

This should significantly reduce the load in places like sending updater's download progress

@lucasfernog lucasfernog added this to the 2.5 milestone Apr 12, 2025
@lucasfernog

Copy link
Copy Markdown
Member

sample benchmark script:

pub fn spam(channel: Channel<tauri::ipc::InvokeResponseBody>) -> tauri::Result<()> {
  for i in 1..=255 {
    channel.send(tauri::ipc::InvokeResponseBody::Raw(
      std::iter::repeat(i).take(1024).collect(),
    ))?;
  }
  Ok(())
}

(change 1024 to whatever you want to test)

channel = new __TAURI__.core.Channel();
i = 0;
channel.onmessage = (m) => {console.log(m); i++; if (i === 255) { console.timeEnd('spam') } };
console.time('spam');
__TAURI__.core.invoke('spam', { channel });

@lucasfernog lucasfernog merged commit 66e6325 into tauri-apps:dev Apr 12, 2025
InvokeResponseBody::Raw(bytes) if bytes.len() < MAX_RAW_DIRECT_EXECUTE_THRESHOLD => {
webview.eval(format!(
"window['_{callback_id}']({{ message: {}, index: {current_index} }})",
serde_json::to_string(&bytes)?,

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.

@Legend-Master Legend-Master Apr 20, 2025

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.

That's pretty much the only part not written by me in this PR (ebb919b) 😅, anyways, I'll take a look tomorrow

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.

Oh, my bad, I didn't notice that 😂

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.

[bug] Listening to Tauri channel events creates memory leak

4 participants