Skip to content

Hackathon 25.4: A signal effect runs without the session lock when a RequestEndedEvent listener changes a shared signal #27187

Open

Description

@manolo

Description

A RequestEndedEvent listener, new in 25.4, that writes a SharedValueSignal runs every element effect of the request's own UI on the request thread, without the session lock, and the effect fails:

event.getSource().getEventBus().addListener(RequestEndedEvent.class,
        ended -> ENDED.update(count -> count + 1));

// in a view
Signal.effect(count, () -> count.setText("requests ended: " + ENDED.get()));
Exception in thread "http-nio-8125-exec-1" java.lang.IllegalStateException: Cannot access state in VaadinSession or UI without locking the session.
	at com.vaadin.flow.server.VaadinSession.checkHasLock(VaadinSession.java:683)
	at com.vaadin.flow.dom.ElementEffect.lambda$enableEffect$ff2b8a2a$1(ElementEffect.java:272)

The effect's own action throws, and then the error handler throws again on getErrorHandler(). ElementEffect.enableEffect decides with UI.getCurrent() == ui alone:

if (UI.getCurrent() == ui) {
    // Run immediately if on the same UI
    command.run();
} else { ... ui.access ... }

When the request ends, the lock is already released but UI.getCurrent() still names the request's UI, so the effect takes the inline branch. Effects of other UIs go through the dispatcher and work.

Why it matters

A request end listener is where request counters and timings belong, and the shared signal is how they reach a live view. The view that shows them is exactly the one whose own requests trigger the failure: the number stops moving after the first click, and the log fills with an exception per request. In the application where it was found, the lost update also took the response of the click with it, so a button on the same page did nothing.

Expected

The inline branch checks that the current thread holds the session lock, ui.getSession().hasLock(), and otherwise the effect is dispatched through ui.access like any other thread's.

Workaround

The listener clears the current instances around the signal write, so the effect sees no current UI and is dispatched:

var current = CurrentInstance.getInstances();
CurrentInstance.clearAll();
try {
    ENDED.update(count -> count + 1);
} finally {
    CurrentInstance.restoreInstances(current);
}

Reproduce

  1. mvn spring-boot:run, and open http://localhost:8125/.
  2. The page shows how many requests have ended, 3 on a fresh start. Press Click.
  3. The number does not move however many times Click is pressed, and the log shows the exception above for the requests of this page.

With the workaround in RequestCounter, the number moves with every click, 34 to 36 after two, and the log stays clean.

RequestEndedEvent is new in 25.4, so the project does not compile on 25.3.5.

Found on 25.4.0-beta2.

Getting the project

25-effect-unlocked-request-end.zip, 4 KB, sources only. Unzip it and:

mvn spring-boot:run

Activity

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

    @totally-not-ai
    Contributor

    Triage: confirmed bug. A signal effect of the request's own UI throws a session-lock exception when a shared signal is changed after the request has released the lock, and the UI update is lost.

    • Affects: code that changes a signal at the end of a request, after the lock is released but while the request's UI is still current: a RequestEndedEvent listener (25.4) or a VaadinRequestInterceptor.requestEnd. Effects of other UIs are fine.
    • Affected branches: the same check is on main and 25.0–25.4. Before 25.4 it can only be reached through a request interceptor.
    • Regression: no. The check was always there; the new request end event makes it easy to hit.
    • Severity: major. An exception is thrown for each request and the effect does not update the view.
    • Workaround: clear the current instances around the signal write, as shown in the issue. It is cheap but easy to forget.
    • Root cause: the effect decides to run right away based only on "is this UI the current UI", without checking that the thread holds the session lock.
    • Next step: draft PR fix: run signal effects inline only when session is locked #27188 runs the effect right away only when the session is also locked, and otherwise dispatches it through UI.access. The same check is applied to the signal result notifier and to the background-change flag.
    Root cause
    • ElementEffect.enableEffect runs the command inline when UI.getCurrent() == ui. Its fallback inside the dispatcher has the same check.
    • VaadinService.requestEnd fires RequestEndedEvent and calls the interceptors while CurrentInstance still holds the UI, but after the request handler has released the session lock.
    • The result notifier in VaadinService (createCurrentUiDispatcher) and Effect.onDependencyChange (the background-change flag) use the same UI-only check.
  2. Artur- commented on Nov 23, 2026

    @Artur-
    Member

    code that changes a signal at the end of a request, after the lock is released but while the request's UI is still current

    @totally-not-ai isn't this the real bug that the current UI is set when session is not locked? That sounds inconsistent with everything else?

  3. totally-not-ai commented on Nov 23, 2026

    @totally-not-ai
    Contributor

    @Artur- Yes, I agree. The lock check in the effect only treats the symptom. The real inconsistency is that the request's UI stays current after the lock is released.

    • Request handlers set the current UI while they hold the lock, for example in findUI and in bootstrap. Nothing restores it when they unlock. It is only cleared by CurrentInstance.clearAll() at the very end of requestEnd. So response writing, VaadinRequestInterceptor.requestEnd and RequestEndedEvent listeners all run with a current UI but without the lock.

    • Everywhere else, the current UI exists only while the lock is held: UI.access and VaadinSession.access set it and restore it around the locked work.

    • Changing this is a behaviour change. A requestEnd interceptor or request end listener that reads UI.getCurrent() today would get null.

    • Next step: restore the previous current UI when the request handler releases the session lock. This covers the UIDL, heartbeat and bootstrap handlers, and push. Please answer:

      1. Fix it at the root as described? (recommended: yes)
      2. Keep the lock check in effects as a cheap safety net for code that sets the current UI itself? (recommended: yes)
      3. Do the root fix only on main/25.4, and backport only the effect check to 25.0–25.3, where the interceptor change would be riskier? (recommended: yes)

      One reply from a maintainer mentioning @totally-not-ai with the answers is enough for me to update fix: run signal effects inline only when session is locked #27188. Until then, that PR contains only the effect check.

  4. added theissue type on Nov 24, 2026
  5. manolo commented on Dec 2, 2026

    @manolo
    MemberAuthor

    Related: #27446 is the same rule broken another way, UI serialization making the UI current without the session lock, which also reaches the inline effect branch.

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions