softwaremill / sttp

The Scala HTTP client you always wanted!
https://sttp.softwaremill.com
Apache License 2.0
1.45k stars 304 forks source link

When sending a `pong` fails, check if the WS isn't already closed #2236

Closed adamw closed 2 months ago

adamw commented 2 months ago

When sending an automated pong message for an incoming ping, it might happen that the WS becomes meanwhile closed. We should catch these exceptions and ignore them, if that's the case.

kciesielski commented 2 months ago

The issue happens here: https://github.com/softwaremill/sttp-shared/blob/master/ws/src/main/scala/sttp/ws/WebSocket.scala#L54

if pongOnPing is true (which is the default), the client tries to send a frame, which then fails for JDK ws client.

[info]   Cause: java.io.IOException: Output closed
[info]   at java.net.http/jdk.internal.net.http.websocket.MessageEncoder.encodePong(MessageEncoder.java:301)
[info]   at java.net.http/jdk.internal.net.http.websocket.TransportImpl$SendTask$1.onPong(TransportImpl.java:404)
[info]   at java.net.http/jdk.internal.net.http.websocket.TransportImpl$SendTask$1.onPong(TransportImpl.java:367)

it may be in a state that allows receiving a Close, but not sending anything. I don't know if this will happen for other clients.