Skip to content

fix: deliver shared signal changes hidden by the session snapshot - #27177

Draft
totally-not-ai[bot] wants to merge 27 commits into
mainfrom
fix/shared-signal-change-hidden-by-session-snapshot
Draft

totally-not-ai[bot] wants to merge 27 commits into
mainfrom
fix/shared-signal-change-hidden-by-session-snapshot

Conversation

@totally-not-ai

@totally-not-ai totally-not-ai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #27160

Behavior deviation · flow-server · apps that change shared signals from threads other than the request thread

Background — session snapshot. While a session is locked, shared signal reads come from a write-through transaction that keeps returning the values it first read. A change made by another thread becomes visible once that transaction is cleared, which happens before each pending access task and when the lock is released.

When another thread changed a shared signal during a request, a binding attached later in that request, or an effect created later in it, kept showing the old value until the next change. Such a listener is now held back until the transaction that hides the change ends, and is then added again as if the change had happened at that moment. For a UI, that means a new access task that runs before the response is written, so the browser gets one update in the same round trip. The workaround was to make the change through access.

Risks:

  • ⚠️ Behaviour change: such effects and bindings now re-run once with the new value in an access task, and report EffectContext.isBackgroundChange() as true for that re-run. Before, they never saw the change.
  • ⚠️ Behaviour change: when a thread holds a UI's session lock but a different UI (or none) is current, an element effect of that UI is now queued with ui.access directly instead of going through the effect dispatcher thread. This also means a change made in one UI's request reaches another UI of the same session in the same round trip.
  • ⚠️ Public API in com.vaadin.flow.signals.impl: Transaction.runAfterFinalize(Runnable), which returns a Registration for removing the task, and Transaction.finalizeTransaction(), which runs all tasks and then rethrows the first exception with the others suppressed. Transaction.runInTransaction can thus throw from a failing deferred observer after the transaction has applied its changes; if the transaction task itself threw, that exception is thrown with the finalize failure added as suppressed. Code that installs its own transaction fallback with Transaction.setTransactionFallback must finalize that transaction when it stops providing it; otherwise observers deferred on it are never notified. SignalTree.observeNextChange can now notify an observer when the current transaction is finalized, if that transaction hid a newer version of the node.
  • ⚠️ Behaviour change: VaadinService.runPendingAccessTasks now always clears the session-scoped transaction, also when no access task is queued. Shared signal reads after it, e.g. after UI.push() in an access block or while the response is written, see the latest values.
  • ✅ No serialization or performance concerns. Listeners are only deferred when the tree has moved on from the value that was read, and a failing deferred listener doesn't stop the others from being notified.

Context. #24400 cleared the snapshot before access tasks. That fixed effects that were already listening, because the tree notifies them on the writer's thread. A listener added after the change compared against the same stale snapshot and then only waited for future changes, so it never saw the change that had already happened.

  • SignalTree.observeNextChange checks, under the tree lock, whether the current transaction shows an older version of the node than the tree. If so, it registers a task with Transaction.runAfterFinalize. When the transaction is finalized, the observer is notified as if the node had changed at that moment, and added back to the tree if it asks to keep listening, all under the tree lock. If an outer transaction still hides the change, the observer is deferred again until that one is finalized.
  • Transaction.runInTransaction finalizes its transaction when it returns. VaadinSession finalizes its session-scoped transaction whenever it clears it, outside any transaction and without a current UI: at the start of runPendingAccessTasks, before each access task, when the access queue runs empty, and on unlock. If a deferred observer fails, the session passes the exception to its error handler after clearing the transaction.
  • With no current UI, the effect sees a background change. ElementEffect's dispatcher then queues the re-run with ui.access, because the current thread already holds the session lock, and the running drain loop picks it up before the response is written.
  • Tests cover a binding attached after the change, an effect created after it, an effect on a detached component, an effect reading a computed signal (whether or not the other thread refreshed its cache), an effect created in an access task, the release when the next access task starts, a UI detached before the release, an effect of another UI of the locked session, and a change made in the session transaction that still re-runs right away. Signal-level tests cover the release when an explicit transaction ends for a value and a list signal, deferral to an outer transaction, a hidden change that was reverted, removing a listener before or after its release, and a failing finalize task.

Working on behalf of @Legioth

While the session is locked, shared signal reads come from a snapshot
taken at the first read. An effect that started listening against that
snapshot, either when its component was attached or when it was
created later in the same request, missed a change made by another
thread in the meantime and kept showing the old value.

Such a change is now delivered in a new access task, which runs with a
fresh snapshot before the response is written.

Fixes #27160
Queue the deferred listener on the session so it still runs when the
UI is closed in the meantime, and run it right away for transactions
that don't defer, which keeps their old behaviour.
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Type of change

  • Bugfix
Why this approach
  • It follows option 1b from the issue discussion: the usage notices that the tree has moved on from the value it read and re-listens from a context where the change is visible.
  • The re-run is queued as a new access in the same round trip rather than left for the next request, so only one update goes to the browser.
  • Alternative: the session could remember every usage checked against its snapshot and recheck them all when the snapshot is cleared. That needs extra bookkeeping per session, so I didn't pick it.
API changes
// com.vaadin.flow.signals.impl.Transaction
// Added
public static Transaction createWriteThrough(SerializableExecutor freshReadExecutor)
public void runWithFreshReads(Runnable task) // default runs the task right away

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Added tests for the paths that were not covered: delivery when there is no current UI or the UI closes before delivery, a listener removed before delivery, and an explicit write-through transaction, which keeps listening for later changes. Each test fails if its path breaks.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 655 files  ± 0   1 746 suites  ±0   1h 49m 44s ⏱️ + 1m 19s
14 078 tests +24  14 006 ✅ +24  72 💤 ±0  0 ❌ ±0 
14 504 runs  +24  14 432 ✅ +24  72 💤 ±0  0 ❌ ±0 

Results for commit 26e1765. ± Comparison against base commit f61c209.

♻️ This comment has been updated with latest results.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

createWriteThrough(SerializableExecutor) now checks its argument with Objects.requireNonNull instead of an assert, because it is public API. I left the requestHandlers field in VaadinSession as it is: this PR doesn't touch it, and the warning is not about this change.

@Legioth

Legioth commented Nov 25, 2026

Copy link
Copy Markdown
Member

I'm not sure about the abstraction level used for this fix. If each access tasks has its own transaction, then I imagine it would be enough to make sure the effect's change processing logic kicks in also in this case since that logic should be run as a new access task that thus sees the updated values.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Legioth I tried that: drop the new Transaction API and the deferred registration, and let onNextChange call the listener right away when the tree has a newer value than the snapshot, so the effect's normal change handling takes over. It doesn't work on its own, because in the reported case that handling doesn't run as a new access task.

  • During attach (and when an effect is created in a request), the owner UI is current, so the ElementEffect dispatcher runs the invalidation inline, in the same access. The re-run reads the same snapshot, registers again, sees the hidden change again and recurses. Both effect tests in this PR end in a StackOverflowError with initial repeated hundreds of times.
  • Only a change from another thread can be hidden like this, and those changes already go through ui.access. The inline path exists for changes made in the current access, which the snapshot does show.

So some piece has to know that the change is hidden and move the re-run to a new access. Today that is the usage, which asks the transaction for a context with fresh reads. The other place is the effect:

  1. Keep it in the usage (current PR). It covers every listener of a usage, not only effects, and the session decides how to reach a fresh access.
  2. Move it to the effect. The usage just reports the hidden change right away. Effect sees that none of its usages show a change in the current context and dispatches the invalidation through a new access instead of inline. That removes the deferred registration from AbstractSharedSignal. It also lets isBackgroundChange() return true for this re-run. But Effect still needs a way to get to a fresh access (the same transaction hook, or a second dispatcher from ElementEffect), and it needs a guard for contexts where no fresh access exists, such as an explicit transaction.

I'd keep 1, but 2 is a reasonable fit if you'd rather keep this in the effect. Tell me which one and I'll update the PR.

@Legioth

Legioth commented Nov 26, 2026

Copy link
Copy Markdown
Member

The main architectural mismatch is maybe that a transaction has to know anything about how to run a tasks outside that transaction. That seems like something that would be more related to the effect instead. Would it be feasible to move only that part of the logic while keeping the rest?

A transaction should not need to know how to run a task outside
itself. The usage still detects a change that the current transaction
hides, but it now asks the listener for a context with fresh reads
instead of the transaction. Effect provides that context by queuing a
new session access, so the session-scoped transaction and VaadinSession
no longer take part in delivering the change.

Listeners that don't provide such a context keep the previous
behaviour and are only notified of later changes. The tests that used a
plain usage listener under a session lock now use an effect, since a
plain listener no longer gets the deferred delivery.
A listener wrapper that doesn't delegate runWithFreshReads hides the
wrapped listener's context, so the cached signal usage wrapper now
forwards it like the combined usage does, and the TransientListener
Javadoc says wrappers should delegate.

The removed-listener case is now tested at the usage level, since a
disposed effect never re-runs regardless of whether the deferred
registration is added. Also covers an effect whose hidden change has no
locked session to defer to.
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Legioth Yes, I moved that part to the effect and kept the rest.

  • Transaction and VaadinSession are back to what they were on main.
  • The usage still detects a change that the current transaction hides. Instead of asking the transaction, it now asks the listener for a context with fresh reads, through a new default method TransientListener.runWithFreshReads(Runnable). By default the method returns false, so other listeners keep their old behaviour.
  • Effect implements it: when the current session is locked, it queues a new session access with the current UI set, so the re-run still lands in the same round trip. The usage wrappers in UsageTracker and CachedSignal delegate it to the listener they wrap.
  • One small change in behaviour: an effect created inside an explicit transaction while the session is locked now also gets the deferred re-run.

Now that the effect starts the deferred delivery, it could also mark that re-run so that isBackgroundChange() returns true. I've left that out of this PR; tell me if you want it here.

Covers a computed signal whose cache the other thread never refreshes,
the effect re-running with its UI as the current UI, a fresh-read task
that runs while the change is still hidden, and removing a listener
after its deferred registration was added.
@github-actions github-actions Bot added +0.1.0 and removed +0.0.1 labels Nov 26, 2026
@Legioth

Legioth commented Nov 26, 2026

Copy link
Copy Markdown
Member

Make isBackgroundChange() behave as expected for these re-runs

The re-run that delivers a change hidden by the session snapshot runs
in a new access with the owner UI as the current UI, so it looked like
a change made by that UI. The change was made by another thread, so
the effect now marks the re-run explicitly and isBackgroundChange()
returns true for it.
Also words the comments as a change made outside the session
transaction, since that is what the flag tracks; usually that is
another thread.
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Legioth Done. The re-run that delivers a hidden change now reports isBackgroundChange() as true.

  • Effect's listener sets a flag while the deferred registration runs in the new access, and onDependencyChange treats a change it receives then as a background change. It runs with the owner UI as the current UI, so the old check would have said false.
  • The flag is cleared right after, so later changes made by the UI itself still report false. The tests check both.
  • Strictly, the flag marks a change that was made outside the session transaction. That is almost always another thread. The one exception is code on the request thread that writes through Signal.runWithoutTransaction, which would also count as a background change.

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Memory footprint

No change compared with the base commit f61c209.

@Legioth

Legioth commented Nov 26, 2026

Copy link
Copy Markdown
Member

Effect shouldn't really know anything about VaadinSession since that's the part that ElementEffect is responsible for. But at the the same time, it raises the question of how this should work for a Signal.unboundEffect that technically has nothing to do with sessions but still need a way of applying changes correctly even if they get hidden by a transaction.

@Legioth

Legioth commented Nov 26, 2026

Copy link
Copy Markdown
Member

Is it so that the initial read when the effect is created leaves no listeners behind which means that the "concurrent" write doesn't deliver any updates. This in turn means that detection of the situation would have to happen when reading for the attach?

Would it be a valid approach to always put all attach reads in a separate access task, or would that lead to other side effects? One such side effect might be that a view with many signal bindings would end up with lots of access tasks to process.

Alternatively, could ElementEffect detect the situation when handling the attach and run it through access only in that case?

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Legioth Yes, that's what happens. The probe run of a detached element passivates the effect right away, so no listener is left on the tree, and the other thread's set notifies nobody. On attach, activate() calls hasChanges(), which reads through the session snapshot and says "no change". It then registers for later changes only. So the situation has to be detected when the effect registers its listeners again. That happens at attach, but also when an effect is created on an already attached component later in the same request (the second test case), and whenever an effect re-runs during the request and reads a dependency that has a hidden change.

Always running the attach in a separate access:

  • It would only cover attach, not the create and re-run cases above.
  • Every binding would get its first value one access later. Code that runs after add(...) in the same request, such as getText() on a bound component, a beforeEnter/afterNavigation observer or a browserless test, would no longer see the bound value right away.
  • Each access task gets a new session transaction, so bindings in the same view could end up rendering values from different snapshots instead of one consistent one.
  • It adds one access task per binding on every attach. The tasks are cheap, but a large view would queue hundreds of them to handle a rare case.

Having ElementEffect detect it and use access only then: that's possible. ElementEffect would need to ask whether a dependency has a change that the current transaction hides, so Effect/Usage would need a query method for that instead of runWithFreshReads. It would also have to ask after creation and after every run during a request, not only at attach, to cover the same cases. That spreads the check over several call sites in ElementEffect. Today the usage does the check once, at the point where the listener is registered.

So I see three options:

  1. Keep the current shape: the usage detects it and the effect routes the task through the fresh read dispatcher that ElementEffect provides.
  2. Same detection, but Effect marks the task so that uiDispatcher takes the access branch, with no second dispatcher (as in my previous comment).
  3. ElementEffect checks for hidden changes after attach, creation and each run, and goes through access only then.

I'd go with 1 or 2. Let me know which one you prefer.

@Legioth

Legioth commented Nov 26, 2026

Copy link
Copy Markdown
Member

Yet another brainstorming idea: could the logic that starts listening for changes detect that the listener was added from a context that sees a stale value and in that case schedule a listener invocation to be scheduled in a way that uses access when it happens through ElementEffect? The main challenge there might be how the generic listener adding logic would know how to dispatch the invocation.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Legioth That's close to what the PR does now. The listener-adding logic in AbstractSharedSignal's usage (onNextChange) checks, under the tree lock, whether the tree has moved on from the value this context read. If it has, it doesn't register for later changes only, but schedules work through a dispatcher that the listener provides (TransientListener.runWithFreshReads). Effect passes that on to its fresh read dispatcher, which ElementEffect sets to ui.access.

Two differences from your sketch:

  • What is scheduled. The usage doesn't invoke the listener directly from the scheduled task. It adds the listener again from the fresh context, so the listener runs right away if the change is still there, and otherwise keeps listening normally. That handles listeners that want to keep listening after the first invocation, and a change that is reverted before the task runs. The task's registration can also be removed before the task runs, for example when an effect is passivated or disposed. For Effect, the outcome is the same as invoking it directly.
  • How the generic logic knows how to dispatch. I see two places that could supply that:
    1. The listener, as now. Only listeners that know a fresh context, which today means effects, get the delayed delivery. ElementEffect decides it's ui.access, and an unbound effect uses the default effect dispatcher. The effect also knows the delivery is late, which is how isBackgroundChange() returns true for it.
    2. The SignalEnvironment, like getResultNotifier() already does with createCurrentUiDispatcher(). The VaadinService environment would return a dispatcher that queues UI.getCurrent().access(...) (or VaadinSession.access) while the session is locked. That would remove the new API on Effect and TransientListener and the changes to ElementEffect, at the cost of a new protected method on SignalEnvironment. Two drawbacks: every listener would get the delayed delivery, not just effects, and the effect could no longer tell the delivery apart from a change by its own UI. So isBackgroundChange() would be false for it again, unless the environment also marks it somehow.

I lean towards 1, because it keeps isBackgroundChange() right. 2 is the smaller change if you'd rather keep this out of Effect and ElementEffect altogether. Which do you prefer?

@Legioth

Legioth commented Nov 26, 2026

Copy link
Copy Markdown
Member

Semantically, the thread where the view is created should perceive things as though the signal write happened exactly at the moment when the shadowing transaction ended. The automatic read transaction is closed between access tasks which means that the effect listener would be notified at a time when there's no current UI which in turn means that the change would be applied as a new access task. Would it work to collect a quite of pending notifications on the transaction instance and then release them at a stage where no custom dispatching logic is needed?

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Legioth Yes, I think that works, and it fits the semantics better: the transaction keeps a queue of listeners that were added while it hid a newer value, and releases them when it ends. Releasing one means adding the listener again outside the transaction, the same re-listen the usage does now, so a listener that wants to keep listening still ends up registered. The TransientListener hook, Effect's fresh read dispatcher and the ElementEffect changes would all go away. Transaction would get an internal "run after this transaction ends" hook instead of knowing how to dispatch. Explicit transactions would release their queue when runInTransaction finishes, so they'd be covered by the same mechanism.

Three things to settle:

  1. Release points. The session transaction is cleared before each pending access task, but runPendingAccessTasks returns early when the queue is empty. Otherwise the next clear is in unlock(), after the response has been written and while the request's UI is still current. So the session would release the queue with the current instances cleared, both before access tasks and at unlock. Then nothing runs inline as if it were a change from that UI.
  2. Timing. With no current UI, the effect takes the normal background path: uiDispatcher → default effect dispatcher → ui.access from a pool thread. That task may or may not be queued before the request has run its pending access tasks. So the update reaches the browser with the next push or round trip, not reliably in the current response. This is exactly the "as if the write happened when the transaction ended" behaviour. It gives up the same-round-trip delivery we have now, which is what made the separate dispatcher necessary in the first place.
  3. isBackgroundChange() comes out true by itself, since there is no current UI when the listener fires. The extra flag in Effect goes away too.

If you're fine with 2, I'll rework the PR to this approach.

@Legioth

Legioth commented Nov 26, 2026

Copy link
Copy Markdown
Member

Is there any way of making the ui dispatcher do access immediately without going through the pool thread specifically in this case?

@Legioth

Legioth commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Would a locked session but no current UI work as av indication that access should skip the executor dispatcher?

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Legioth Yes to both, with a slightly narrower condition: "the current thread holds the lock of this UI's session" rather than "a session is locked and there's no current UI".

  • The detour through the pool thread is there so that a thread never takes a second session's lock while it holds another one, and so that a writer thread doesn't block on a session lock. When the current thread already holds ui's own session lock, ui.access doesn't lock anything. It just adds the task to the queue that this thread drains before writing the response. So uiDispatcher can call ui.access directly in that case: UI.getCurrent() != ui && ui.getSession().hasLock().
  • "Locked session, no current UI" on its own isn't safe. A VaadinSession.access task for another user's session also has no current UI, but holds that other session's lock. Calling ui.access there would lock a second session, and that's the deadlock the pool thread avoids. With the own-session check, that case still goes through the pool thread.
  • It also helps in a case unrelated to this bug: a change made while another UI of the same session is current would then reach this UI in the same round trip.

Combined with your transaction-queue idea, that gives same-round-trip delivery without any custom dispatching:

  1. The usage queues the listener on the transaction that hides the change. The transaction re-adds it outside itself when it ends.
  2. The session releases that queue when it clears its transaction, with no current UI. runPendingAccessTasks currently returns early when the access queue is empty, so I'd clear and release before that check. That way the pending listeners from this request are always released before the response is written.
  3. The effect is notified with no current UI, so isBackgroundChange() is true. uiDispatcher sees its own session locked and calls ui.access, and the drain loop that is already running picks the task up in the same round trip.
  4. A change hidden in the very last access before unlock() is released there and picked up by the existing purge after unlocking, so it arrives with the next push or round trip.

That removes the TransientListener hook, Effect's fresh read dispatcher and the extra flag, and keeps only the uiDispatcher branch in ElementEffect. If that sounds right, I'll rework the PR to it.

@Legioth

Legioth commented Nov 26, 2026

Copy link
Copy Markdown
Member

Du it

A listener added while the current transaction hides a newer value is
now queued on that transaction and added again when the transaction
ends, as if the change had happened at that moment. Explicit
transactions release the queue when runInTransaction returns. The
session releases the queue of its session-scoped transaction without
a current UI before pending access tasks run, after each of them and
when it is unlocked.

Since there is no current UI when a released listener fires, the
effect re-run reports a background change by itself. The UI effect
dispatcher queues the re-run with ui.access directly when the current
thread already holds that UI's session lock, instead of going through
the effect dispatcher thread, so it is run before the response is
written.

This replaces the fresh read hook on TransientListener and the fresh
read dispatcher of Effect, so those and their tests are removed.
A failing deferred listener no longer stops the remaining ones from
being added again or leaves the stale session transaction in place: the
session clears its transaction before releasing the listeners, and each
end task's exception goes to the thread's uncaught exception handler.
Also documents that the owner of a fallback transaction must release
its end tasks, and covers the release when the next access task starts
and a UI that is detached before the release.
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Legioth Done. The PR now uses the transaction queue plus the uiDispatcher branch, and the previous hooks are gone.

  • Deferring: when the usage sees that the current transaction hides a newer value, it queues the listener with the new Transaction.runAfterEnd(Runnable) instead of registering it. When the transaction ends, the listener is added again in the context that ended it. If an outer transaction still hides the change, it waits for that one too.
  • Releasing: runInTransaction releases its queue when it returns. The session releases its own with no current UI and outside any transaction, through Transaction.runEndTasks(). That happens at the start of runPendingAccessTasks (so it no longer depends on the queue being non-empty), whenever it clears the transaction before a task, when the queue runs empty, and on unlock.
  • uiDispatcher: when the current thread holds this UI's session lock and the UI isn't current, it calls ui.access directly, so the re-run is picked up before the response is written. isBackgroundChange() is true for it without any extra flag.
  • Removed: TransientListener.runWithFreshReads, Effect's fresh read dispatcher, the constructor and setter for it, and the background-change flag in Effect.
  • A failing released listener goes to the uncaught exception handler, so it doesn't stop the others or leave the stale transaction in place. The setTransactionFallback Javadoc now says that the owner of a fallback transaction must call runEndTasks().

@github-actions github-actions Bot added +0.0.1 and removed +0.1.0 labels Nov 26, 2026
SignalTree.observeNextChange now checks whether the current
transaction shows an older version of the observed node than the tree,
and in that case notifies the observer once the transaction has been
finalized instead of only waiting for later changes. This replaces the
same check in the shared signal usage, so every observer of a tree gets
it.

Renames the transaction hooks to runAfterFinalize and
finalizeTransaction after what they mean, catches any exception from a
finalize task, and lets the session clear its transaction instead of
only running its finalize tasks, so that finalizing always means the
transaction no longer applies.
The observer released when a transaction is finalized is now checked
again against the finalizing context, notified with immediate set to
true since the change happened before it was added, and added back to
the tree under the same lock so that a concurrent change isn't missed.
Documents the deferred path on observeNextChange and covers a hidden
list insert, a hidden change that was reverted, a fresh read after the
access tasks have run and a failing finalize task.
runAfterFinalize now returns a registration that removes the task, so a
deferred observer only needs a small holder guarded by the tree lock
instead of a separately synchronized registration. Adding an observer
and adding a released one share the same code path, and the check is
renamed to hasHiddenChange.

finalizeTransaction runs all tasks and then rethrows the first
exception with the others suppressed. runInTransaction keeps the
exception of its own task and adds a finalize failure to it as
suppressed, and the session passes a failure to its error handler
after clearing its transaction.
…nge-hidden-by-session-snapshot

# Conflicts:
#	flow-server/src/main/java/com/vaadin/flow/dom/ElementEffect.java
Hold a deferred observer's registration in an AtomicReference where null
means removed, and keep it removed when the observer removes itself while
it is notified after the transaction ends; it used to stay in the tree and
get notified again. Keep finalize tasks in an ArrayDeque so that running
them doesn't shift the list each time, assert that a transaction isn't
finalized while it is current, and let the session handle any exception
from finalizing its transaction.
if (finalizeTask != null) {
Registration registration = observe(nodeId, observer, true);
// The observer may have been removed while it was notified
if (!current.compareAndSet(finalizeTask, registration)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The atomic operation makes this more difficult to understand than necessary. Checking for current.get() == null would be easier to understand and should be just as correct since current is also protected by the lock.

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.

@Legioth Agreed. The finalize task now checks current.get() == null after notifying the observer: if it is null, the observer was removed meanwhile and the new registration is removed; otherwise it is stored. The returned registration likewise uses get() and set(null) instead of getAndSet. The AtomicReference is now only a mutable holder guarded by the tree lock, as the comment above it says.

The holder is only accessed while holding the tree lock, so the atomic
compare-and-set adds nothing over a null check.
@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hackathon 25.4: A shared signal changed by another thread before a component is attached never reaches its bindings

1 participant