Skip to content

[Tabs] Fix active tab flipping after programmatic selection - #5470

Closed
NoobMaster (oONoobMasterOo) wants to merge 4 commits into
microsoft:devfrom
oONoobMasterOo:fix/tabs-nested-activeid-loop
Closed

NoobMaster (oONoobMasterOo) wants to merge 4 commits into
microsoft:devfrom
oONoobMasterOo:fix/tabs-nested-activeid-loop

Conversation

@oONoobMasterOo

@oONoobMasterOo NoobMaster (oONoobMasterOo) commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request

📖 Description

Cause

fluent-tablist raises change not only on user selection, but also when the server writes activeid and whenever its tab collection changes (setTabs re-announces the current tab).

Since #5382, every tabchange received by the server is written back to activeid. When a change raised by the browser is still in flight while the server sets another tab (e.g. a new tab becomes visible, then ActiveTabId is set to it), the stale event is applied and written back, which raises a new change, and so on. Both values then alternate indefinitely.

Fix

The tab list renders an activeid-version attribute, incremented each time the server changes the active tab (ActiveTabId / ActiveTab parameters, SelectTabAsync, removal of the active tab). The tabchange event sends the version rendered when it was raised (TabChangeEventArgs.ActiveIdVersion), and FluentTabs ignores the events raised with an older version: they were raised before the browser applied the last server selection. All other events, including a user selection right after a server selection, are handled as before, so the behavior added in #5382 is kept.

activeid-version is rendered before activeid, so the change raised synchronously when activeid is updated already sends the new version. An event without a version (ActiveIdVersion = null) is handled as before.

🎫 Issues

👩‍💻 Reviewer Notes

The issue only reproduces with some latency between the browser and the server (see the Test Plan). The change is in FluentTabs.razor.cs (version check in the tab change handler) and FluentUICustomEvents.ts (the version sent with the event).

📑 Test Plan

Unit tests

Added:

  • FluentTabs_ParameterSelection_IgnoresTabChangeRaisedBeforeIt
  • FluentTabs_ServerSelection_IgnoresMatchingTabChangeRaisedBeforeIt (old B, old A, then the actual B echo, through the ActiveTabId parameter and SelectTabAsync)
  • FluentTabs_ParameterSelection_AcceptsUserSelectionRaisedAfterIt
  • FluentTabs_ParameterSelection_IgnoresStaleTabChangeForShownTab
  • FluentTabs_ParameterSelection_EchoUpdatesActiveTab

The snapshots are updated with activeid-version.

  • Full tests/Core suite on this branch: 4909 tests, 0 failed (at 175ea55b5).

Manual test

The issue does not reproduce on localhost because the round-trip is too fast. To reproduce it, add a latency to the outgoing messages in the browser console:

const send = WebSocket.prototype.send;
WebSocket.prototype.send = function (d) { setTimeout(() => send.call(this, d), 300); };
Scenario Before After
Sample from #5468 (300 ms latency), change events after "Show results" 37 in 6 s, then 32 in the next 5 s (still flipping) 3, then 0; stays on Results
Server selects B while the user clicks B then A (600 ms latency) — stale events ignored; stays on B, 0 events afterwards

Other checks

  • Before / after video: see the comment with the recordings below.
  • Tested with Interactive Server. Not tested with WebAssembly, where the round-trip has no network latency.

✅ Checklist

General

  • I have added tests for my changes.
  • I have tested my changes.
  • I have updated the project documentation to reflect my changes.
  • I have read the CONTRIBUTING documentation and followed the standards for this project.

AI contribution declaration

AI assistance is allowed; fully AI-generated or autonomous pull requests without meaningful human review are not accepted. See the AI contribution policy.

  • I have read and followed the AI contribution policy.
  • I have personally reviewed and understood all changes, tested them, and take responsibility for this contribution.

Component-specific

  • I have added a new component
  • I have added Unit Tests for my new component
  • I have modified an existing component
  • I have validated the Unit Tests for an existing component

⏭ Next Steps

None.

Since 5.0.1 a tab change event raised by the browser before it applied the
active tab set by the server was written back to the activeid attribute,
which made both values alternate indefinitely (e.g. nested FluentTabs).
Tab change events are now ignored until the browser confirms the tab
selected from code.

Fixes microsoft#5468

Copilot AI left a comment

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.

🟡 Changes recommended

Pending-selection handling can block user selections, leave component state stale, and miss dynamically added or shown tabs.

3 open issues
What changed in this PR

Addresses #5468 by tracking pending programmatic tab selections to prevent stale browser events from causing repeated tab switching.

Changes:

  • Adds pending-selection handling to FluentTabs.
  • Adds stale-event regression tests and updates confirmation-event coverage.
File Description
tests/​Core/​Components/​Tabs/​FluentTabsTests.razor Tests stale events and selection after browser confirmation.
src/​Core/​Components/​Tabs/​FluentTabs.razor.cs Tracks pending selections and filters incoming tab-change events.

Review effort: Balanced

var tab = Tabs.FirstOrDefault(t => t.Visible && !t.Disabled && string.Equals(t.Id, tabId, StringComparison.Ordinal));
if (await SetActiveTabAsync(tab))
{
_pendingActiveTabId = tab?.Id;

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.

Fixed in fb42ed5. The pending tab is now set inside SetActiveTabAsync before the selection callbacks are invoked, and only when the activeid attribute actually changes. Added FluentTabs_SelectTabAsync_AcceptsUserSelectionAfterEchoDuringCallback, which delivers the echo while the callback is suspended.

Comment on lines +172 to +174
_pendingActiveTabId = Tabs.Any(t => t.Visible && !t.Disabled && string.Equals(t.Id, activeTabId, StringComparison.Ordinal))
? activeTabId
: null;

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.

Fixed in fb42ed5. The pending tab is now validated when a tab change is received, once the child tabs are updated; if it can't be selected, it's cleared so the event is handled normally. Added FluentTabs_ParameterSelection_IgnoresStaleTabChangeForShownTab.

Comment on lines +300 to +305
if (string.Equals(args.ActiveId, _pendingActiveTabId, StringComparison.Ordinal))
{
_pendingActiveTabId = null;
}

return;

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.

Fixed in fb42ed5. On a matching echo, the pending tab is cleared and the event goes through SetActiveTabAsync, so ActiveTab and ActiveTabChanged are synchronized. Added FluentTabs_ParameterSelection_EchoUpdatesActiveTab.

@oONoobMasterOo

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Validate the pending tab when a tab change is received instead of when the
parameters are set, so a tab shown or added in the same render is covered.
Set the pending tab before invoking the selection callbacks, and synchronize
the active tab when the browser confirms the pending selection.
@dvoituron Denis Voituron (dvoituron) added the Generated by AI Content generated or significantly assisted by AI. label Oct 19, 2026
@dvoituron

Copy link
Copy Markdown
Collaborator

Could you please provide a short video showing the issue before the code change, and then the expected behavior after the fix? This will make it easier to clearly see and validate the improvement.
(You can use “Peek - Screen Recorder,” a free tool, to do this)

return;
}

if (_pendingActiveTabId is not null)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High: a stale event can acknowledge the wrong request

Confidence: high — browser reproduction and two failing bUnit tests.

Primary location: FluentTabs.razor.cs, line 295, the block spanning lines 295 through 303.

The code treats args.ActiveId == _pendingActiveTabId as sufficient acknowledgement and clears pending state. An older user event can already carry that ID. Equality of values does not prove that the browser has processed the latest server selection.

Confirmed sequence:

  1. Server and browser initially select A.
  2. The browser selects B, then A; their events are delayed in transit.
  3. Before they reach the server, an asynchronous server operation requests B, making B pending.
  4. The old B event arrives and clears pending state, even though it predates the server request.
  5. The old A event is then accepted and writes A back.
  6. The actual B echo arrives afterwards. A/B write-backs create further events and sustain the oscillation.

Impact: the defect this PR aims to fix remains reproducible when a server selection overlaps user changes in flight. Panels keep alternating and the circuit continues exchanging events without further interaction. Both ActiveTabId parameter selection and SelectTabAsync are affected.

Recommended correction: do not use the tab ID alone as proof that the current request has been processed. Establish explicit correlation between the server request and browser acknowledgement, or an equivalent mechanism that distinguishes events predating that request without blocking later user selections. This may require an interop contract adjustment and changes to TypeScript sources, not generated JavaScript. Add coverage for “old B, old A, actual B echo” through both entry points, plus browser validation under latency.

Both temporary review tests fail after delivering old B followed by old A, before delivering the actual echo:

TabChangeHandlerAsync_StaleMatchingEventBeforeRealEcho_KeepsProgrammaticSelection
  Expected: "b"
  Actual:   "a"

SelectTabAsync_StaleMatchingEventBeforeRealEcho_KeepsProgrammaticSelection
  Expected: "b"
  Actual:   "a"

total: 2; failed: 2; succeeded: 0; exit code: 2

The two selection entry points are consolidated into one finding because they share one root cause.

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.

You were right: the matching ID doesn't prove the request was processed. Reworked in e7814e2 with an explicit correlation: the tab list renders an activeid-version (incremented when the server changes the active tab, rendered before activeid), and the tabchange event now sends the version rendered when it was raised (TypeScript in FluentUICustomEvents.ts). Events raised with an older version are ignored, so the pending state is gone and later user selections are never blocked.

Added FluentTabs_ServerSelection_IgnoresMatchingTabChangeRaisedBeforeIt (old B, old A, then the actual B echo) for both the ActiveTabId parameter and SelectTabAsync: both cases fail with the previous implementation and pass now.

Browser validation under latency: with 600 ms on the outgoing messages, "select B in 500 ms" and then clicking B and A before the B render arrives: both clicks are sent with the old version and ignored, the B echo (new version) is accepted, and the tabs stay on B with no further events. The #5468 sample with 300 ms latency settles on Results after 3 events.

A matching tab ID was used as proof that the browser had applied the
active tab set by the server, but an older user event can carry the same
ID. The tab list now renders an activeid-version, incremented when the
server changes the active tab, and the tabchange event sends the version
rendered when it was raised, so older events are ignored.
@oONoobMasterOo

Copy link
Copy Markdown
Contributor Author

Here is the requested video, using the sample from #5468 with 300 ms latency added to the outgoing messages (WebSocket.prototype.send delayed in the browser console), since the round-trip is too fast on localhost.

Before (dev): after "Show results", the inner tabs keep alternating between Setup and Results.

5470-before.mp4

After (this PR): the inner tabs switch to Results and stay there, and clicking Setup / Results still works.

5470-after.mp4

@dvoituron

Copy link
Copy Markdown
Collaborator

We've added a new requirement regarding PRs

@oONoobMasterOo

Copy link
Copy Markdown
Contributor Author

We've added a new requirement regarding PRs

updated and checked.

@dvoituron

Copy link
Copy Markdown
Collaborator

Thank you for your contribution and your interest in improving the library.

I’m closing this PR without merging it. In its current form, the review and validation effort required is beyond what we can reasonably take on as maintainers.

Our AI contribution policy welcomes AI-assisted contributions, but requires meaningful human review, understanding, testing, and ownership of every change. AI-generated code is not automatically disqualified, but contributors remain responsible for explaining the implementation, validating its behavior, and considering potential regressions. That responsibility cannot be transferred to maintainers.

This decision does not mean the underlying issue is invalid. If you would like to revisit it, please consider submitting a smaller, focused PR with a clear explanation of the root cause, the proposed solution, and the validation performed.

Thank you for your understanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Generated by AI Content generated or significantly assisted by AI.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: programmatic tab selection flips between tabs in nested FluentTabs (regression 5.0.0 -> 5.0.1)

3 participants