Skip to content

UI locking: add UI lock API that delegates to the session lock #27580

Backlog

Description

@totally-not-ai

Part of #24187.

First step towards per-UI locking: introduce the UI lock API without changing behaviour, so it becomes explicit which framework code protects UI state and which protects session state.

Scope

  • Add UI.getLockInstance(), lock(), unlock(), hasLock(), checkHasLock() and getPendingAccessQueue(). For now they delegate to the session lock and throw UIDetachedException when the UI is detached.
  • Change framework code that protects UI state from session.checkHasLock() to ui.checkHasLock(): UIInternals, StateTree, Router, BeforeLeaveEvent, ValueSignal, …

Acceptance criteria

  • No behaviour change: all existing tests pass unchanged.
  • Unit tests for the new UI methods, including the detached-UI case.
  • Add-ons can start migrating from session to UI lock checks.

Working on behalf of Mikhail

Activity

  1. totally-not-ai commented on Dec 1, 2026

    @totally-not-ai
    ContributorAuthor

    Triage: valid enhancement. Not on the default branch yet; it adds public API to UI, so a maintainer should confirm a few points first.

    • Where it fits: the new UI methods mirror the existing VaadinSession lock methods and, for now, just delegate to the session lock. Framework checks that guard UI state then call the UI instead of the session.
    • Caveat: "throw when detached" cannot apply to every method. Today the UI state tree and ValueSignal quietly skip the lock check when the UI has no session. Components of a detached or never-attached UI are routinely changed, for example in unit tests. If checkHasLock() threw there, it would break the "no behaviour change" goal.
    • Caveat: the locking rules ("step 0" in Locking issue when using Multiple Tabs in a near Real-Time-Application #24187) are not agreed yet. The method names and semantics below would become public before the per-UI lock exists.
    • Next step: add the delegating API and move the UI-state checks over, keeping today's detached-UI tolerance. 1. OK to add this public UI API now, before step 0 is settled? (recommended: yes — it only delegates and copies the VaadinSession names) 2. On a detached UI: lock(), unlock() and getLockInstance() throw UIDetachedException, hasLock() returns false, and checkHasLock() does nothing? (recommended: yes) 3. Also add getLastLocked() / getLastUnlocked(), as listed in Locking issue when using Multiple Tabs in a near Real-Time-Application #24187? (recommended: no, add them with the per-UI lock). One reply from a maintainer mentioning @totally-not-ai (e.g. @totally-not-ai do it, or @totally-not-ai 1: yes, 2: yes, 3: no) is enough for me to open a PR; others are welcome to weigh in.
    Call sites that check the lock for UI state today
    • UIInternals: eight session.checkHasLock() calls, plus a session != null && session.hasLock() check.
    • StateTree.checkHasLock(): skips the check when the session is null.
    • Router (ui.getSessionOrThrow().checkHasLock()), BeforeLeaveEvent (getSessionOrThrow().hasLock(), in setReferences and proceed).
    • ValueSignal: session == null || !session.hasLock().
    • UI.push() throws UIDetachedException and then calls session.checkHasLock(). That's the existing precedent for the throwing methods.
    • Session-scoped checks (StreamResourceRegistry, VaadinService, VaadinSession itself) should stay on the session lock.
    Notes for the implementation
    • Each new method should keep the exact exception type and message that its call site produces today. getSessionOrThrow() call sites already throw on a detached UI. StateTree and ValueSignal treat it as "no check" or "not locked".
    • getPendingAccessQueue() returns the session queue for now. UI.access() keeps using the session queue until step 3.
    • Tests: the new methods with an attached UI, a detached UI and a never-attached UI; the existing suites should pass unchanged.
  2. added theissue type on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions