Skip to content

Fix tab indexing in close/reload/next/prev tab functions#2396

Open
lefevau wants to merge 3 commits into
brookhong:masterfrom
lefevau:fix/tab-indexing
Open

Fix tab indexing in close/reload/next/prev tab functions#2396
lefevau wants to merge 3 commits into
brookhong:masterfrom
lefevau:fix/tab-indexing

Conversation

@lefevau

@lefevau lefevau commented Feb 19, 2026

Copy link
Copy Markdown

This fixes issues with Zen (and potentially other browsers) where the index in the tab snapshot provided to these functions is stale, causing the wrong tabs to be targeted.

Closes #2306

@brookhong

Copy link
Copy Markdown
Owner

So tab.index is missing from Zen? Please create a function like getTabIndex and use it at line 917 and 948, the function should use tab.index if exists, otherwise use indexOf to figure it out.

@lefevau

lefevau commented Mar 2, 2026

Copy link
Copy Markdown
Author

It's not that it's missing, but rather it's wrong or maybe just stale.

Doing some more digging I found: zen-browser/desktop#11210
So perhaps this can be closed if you just want to wait for it to be resolved in Zen.

Either way I made the requested changes to move the tab indexing into a getTabIndex function.

hbt added a commit to hbt/Surfingkeys that referenced this pull request May 31, 2026
…lookup

Fixes 4 stale-snapshot usages where `tab.index` (captured from `sender.tab`
at message-dispatch time) was used inside a `chrome.tabs.query` callback,
by which time the tab may have been reordered.

Changes:
- Add `_getTabIndex(tabs, tabId)` helper that resolves live position by ID
- `_nextTab`: replace `tab.index` with `_getTabIndex(tabs, tab.id)` for
  boundary check and `_fixTo` call; return early if tab not found
- `_roundRepeatTabs`: same — replace `tab.index` with live lookup before
  `_roundBase` call
- `previousTab`/`nextTab` switch-case (~line 783): move index arithmetic
  inside `chrome.tabs.query` callback; look up live index by tab ID instead
  of capturing stale `tab.index` before the async call
- `tabURLAccessed` (~line 2231): add comment clarifying `sender.tab.index`
  is Chrome-fresh at message receipt (not a stale snapshot) — no fix needed

Add scratch tests documenting the stale-index bug scenario:
- `scratch-tab-stale-index.spec.ts` — covers `_nextTab` / `_roundRepeatTabs`
- `scratch-tab-stale-index-prev-next.spec.ts` — covers the switch-case handler

Relates to upstream brookhong#2396 (partial fix, only `_nextTab`
and `_roundRepeatTabs`).

#tab-navigation #async-race #stale-snapshot #fix #chrome-tabs
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.

version 1.17.5 on Zen browser doesnot work properly.

2 participants