Skip to content

Closing the receiver tab will not throw an error in tabs.sendMessage() #365

@fregante

Description

@fregante

Related:

From my understanding, the code above was added to silence the error Chrome throws when a message is received by the listener but not "handled" (i.e. the listener did not return a promise).

For Chrome, this is a user error: The user must call sendResponse, even without arguments.

Unfortunately the polyfill’s workaround means that I can't distinguish between:

  • the tab never handled my request (if there's any listener registered)
  • the tab closed before my request was answered

… because in both cases, the promise will resolve with undefined.

Since the solution to this problem is simple, I suggest surfacing this error instead of ignoring it. The solution for users is:

// use an async listener
browser.runtime.onMessage.addListener(async (message) => {
	// Handled, done
});

// return anything (or better, a resolved promise)
browser.runtime.onMessage.addListener((message) => {
	return Promise.resolve();
});

I understand however that this would be a breaking change, but the current situation means I can't use this polyfill to send long-running messages (e.g. send message, wait for HTTP response(2))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions