[ 
https://issues.apache.org/jira/browse/CAMEL-27569?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Rich T updated CAMEL-27569:
---------------------------
    Description: 
The undertow endpoint option `sendTimeout` is documented as the timeout for 
sending to websocket channels. {{CamelWebSocketHandler.send(WebSocketChannel, 
Object, ExtendedWebSocketCallback, long timeoutMillis)}} applies it only to 
binary messages:
{code:java}
if (message instanceof String)
{     WebSockets.sendText((String) message, channel, callback);                 
// timeout not passed }
else if (message instanceof byte[])
{     WebSockets.sendBinary(buffer, channel, callback, timeoutMillis); } else 
if (message instanceof Reader) {     
WebSockets.sendText(IOConverter.toString(r), channel, callback);          // 
timeout not passed } else if (message instanceof InputStream) {     
WebSockets.sendBinary(buffer, channel, callback, timeoutMillis); }
{code}
 

Text messages, which are the common case for websocket producers, therefore get 
no send timeout at all, whatever the endpoint is configured with.

 

With a slow or stalled websocket peer, text sends queue up in Undertow 
indefinitely. The exchange and its callbacks stay referenced until the write 
completes, so memory grows with every message. In our case, ~1.1 million 
pending sends filled a 4.4 GB heap. Users who set {{sendTimeout }}expecting a 
bound on this don't get one.
h3. Suggested fix

Pass {{timeoutMillis }}in both text branches:

 
{code:java}
WebSockets.sendText((String) message, channel, callback, timeoutMillis);
...
WebSockets.sendText(IOConverter.toString(r), channel, callback, 
timeoutMillis);{code}
>From reading the Undertow 2.3.26 source (not yet tested), applying the timeout 
>would in practice close a stalled connection about {{sendTimeout}} after it 
>stalls. The timed-out frame's sink is closed, and the next flush pass hits 
>that closed frame and tears down the connection. So this one-line change would 
>likely have prevented the OOM above.

 

Caveat for the producer's error reporting: Undertow reports a timed-out frame 
to the callback as complete()`, not `onError()`, so `MultiCallback` will mark 
the exchange successful even though the message was dropped. That's an Undertow 
issue (UNDERTOW-XXXX, link once filed), but it may be worth noting in the 
`sendTimeout` documentation.
 # 
 ## Related (separate improvement, optional)

`CamelWebSocketHandler.send(Predicate, ...)` finds the target peer with 
`delegate.getPeerConnections().stream().filter(peerFilter)` on every send. For 
a `CONNECTION_KEY` target that scans every connected peer for each message, so 
fan-out costs O(connections²) per broadcast event. A map from connection key to 
channel would make single-peer sends O(1).

  was:
The undertow endpoint option `sendTimeout` is documented as the timeout for 
sending to websocket channels. `CamelWebSocketHandler.send(WebSocketChannel, 
Object, ExtendedWebSocketCallback, long timeoutMillis)` applies it only to 
binary messages:

```java
if (message instanceof String) {
    WebSockets.sendText((String) message, channel, callback);                 
// timeout not passed
} else if (message instanceof byte[]) {
    WebSockets.sendBinary(buffer, channel, callback, timeoutMillis);
} else if (message instanceof Reader) {
    WebSockets.sendText(IOConverter.toString(r), channel, callback);          
// timeout not passed
} else if (message instanceof InputStream) {
    WebSockets.sendBinary(buffer, channel, callback, timeoutMillis);
}
```

Text messages, which are the common case for websocket producers, therefore get 
no send timeout at all, whatever the endpoint is configured with.

## Impact

With a slow or stalled websocket peer, text sends queue up in Undertow 
indefinitely. The exchange and its callbacks stay referenced until the write 
completes, so memory grows with every message. In our case, ~1.1 million 
pending sends filled a 4.4 GB heap. Users who set `sendTimeout` expecting a 
bound on this don't get one.

## Suggested fix

Pass `timeoutMillis` in both text branches:

```java
WebSockets.sendText((String) message, channel, callback, timeoutMillis);
...
WebSockets.sendText(IOConverter.toString(r), channel, callback, timeoutMillis);
```

>From reading the Undertow 2.3.26 source (not yet tested), applying the timeout 
>would in practice close a stalled connection about `sendTimeout` after it 
>stalls. The timed-out frame's sink is closed, and the next flush pass hits 
>that closed frame and tears down the connection. So this one-line change would 
>likely have prevented the OOM above.

Caveat for the producer's error reporting: Undertow reports a timed-out frame 
to the callback as `complete()`, not `onError()`, so `MultiCallback` will mark 
the exchange successful even though the message was dropped. That's an Undertow 
issue (UNDERTOW-XXXX, link once filed), but it may be worth noting in the 
`sendTimeout` documentation.

## Related (separate improvement, optional)

`CamelWebSocketHandler.send(Predicate, ...)` finds the target peer with 
`delegate.getPeerConnections().stream().filter(peerFilter)` on every send. For 
a `CONNECTION_KEY` target that scans every connected peer for each message, so 
fan-out costs O(connections²) per broadcast event. A map from connection key to 
channel would make single-peer sends O(1).


> sendTimeout is ignored for String and Reader websocket messages
> ---------------------------------------------------------------
>
>                 Key: CAMEL-27569
>                 URL: https://issues.apache.org/jira/browse/CAMEL-27569
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-undertow
>    Affects Versions: 4.22.0
>            Reporter: Rich T
>            Priority: Major
>
> The undertow endpoint option `sendTimeout` is documented as the timeout for 
> sending to websocket channels. {{CamelWebSocketHandler.send(WebSocketChannel, 
> Object, ExtendedWebSocketCallback, long timeoutMillis)}} applies it only to 
> binary messages:
> {code:java}
> if (message instanceof String)
> {     WebSockets.sendText((String) message, channel, callback);               
>   // timeout not passed }
> else if (message instanceof byte[])
> {     WebSockets.sendBinary(buffer, channel, callback, timeoutMillis); } else 
> if (message instanceof Reader) {     
> WebSockets.sendText(IOConverter.toString(r), channel, callback);          // 
> timeout not passed } else if (message instanceof InputStream) {     
> WebSockets.sendBinary(buffer, channel, callback, timeoutMillis); }
> {code}
>  
> Text messages, which are the common case for websocket producers, therefore 
> get no send timeout at all, whatever the endpoint is configured with.
>  
> With a slow or stalled websocket peer, text sends queue up in Undertow 
> indefinitely. The exchange and its callbacks stay referenced until the write 
> completes, so memory grows with every message. In our case, ~1.1 million 
> pending sends filled a 4.4 GB heap. Users who set {{sendTimeout }}expecting a 
> bound on this don't get one.
> h3. Suggested fix
> Pass {{timeoutMillis }}in both text branches:
>  
> {code:java}
> WebSockets.sendText((String) message, channel, callback, timeoutMillis);
> ...
> WebSockets.sendText(IOConverter.toString(r), channel, callback, 
> timeoutMillis);{code}
> From reading the Undertow 2.3.26 source (not yet tested), applying the 
> timeout would in practice close a stalled connection about {{sendTimeout}} 
> after it stalls. The timed-out frame's sink is closed, and the next flush 
> pass hits that closed frame and tears down the connection. So this one-line 
> change would likely have prevented the OOM above.
>  
> Caveat for the producer's error reporting: Undertow reports a timed-out frame 
> to the callback as complete()`, not `onError()`, so `MultiCallback` will mark 
> the exchange successful even though the message was dropped. That's an 
> Undertow issue (UNDERTOW-XXXX, link once filed), but it may be worth noting 
> in the `sendTimeout` documentation.
>  # 
>  ## Related (separate improvement, optional)
> `CamelWebSocketHandler.send(Predicate, ...)` finds the target peer with 
> `delegate.getPeerConnections().stream().filter(peerFilter)` on every send. 
> For a `CONNECTION_KEY` target that scans every connected peer for each 
> message, so fan-out costs O(connections²) per broadcast event. A map from 
> connection key to channel would make single-peer sends O(1).



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to