playframework icon indicating copy to clipboard operation
playframework copied to clipboard

Netty: Websockets should respect play.server.http.idleTimeout

Open mkurz opened this issue 3 years ago • 1 comments

So a premium sponsor reached out to me regarding problems with Websockets. Turns out the Netty server does not care about the play.server.http.idleTimeout config when upgrading a connection to a websocket. akka-http however works correctly (did extensive manually testing using https://github.com/vi/websocat and even wireshark to watch those frames).

The problem step-by-step:

  1. The netty backend sets up request handler which gets added to the netty pipeline via a HttpStreamsServerHandler.
  2. Our PlayRequestHandler reacts to IdleStateEvents to close connections if they are idle.
  3. When upgrading a connection to a websocket, the handler of the connection (which is a HttpStreamsServerHandler which wraps our PlayRequestHandler) removes itself from the pipeline. Problem here is when our request handler is not on the pipeline anymore, then also our userEventTriggered handler to close connections isn't there anymore as well!

To fix that I split out the userEventTriggered method into its own handler, which always stays in the pipeline.

This is working great, I did manual testing. However... actually I am trying to add a test to https://github.com/playframework/playframework/blob/main/core/play-integration-test/src/it/scala/play/it/http/websocket/WebSocketSpec.scala since hours, however I just can't get akka-http to work correctly, the connection always stays open, even when passing the serverconfig a idle timeout of 1 second and stopping a thread within a websockt for like 3 seconds. Netty correctly closes the connection. I am not sure what I do wrong in the test, maybe akka-http is configured different.... I need to find out.

mkurz avatar Sep 12 '22 21:09 mkurz

Aaarghh... Turns out there is a bug in akka-http when http2 is enabled (which is for the TestSuite): https://github.com/akka/akka-http/issues/3959 I was bumping my head against the wall already why I can't get a test working correctly...

mkurz avatar Sep 13 '22 11:09 mkurz

@Mergifyio backport main

mkurz avatar Nov 01 '22 06:11 mkurz

backport main

✅ Backports have been created

mergify[bot] avatar Nov 01 '22 06:11 mergify[bot]