Skip to content

vaadin-spring: @RouteScope beans are shared by all UIs with the same window.name (Spring twin of vaadin/cdi#526) #26820

Description

@DzmitryZaitsauIG

Description of the bug

VaadinRouteScope.RouteStoreWrapper.getUIStoreKey keys the route-scope bean store by window.name whenever the extended client details carry one. In Vaadin 24 the window name was fetched only for @PreserveOnRefresh routes, so only those views shared beans across UIs. In Vaadin 25 the window name is always known, so every route-scoped bean is shared by all UIs of a browser tab.

When a tab loads again while its previous UI is still open on the server, the new UI is given the previous UI's route-scoped component instance. Attaching it fails with IllegalStateException: Can't move a node from one state tree to another, and the page renders blank. The previous UI stays open whenever the old page ends without its UNLOAD beacon: a crashed or killed tab, a discarded tab, or a reload while offline. A normal reload works, because the beacon closes the old UI first.

vaadin/cdi#526 reported the same problem for CDI, and vaadin/cdi#527 fixed it there. The Spring implementation still keys by window name in 25.3.6.

Expected behavior

A new UI gets new route-scoped beans unless the route or its layout is @PreserveOnRefresh, the same rule as vaadin/cdi#527.

Minimal reproducible example

@Push
@SpringBootApplication
public class Application implements AppShellConfigurator {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

@Route("scoped")
public class ScopedView extends VerticalLayout {
  public ScopedView(ScopedPanel panel) {
    add(new Span("Scoped view ready"), panel);
  }
}

@SpringComponent
@RouteScope
@RouteScopeOwner(ScopedView.class)
public class ScopedPanel extends HorizontalLayout {
  public ScopedPanel() {
    add(new Span("route-scoped panel " + System.identityHashCode(this)));
  }
}
  1. Open /scoped. The panel renders.
  2. Make the old UI outlive the page, in either of these ways:
    • In Chrome, open the Task manager, end the tab's process, then click Reload.
    • Or in the DevTools console, run navigator.sendBeacon = () => true and then reload.
  3. The page is blank, and the server logs:
ERROR c.v.flow.router.InternalServerError : There was an exception while trying to navigate to 'scoped'
java.lang.IllegalStateException: Can't move a node from one state tree to another. [...] Offending component: node id=6, element with tag 'vaadin-horizontal-layout', component 'repro.ScopedPanel', used in 'repro.ScopedView', created at ScopedPanel.java:13, attached at ScopedView.java:10
	at com.vaadin.flow.internal.StateNode.doSetTree(StateNode.java:837) ~[flow-server-25.3.6.jar:25.3.6]

Versions

  • Vaadin 25.3.4, with Flow and vaadin-spring 25.3.6. First seen on 25.1.0.
  • Spring Boot 4.1.1
  • Java 21 (Corretto 21.0.7)
  • Chrome 156, macOS

Activity

  1. totally-not-ai commented on Nov 2, 2026

    @totally-not-ai
    Contributor

    Thanks for the detailed report and the reproduction. This is a real bug in vaadin-spring, and the code on main still has it.

    Root cause

    VaadinRouteScope.RouteStoreWrapper.getUIStoreKey(UI) keys the route bean store by "win-" + windowName whenever ExtendedClientDetails.getWindowName() is non-null, and by "uid-" + uiId otherwise. Up to Vaadin 24 a window name was only known for @PreserveOnRefresh chains, so this check was effectively a "preserve on refresh" detector. Since the client details are collected during UI initialization (Flow 25), every UI has a window name, so every route bean store is shared by all UIs of the tab.

    getBeanStore(UI) then hands the existing store to the new UI and re-points currentUI to it (beanStore.currentUI = ui). The route-scoped component it returns still belongs to the old UI's state tree. If the old UI is still attached (no UNLOAD beacon: killed or discarded tab, offline reload, duplicated or restored tab), attaching that component to the new UI throws Can't move a node from one state tree to another. After a normal reload the beacon has already closed the old UI, and RouteBeanStore.onComponentEvent(DetachEvent) / findPreservingUI hide the problem.

    Proposed fix (same rule as vaadin/cdi#527)

    • Key the store by UI id (uid-<uiId>) by default, and destroy it when that UI detaches.
    • Use the window key (win-<windowName>) only when the current navigation target or one of its layouts has @PreserveOnRefresh. NavigationListener already tracks currentNavigationTarget and currentLayouts from beforeEnter, so it can make that check before the beans are created. This is the same rule Flow applies in AbstractNavigationStateRenderer.isPreserveOnRefreshTarget.
    • Only window-keyed stores should be handed over to a preserving UI on detach (resetUI / findPreservingUI). A UI-keyed store is destroyed with its UI.
    • Re-scope the store when the same UI navigates between a preserved and a non-preserved chain while an owner stays in the chain (for example a shared layout). Move the store between the two keys instead of recreating the beans, and only move it back to the UI key if no other live UI of the window holds the preserved chain. The CDI fix needed this too. In Spring it should be simpler, because there is one RouteBeanStore per UI/window rather than one per owner.
    • The retrieveExtendedClientDetails(... relocateStore ...) path only matters when the window name is still unknown (e.g. embedded web components). With UI-id keys by default it can likely be reduced to the preserved case.

    Tests: unit tests in VaadinRouteScopeTest for (a) two live UIs with the same window name on a plain route getting different bean instances, (b) a @PreserveOnRefresh route (and a route whose layout has @PreserveOnRefresh) still sharing beans across a refresh, and (c) navigating preserved → plain → preserved in one UI keeping beans whose owner stays in the chain.

    Because this changes the lifecycle of route-scoped beans and has the re-scoping edge cases above, I'm leaving the implementation for a maintainer to confirm the approach rather than opening a PR right away. A related note: open PR #25901 (BrowserTab) also adds per-tab state keyed by window.name. It doesn't affect this fix, but the two should agree on what "same tab" means.

  2. added theissue type on Nov 3, 2026
  3. mshabarov commented on Nov 5, 2026

    @mshabarov
    Contributor

    @totally-not-ai implement your proposed fix

  4. moved this to 🔖 Normal Priority (P2) in Vaadin Flow bugs & maintenanceon Nov 5, 2026
  5. totally-not-ai commented on Nov 5, 2026

    @totally-not-ai
    Contributor

    @mshabarov I opened #26852 with the fix, a draft for now.

    • Spring route-scoped beans are now stored per UI and destroyed when their UI closes. They are shared by all UIs of a browser tab only while the route or one of its layouts has @PreserveOnRefresh. This is the same rule as fix: bind route scope to window name only for @PreserveOnRefresh cdi#527 and the CDI RouteScopedContext in this repo.
    • When a UI moves between a preserved and a non-preserved route, its existing beans move to the other storage, so beans whose owner stays in the chain are kept. A tab-level store only goes back to the UI that used it last. That way a new UI of a crashed tab never picks up the beans of the previous UI that is still open.
    • Behaviour change: on routes without @PreserveOnRefresh, route-scoped beans are no longer kept across a reload. That matches Vaadin 24.
    • vaadin-spring unit tests pass, including a new test for the scenario in this issue, which fails without the fix. I did not run the browser reproduction.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions