Open Bug 1987397 Opened 6 months ago Updated 6 months ago

browser.tabs.query still has removed tab when tabs.onRemoved fires

Categories

(WebExtensions :: General, defect, P3)

Firefox 142
defect

Tracking

(firefox142 affected, firefox143 affected, firefox144 affected)

Tracking Status
firefox142 --- affected
firefox143 --- affected
firefox144 --- affected

People

(Reporter: jannis.baum, Unassigned, Mentored)

References

Details

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:142.0) Gecko/20100101 Firefox/142.0

Steps to reproduce:

Create a directory with a manifest.json with

{
    "manifest_version": 3,
    "name": "broken-onRemove",
    "version": "1.0",

    "permissions": ["tabs"],

    "background": {
        "scripts": ["background.js"],
        "type": "module"
    }
}

and a background.js with

browser.tabs.onRemoved.addListener(async () => {
    console.log("tabs.onRemoved triggered. Tabs:")
    console.log(await browser.tabs.query({}));
});

Then go to (about:debugging#/runtime/this-firefox)[about:debugging#/runtime/this-firefox), click "Load Temporary Add-on" and select the manifest.json you created. Next, click "Inspect" to open the console. Open at least 2 tabs and close one.

I have tested this on Firefox 142.0.1 (both via Zen 1.15.5b and in Vanilla Firefox)

Actual results:

The browser.tabs.query will return all tabs, including the one that was removed onRemoved.

Expected results:

browser.tabs.query should have only returned the open tabs, i.e. without the one that was removed. This is how it used to be up until a recent version

The Bugbug bot thinks this bug should belong to the 'WebExtensions::Untriaged' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Product: Firefox → WebExtensions

I reproduced the issue on the latest Nightly (144.0a1/20250909205747), Beta (143.0/20250908174027) and Release (142.0.1/20250827004350) under Windows 11 and Ubuntu 24.04 LTS.

As soon as I close a tab, the add-on console logs a tabs.onRemoved triggered. Tabs: followed by an array that indeed contains all tabs, including the one that was removed.
However, manually running browser.tabs.query({}) in the console will return only the open tabs.

Status: UNCONFIRMED → NEW
Ever confirmed: true

There was a fix for this in bug 1291830, but that piece of code is no longer around.

The reason for the tab being around is because we fire tabs.onRemoved on TabClose internally, which is during _beginRemoveTab. The actual removal is a bit later if animations are involved. A more detailed write-up was posted before at https://bugzilla.mozilla.org/show_bug.cgi?id=1396758#c11

We could fix this by using a different event to dispatch tabs.onRemoved, or by somehow hiding tabs from TabTracker after the TabClose event. The downside to the latter is that information may become unavailable if an event is fired after TabClose. So I'm inclined to delay tabs.onRemoved.

Component: Untriaged → General
See Also: → 1291830, 1396758
Summary: browser.tabs.query still has removed tab onRemove → browser.tabs.query still has removed tab when tabs.onRemoved fires

The severity field is not set for this bug.
:willdurand, could you have a look please?

For more information, please visit BugBot documentation.

Flags: needinfo?(wdurand)
Mentor: rob
Severity: -- → S4
Flags: needinfo?(wdurand)
Priority: -- → P3
You need to log in before you can comment on or make changes to this bug.