eclipse-ee4j / tyrus

Tyrus
Other
115 stars 37 forks source link

Unwanted Exception in Tyrus thread #771

Closed MaciejCiszewski closed 2 years ago

MaciejCiszewski commented 2 years ago

java.lang.IllegalArgumentException: Reason Phrase cannot exceed 123 UTF-8 encoded bytes: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@749a0411[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@1f9b7416[Wrapped task = org.glassfish.tyrus.core.TyrusSession$IdleTimeoutCommand@a6e78d8]] rejected from java.util.concurrent.ScheduledThreadPoolExecutor@3bb07421[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 132784] at javax.websocket.CloseReason.(CloseReason.java:70) at org.glassfish.tyrus.client.TyrusClientEngine$TyrusReadHandler.handle(TyrusClientEngine.java:734) at org.glassfish.tyrus.container.jdk.client.ClientFilter.processRead(ClientFilter.java:204) at org.glassfish.tyrus.container.jdk.client.Filter.onRead(Filter.java:111) at org.glassfish.tyrus.container.jdk.client.Filter.onRead(Filter.java:113) at org.glassfish.tyrus.container.jdk.client.SslFilter.handleRead(SslFilter.java:384) at org.glassfish.tyrus.container.jdk.client.SslFilter.processRead(SslFilter.java:347) at org.glassfish.tyrus.container.jdk.client.Filter.onRead(Filter.java:111) at org.glassfish.tyrus.container.jdk.client.Filter.onRead(Filter.java:113) at org.glassfish.tyrus.container.jdk.client.TransportFilter$4.completed(TransportFilter.java:294) at org.glassfish.tyrus.container.jdk.client.TransportFilter$4.completed(TransportFilter.java:278) at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:127) at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishRead(UnixAsynchronousSocketChannelImpl.java:439) at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:191) at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:213) at java.base/sun.nio.ch.EPollPort$EventHandlerTask.run(EPollPort.java:306) at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:829)

This is a stacktrace from version 1.18, but the latest master branch seems to have the same issue.

By the look of the code it seems that it appears against the intentions of the code. None of exceptions thrown in this place are expected to trigger such outcome as far as I see.

(https://github.com/eclipse-ee4j/tyrus/blob/master/client/src/main/java/org/glassfish/tyrus/client/TyrusClientEngine.java#L731-L735)

} 
catch (Exception e) {               
   LOGGER.log(Level.FINE, e.getMessage(), e);                
   socket.onClose(new CloseFrame(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, e.getMessage())));
}

https://github.com/javaee/websocket-spec/blob/master/api/client/src/main/java/javax/websocket/CloseReason.java#L69-L70

try {            
     if (reasonPhrase != null && reasonPhrase.getBytes("UTF-8").length > 123) {                
       throw new IllegalArgumentException("Reason Phrase cannot exceed 123 UTF-8 encoded bytes: " + reasonPhrase);            
     }

It looks that the exception handling code should either trim the string for e.getMessage() or create some generic message rather than try to use a string of unpredictable length in the face of such validation in websocket api.