Skip to content

fix: prevent deadlock on concurrent push and disconnect#24215

Merged
mshabarov merged 1 commit intomainfrom
fix/pushconnection-deadlock-main
Apr 30, 2026
Merged

fix: prevent deadlock on concurrent push and disconnect#24215
mshabarov merged 1 commit intomainfrom
fix/pushconnection-deadlock-main

Conversation

@mcollovati
Copy link
Copy Markdown
Collaborator

The previous AtomicBoolean guard in AtmospherePushConnection closed only the disconnect-vs-disconnect race. A push thread that reads disconnecting as false before a concurrent disconnect() flips it can still proceed into synchronized(lock) behind the disconnect thread, which is itself blocked inside resource.close() waiting for the servlet container's HTTP session lock held by the push thread — a two-lock cycle.

Move resource.close() out of the monitor: inside synchronized(lock) capture the resource into a local and call connectionLost() to transition the state, then release the monitor before invoking close() on the stashed reference. Add a matching re-check of isConnected() at the top of the synchronized block in push() so a push that waited for the monitor observes the late disconnect and defers via PUSH_PENDING/RESPONSE_PENDING instead of NPEing on the cleared resource. The disconnecting flag stays set until close() returns so subsequent pushes take the fast path and no new disconnect() re-enters while close() is still in flight.

Related-to #24192

The previous `AtomicBoolean` guard in `AtmospherePushConnection`
closed only the disconnect-vs-disconnect race. A push thread that
reads `disconnecting` as false before a concurrent `disconnect()`
flips it can still proceed into `synchronized(lock)` behind the
disconnect thread, which is itself blocked inside `resource.close()`
waiting for the servlet container's HTTP session lock held by the
push thread — a two-lock cycle.

Move `resource.close()` out of the monitor: inside
`synchronized(lock)` capture the resource into a local and call
`connectionLost()` to transition the state, then release the monitor
before invoking `close()` on the stashed reference. Add a matching
re-check of `isConnected()` at the top of the `synchronized` block
in `push()` so a push that waited for the monitor observes the late
disconnect and defers via `PUSH_PENDING`/`RESPONSE_PENDING` instead
of NPEing on the cleared resource. The `disconnecting` flag stays
set until `close()` returns so subsequent pushes take the fast path
and no new `disconnect()` re-enters while `close()` is still in
flight.

Related-to #24192
@sonarqubecloud
Copy link
Copy Markdown

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 29, 2026

Test Results

 1 394 files  ±0   1 394 suites  ±0   1h 17m 21s ⏱️ +8s
10 066 tests +1   9 996 ✅ +1  70 💤 ±0  0 ❌ ±0 
10 541 runs  +1  10 462 ✅ +1  79 💤 ±0  0 ❌ ±0 

Results for commit 9b526f9. ± Comparison against base commit 8450563.

♻️ This comment has been updated with latest results.

@mshabarov mshabarov added this pull request to the merge queue Apr 30, 2026
Merged via the queue into main with commit ec242f2 Apr 30, 2026
49 of 51 checks passed
@mshabarov mshabarov deleted the fix/pushconnection-deadlock-main branch April 30, 2026 11:56
vaadin-bot added a commit that referenced this pull request Apr 30, 2026
… 25.1) (#24227)

This PR cherry-picks changes from the original PR #24215 to branch 25.1.
---
#### Original PR description
> The previous `AtomicBoolean` guard in `AtmospherePushConnection`
closed only the disconnect-vs-disconnect race. A push thread that reads
`disconnecting` as false before a concurrent `disconnect()` flips it can
still proceed into `synchronized(lock)` behind the disconnect thread,
which is itself blocked inside `resource.close()` waiting for the
servlet container's HTTP session lock held by the push thread — a
two-lock cycle.
> 
> Move `resource.close()` out of the monitor: inside
`synchronized(lock)` capture the resource into a local and call
`connectionLost()` to transition the state, then release the monitor
before invoking `close()` on the stashed reference. Add a matching
re-check of `isConnected()` at the top of the `synchronized` block in
`push()` so a push that waited for the monitor observes the late
disconnect and defers via `PUSH_PENDING`/`RESPONSE_PENDING` instead of
NPEing on the cleared resource. The `disconnecting` flag stays set until
`close()` returns so subsequent pushes take the fast path and no new
`disconnect()` re-enters while `close()` is still in flight.
> 
> Related-to #24192

Co-authored-by: Marco Collovati <marco@vaadin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants