Skip to content

fix: don't log push writes to a closed client connection as errors - #26850

Open
totally-not-ai[bot] wants to merge 13 commits into
mainfrom
fix/push-skip-write-on-closed-connection
Open

totally-not-ai[bot] wants to merge 13 commits into
mainfrom
fix/push-skip-write-on-closed-connection

Conversation

@totally-not-ai

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

Copy link
Copy Markdown
Contributor

Review risk: two-way door. It changes what the push handler writes on closing events, and the log level of one specific container error on websocket push connections. Connection cleanup is unchanged.

Fixes #26847

Summary

If a client goes away while a push message is still being written over a WebSocket, Tomcat fails that write and leaves the endpoint in the TEXT_FULL_WRITING state. Atmosphere's write semaphore is still released, so every later write on that connection is rejected with IllegalStateException: The remote endpoint was in state [TEXT_FULL_WRITING] .... That surfaced as ERROR log entries in two places:

  • On the close path, Atmosphere logged "Failed to cancel resource". The handler's onStateChange wrote whatever message was on the shared, unsynchronized event, which can be a broadcast in progress.
  • On the broadcaster path, Flow logged "Exception in push connection" for the next push to that client. In a reproduction against embedded Tomcat 11, this happened on most closes during a large push.

Nothing breaks in either case, but the ERROR lines show up in production logs and applications can't suppress them.

What changed

  • PushAtmosphereHandler.onStateChange no longer runs the inherited write-and-resume logic for events that are cancelled or closed by the client. The connection-lost handling for cancelled and timed-out events is unchanged.
  • When a push write on a websocket connection fails with the container's rejected full-message write (..._FULL_WRITING]), it is now logged at DEBUG as a lost connection instead of at ERROR as "Exception in push connection". Other exceptions are still logged at ERROR, and connection-lost handling still runs in both cases.
  • Behavior change: on a closing event, a message on the event is no longer written to the socket and the resource is no longer resumed. The client has already closed the connection, so there was nothing useful to deliver.

Test summary

  • Unit test: the message is written for a connected client, but not for a cancelled or closed-by-client event, and connection-lost handling still runs for a cancelled event.
  • Unit test: a rejected full-message write on a websocket connection is logged at DEBUG. A long-polling connection or a different endpoint state is still logged at ERROR, and connection-lost handling runs every time.
  • Reproduced on Spring Boot with embedded Tomcat 11, with no mocks. A plain websocket client opened real push connections, stopped reading during large pushes, and closed them 30 times. Without the fix, ERROR lines were logged; with it, none. The integration test is not kept in the build because it is too heavy to run every time. It is in bab63d9 if it is needed again.
  • The existing push communication unit tests in flow-server pass.

Atmosphere's close path calls onStateChange with an event message that
can belong to a broadcast still being written to the same WebSocket.
Writing it again fails on Tomcat with TEXT_FULL_WRITING and is logged
as "Failed to cancel resource" at ERROR level.

Fixes #26847
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 655 files  ±0   1 746 suites  ±0   1h 40m 37s ⏱️ - 5m 52s
14 082 tests +8  14 010 ✅ +8  72 💤 ±0  0 ❌ ±0 
14 508 runs  +8  14 436 ✅ +8  72 💤 ±0  0 ❌ ±0 

Results for commit 3d5c370. ± Comparison against base commit 1e15ac4.

♻️ This comment has been updated with latest results.

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the changes — left 1 comment.

Finding
⚠️ Guard misses the application-initiated close, where the same double-write race still exists

// closing event is a broadcast that is being written concurrently to
// the same socket. Writing it again collides with that write (Tomcat:
// "TEXT_FULL_WRITING") and the client is gone anyway.
if (!event.isCancelled() && !event.isClosedByClient()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

⚠️ Guard misses the application-initiated close, where the same double-write race still exists

The guard covers isCancelled() and isClosedByClient(), but not isClosedByApplication(). Flow closes push connections itself: AtmospherePushConnection.disconnect() waits at most one second for the in-flight broadcast future and then calls resource.close() anyway (the timeout is explicitly tolerated, see the Safari comment there). That leaves exactly the window this PR is fixing — a broadcast still writing while the close path runs onStateChange on the shared event — so the TEXT_FULL_WRITING collision and the ERROR log survive for server-initiated disconnects.

I could not read the Atmosphere sources in this environment, so I could not confirm that the application-close path reaches onStateChange with the stale message and with both checked flags false; if it does, the fix is only half of the problem.

PushAtmosphereHandler.java:58 · correctness

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.

@vaadin-review-bot I checked this against the Atmosphere sources (atmosphere-runtime 3.0.5.slf4jvaadin1). The application-initiated close never reaches onStateChange, so I left the guard as it is:

  • AtmosphereResourceImpl.close() sets closedByApplication, notifies listeners and calls cancel() synchronously, and only then closes the WebSocket. cancel() sets the resource's isCancelled flag and calls asyncSupport.complete()/action(). For the JSR 356 / Servlet 3.0 support those only end the async context, they don't invoke the AtmosphereHandler.
  • When the WebSocket close comes back through DefaultWebSocketProcessor.close(), it skips executeClose() (and with it endRequest → completeLifecycle → onStateChange) when isClosedByApplication() or resource.isCancelled() is true.
  • AsynchronousProcessor.completeLifecycle() itself also returns early when r.isCancelled(), so no other path invokes the handler for an already-closed resource.

Every event that does reach onStateChange through completeLifecycle is either closed by the client or marked cancelled/resumed-on-timeout first. The two flags checked here are the ones that can carry a stale broadcast message into a write during close. Adding isClosedByApplication() would be dead code.

@Artur- Artur- left a comment

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.

Is it possible to create an IT for this to test the real world case where it would be a problem - an IT that does not mock or fake the relevant parts?

Adds an integration test on Spring Boot with Tomcat that opens real push
connections with a plain websocket client, stops reading while the
server pushes large updates, and then closes the connection. It checks
that the server logs no errors.

The test currently fails: after the in-flight write fails, the next
broadcast is rejected by Tomcat with TEXT_FULL_WRITING and logged as
"Exception in push connection".
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Artur- Yes, partly. I added flow-tests/test-push-client-close (bab63d9). It runs Spring Boot on embedded Tomcat 11 with the real Atmosphere stack, and nothing is mocked. A plain java.net.http websocket client opens a real UI and push connection the way the client does. The server keeps pushing 64 KB updates, the client stops reading, and then it sends a close frame. This repeats 30 times, and then the test asks the server for any ERROR log lines.

What it shows:

  • The exact log from the issue (AsynchronousProcessor "Failed to cancel resource") did not show up in about 170 closes, with or without this PR. On the close path, Atmosphere sets the event message to null just before calling onStateChange, so the write only happens if a broadcast sets the message again in that tiny window. I couldn't make that timing reproducible.
  • About 85% of closes log a different ERROR with the same cause, with or without this PR: PushAtmosphereHandler "Exception in push connection", IllegalStateException: ... TEXT_FULL_WRITING, coming from the broadcaster thread (DefaultBroadcaster.executeAsyncWrite → onStateChange).
  • The cause is in Tomcat's TextMessageSendHandler.onResult: it only resets the endpoint state machine when a send succeeds. When the in-flight async send fails because the client went away, Atmosphere's JSR356WebSocket still releases its write semaphore, but the endpoint stays in TEXT_FULL_WRITING. Every later write on that socket is then rejected with IllegalStateException, whether it comes from a broadcast or from the close handling.

So the IT fails on this branch right now, and this PR on its own does not get rid of the ERROR logs in this scenario. A fix that covers both paths should treat the rejected write as a lost connection, at debug level, instead of only skipping the write on closing events. I'll update the PR once the approach is agreed.

@Artur-

Artur- commented Nov 21, 2026

Copy link
Copy Markdown
Member

Update the PR - we probably don't want to include such a complex IT to be run in all builds but it's good to use for testing

When a client goes away while a push message is being written, the
in-flight websocket write fails and the container rejects every later
write on that connection with an IllegalStateException (Tomcat:
TEXT_FULL_WRITING). The next broadcast then ended up logged at ERROR as
"Exception in push connection". Log it at debug level as a lost
connection instead; the connection is still cleaned up as before.

Remove the integration test added earlier: it is too heavy to run in
every build. It reproduced the problem against Tomcat and passes with
this change.
Match the container's rejected-write message instead of every
IllegalStateException on a websocket push connection, so unrelated
state errors are still logged at ERROR.
@totally-not-ai totally-not-ai Bot changed the title fix: skip push write when client closed the connection fix: don't log push writes to a closed client connection as errors Nov 21, 2026
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Artur- Updated. The fix now also covers the error the IT found: when a push write fails with Tomcat's rejected full-message write (TEXT_FULL_WRITING) on a websocket connection, it is logged at DEBUG as a lost connection, not at ERROR as "Exception in push connection". The check matches that specific error only, so other exceptions are still logged at ERROR. Connection cleanup is unchanged. The existing check that skips the write on closing events stays.

I removed the IT module from the build as you suggested. Run against this branch, it passed on every run with no ERROR lines logged, and it fails without the fix. It is still in bab63d9 if it is needed for testing again. The PR description is updated.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Removed the unused throws IOException from the new onThrowable test (java:S1130).

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

✅ Nothing to flag — the changes look good.

totally-not-ai Bot and others added 2 commits November 27, 2026 17:00
…n-closed-connection

# Conflicts:
#	flow-server/src/test/java/com/vaadin/flow/server/communication/PushAtmosphereHandlerTest.java
@Artur-
Artur- marked this pull request as ready for review December 1, 2026 17:07

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

✅ Nothing to flag — the changes look good.

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

✅ Nothing to flag — the changes look good.

* because the client went away, and rejects every further write on that
* connection with this {@link IllegalStateException}.
*/
private boolean isWriteRejectedAfterFailedWrite(

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.

Should have "websocket" in the name

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.

Renamed it to isWebSocketWriteRejectedAfterFailedWrite, since it only matches writes on websocket connections.

@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 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.

Push: "Failed to cancel resource" ERROR with IllegalStateException [TEXT_FULL_WRITING] when client closes WebSocket during async write

2 participants