Skip to content

UI locking: make VaadinSession internals thread-safe #27581

Backlog

Description

@totally-not-ai

Part of #24187.

Make the framework's own VaadinSession internals safe to touch from more than one thread, as a preparation for UI locking. No behaviour change.

Scope

  • Store uIs as a ConcurrentHashMap inside VaadinSession. UIs keep living in the session (no service-wide registry), so they are still collected together with the session.
  • Use atomics or volatile for the request timing fields, errorHandler, browser and state, and a concurrent map in StreamResourceRegistry.
  • Keep the serialized form compatible: don't change the declared types of serialized fields such as nextUIId and requestHandlers, or handle the old form in readObject.
  • Leave Attributes alone. Component data (ComponentUtil.setData) is UI state, and session attributes stay behind the session lock.

Acceptance criteria

  • No behaviour change.
  • A session serialized with the previous version still deserializes (test for it).
  • No extra memory or locking cost for component data.

Working on behalf of Mikhail

Activity

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

    @totally-not-ai
    ContributorAuthor

    Triage: valid preparatory task. Not done on main yet. The scope is small and clear, but one acceptance criterion can't be met as written.

    • Where it fits: VaadinSession (UI map, request timing, errorHandler, browser, state) and StreamResourceRegistry. It is independent of UI locking: add UI lock API that delegates to the session lock #27580 and can land first.

    • Main caveat — serialization: no Flow class declares a serialVersionUID. The computed one for VaadinSession already differs between 25.3.9 and 25.4.0-beta2, so sessions don't survive a minor upgrade today. Adding volatile to a serialized field changes it again. So "a session serialized with the previous version still deserializes" only holds if we pin the UID, or if we read it as "the stream layout is unchanged".

    • Second caveat: the registry map is a final serialized field. Reading an old stream gives back a plain HashMap. To swap in a concurrent map on read, we must drop final, and that changes the registry's computed UID too. That UID has been stable across releases so far.

    • Next step: make the fields concurrent-safe and keep the stream layout: the UI map is still written as a HashMap, and both maps are converted when read. A maintainer needs to settle three points:

      1. Read the criterion as "same stream layout, round-trip test" rather than cross-version binary compatibility? (recommended: yes)
      2. Pin serialVersionUID on VaadinSession / StreamResourceRegistry? (recommended: no — that is a project-wide policy change, best handled in its own issue)
      3. OK to drop final on the registry map so it can be converted on read? (recommended: yes)

      One reply from a maintainer on this issue mentioning @totally-not-ai (e.g. @totally-not-ai 1: yes, 2: no, 3: yes) is enough for me to open a PR. Others are welcome to weigh in.

    Details
    • UIs: uIs is already transient and written by hand in writeObject. Store a ConcurrentHashMap, write new HashMap<>(uIs) so the stream stays the same, and wrap the map in readObject. getUIs() stays an unmodifiable view. Iteration becomes weakly consistent instead of fail-fast, but the order is the same for small integer IDs.
    • Request timing: cumulativeRequestDuration += time is a read-modify-write, so volatile alone would lose updates once two UIs write it concurrently. An AtomicLong would change the declared field type. A VarHandle getAndAdd on a volatile long keeps the type. Today all writers still hold the session lock, so this only matters once UI locking: per-UI lock core behind an experimental feature flag #27582 lands.
    • Not touched: nextUIId and requestHandlers keep their types, as the issue asks. They are only changed under the session lock, and UI creation stays a session-level operation. Attributes is left alone.
    • Lock checks: the timing getters and setters still call checkHasLock(). Relaxing those checks belongs to UI locking: per-UI lock core behind an experimental feature flag #27582, not here.
    • Test plan: deserialize a stream that has the old shape (HashMap for the UIs and for the registry). Check that the UIs and resources are restored and that a round trip keeps the same layout.
  2. Legioth commented on Dec 1, 2026

    @Legioth
    Member

    Let's drop the requirement for serialization compatibility. We aren't promising that in other cases either so there's no point in doing it here either.

  3. 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