Repository navigation
fix: don't log push writes to a closed client connection as errors - #26850
totally-not-ai[bot] wants to merge 13 commits into
Conversation
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
vaadin-review-bot
left a comment
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
@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()setsclosedByApplication, notifies listeners and callscancel()synchronously, and only then closes the WebSocket.cancel()sets the resource'sisCancelledflag and callsasyncSupport.complete()/action(). For the JSR 356 / Servlet 3.0 support those only end the async context, they don't invoke theAtmosphereHandler.- When the WebSocket close comes back through
DefaultWebSocketProcessor.close(), it skipsexecuteClose()(and with itendRequest→completeLifecycle→onStateChange) whenisClosedByApplication()orresource.isCancelled()is true. AsynchronousProcessor.completeLifecycle()itself also returns early whenr.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-
left a comment
There was a problem hiding this comment.
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".
|
@Artur- Yes, partly. I added What it shows:
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. |
…n-closed-connection
|
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.
|
@Artur- Updated. The fix now also covers the error the IT found: when a push write fails with Tomcat's rejected full-message write ( 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. |
|
Removed the unused |
vaadin-review-bot
left a comment
There was a problem hiding this comment.
✅ Nothing to flag — the changes look good.
…n-closed-connection # Conflicts: # flow-server/src/test/java/com/vaadin/flow/server/communication/PushAtmosphereHandlerTest.java
vaadin-review-bot
left a comment
There was a problem hiding this comment.
✅ Nothing to flag — the changes look good.
vaadin-review-bot
left a comment
There was a problem hiding this comment.
✅ 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( |
There was a problem hiding this comment.
Should have "websocket" in the name
There was a problem hiding this comment.
Renamed it to isWebSocketWriteRejectedAfterFailedWrite, since it only matches writes on websocket connections.
|



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_WRITINGstate. Atmosphere's write semaphore is still released, so every later write on that connection is rejected withIllegalStateException: The remote endpoint was in state [TEXT_FULL_WRITING] .... That surfaced as ERROR log entries in two places:onStateChangewrote whatever message was on the shared, unsynchronized event, which can be a broadcast in progress.Nothing breaks in either case, but the ERROR lines show up in production logs and applications can't suppress them.
What changed
PushAtmosphereHandler.onStateChangeno 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...._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.Test summary
flow-serverpass.