Skip to content

fix(api): return awaitable cleanup from combined event listeners - #16195

Merged
amrbashir merged 1 commit into
tauri-apps:devfrom
alessandrorodi:fix/api-composite-unlisten
Oct 7, 2026
Merged

amrbashir merged 1 commit into
tauri-apps:devfrom
alessandrorodi:fix/api-composite-unlisten

Conversation

@alessandrorodi

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix.

The combined cleanup functions returned by Webview.onDragDropEvent, Window.onDragDropEvent and Window.onFocusChanged in @tauri-apps/api called each inner unlisten function but discarded the promises they return. The cleanup returned undefined, so a rejecting inner unlisten (e.g. the listener was already removed, or the plugin:event|unlisten invoke failed) surfaced as an unhandled promise rejection — commonly on React unmount, when cleanup races listener registration or teardown.

What is the current behavior?

return () => {
  unlistenDragEnter()
  unlistenDragDrop()
  unlistenDragOver()
  unlistenDragLeave()
}

Callers have nothing to await or .catch(). See #16194.

What is the new behavior?

Each combined cleanup is now async and awaits all inner unlistens:

return async () => {
  await Promise.all([
    unlistenDragEnter(),
    unlistenDragDrop(),
    unlistenDragOver(),
    unlistenDragLeave()
  ])
}

Affected APIs:

  • Webview.onDragDropEvent (packages/api/src/webview.ts) — 4 listeners
  • Window.onDragDropEvent (packages/api/src/window.ts) — 4 listeners
  • Window.onFocusChanged (packages/api/src/window.ts) — 2 listeners

A grep of packages/api/src for other return () => { unlistenX(); ... } combined cleanups found no other instances.

Backwards compatibility

UnlistenFn is typed () => void, so an async function stays assignable and the public type does not change. Callers that ignore the result behave exactly as before; callers that await or .catch() the result can now handle failures. All inner unlistens still start synchronously, same as today.

Note: Promise.allSettled plus rethrowing the first error is an alternative if maintainers prefer that every failure be observed.

Separately, a rejected intermediate await this.listen(...) inside these helpers leaks the listeners registered before it. Flagging that as a possible follow-up; it is not fixed here.

Verification

  • pnpm install --frozen-lockfile
  • pnpm -C packages/api build
  • pnpm -C packages/api run ts:check / eslint, and repo prettier --check on the changed files

(all passing — see PR checks / discussion; change file added in .changes/fix-api-composite-unlisten.md)

Closes #16194

@alessandrorodi
alessandrorodi requested a review from a team as a code owner October 2, 2026 02:53
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Package Changes Through a3f2e49

There are 1 changes which include @tauri-apps/api with patch

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.12.1 2.12.2

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


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

@amrbashir amrbashir 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

@amrbashir
amrbashir merged commit a916205 into tauri-apps:dev Oct 7, 2026
23 checks passed
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] Combined unlisten from onDragDropEvent / onFocusChanged discards inner promises, causing unhandled rejections

3 participants