shenyu icon indicating copy to clipboard operation
shenyu copied to clipboard

WebSocket proxied connections are closed after ~3 seconds of silence: 3s idle/read/write timeout handlers are installed on the shared HttpClient reused by WebSocketPlugin

Open Chrisp12138 opened this issue 3 weeks ago • 0 comments

Environment

  • ShenYu version: 2.7.2-release (also present on master as of 2.7.x)
  • Deployment: shenyu-bootstrap acting as a WebSocket proxy (websocket plugin, upstream is a Tomcat/Spring-WebSocket service)
  • Client: browser WebSocket

What happens

When a client connects through the gateway's websocket plugin, the proxied connection is silently closed roughly 3 seconds after the last message in either direction. The browser reports onclose with code=1005 ("no status received", a close frame without a code), and there is no error log anywhere in the bootstrap.

Reproduction (very reliable):

  1. Register a websocket plugin selector/rule proxying to any WS upstream.
  2. Connect a client to ws://gateway:9195/<ws-path> and simply stay idle (no client message, no server push for ~3s).
  3. The connection is closed at ~3s. Send anything (e.g. a heartbeat every 15s) and the connection still dies 3s after the last message.

Any long-lived ws session whose backend does not push within 3 seconds — file upload progress channels, notification streams, chat — cannot work behind the gateway's websocket plugin, unless the client heartbeat interval is shorter than 3 seconds.

Root cause

HttpClientProperties (shenyu-plugin-httpclient) defaults every timeout to 3 seconds:

connectTimeout   = 45000
responseTimeout  = 3000
readerIdleTime   = 3000
writerIdleTime   = 3000
allIdleTime      = 3000
readTimeout      = 3000
writeTimeout     = 3000

HttpClientFactory#createInstance() installs them on every connection:

httpClient = httpClient.doOnConnected(connection -> {
    connection.addHandlerLast(new IdleStateHandler(readerIdleTime, writerIdleTime, allIdleTime, MS));
    connection.addHandlerLast(new WriteTimeoutHandler(writeTimeout, MS));   // unconditional
    if (readTimeout > 0) {
        connection.addHandlerLast(new ReadTimeoutHandler(readTimeout, MS));
    }
});

Meanwhile WebSocketPluginConfiguration#reactorNettyWebSocketClient(...) builds the websocket forwarding client by reusing that very same HttpClient bean:

return new ReactorNettyWebSocketClient(httpClient.getIfAvailable(HttpClient::create), builder);

So every proxied websocket connection inherits the HTTP short-request timeout semantics: 3s of silence in the upstream-to-client direction trips the idle/read handlers, 3s of silence in the client-to-upstream direction trips the write handler, and the connection is torn down. Nothing in the docs warns about this, and the close arrives as a code-less close frame (client sees 1005), which makes the symptom look like a network fault rather than a configuration default.

Expected behavior / suggestions

Websocket proxying has fundamentally different lifetime semantics from a short HTTP request; the same 3s defaults should not apply. Possible directions:

  1. Do not reuse the shared HttpClient for the websocket client — build the ReactorNettyWebSocketClient from an idle/read/write-timeout-free HttpClient (or expose dedicated knobs, e.g. shenyu.websocket.idleTimeout, defaulting to something sane like 120s).
  2. And/or relax/document the defaults — a 3s read/write timeout as a global default is aggressive even for plain HTTP proxying (slow endpoints/streaming responses are affected the same way), so at minimum the yml template and the docs should warn about it.
  3. Map the propagated close status faithfully (e.g. surface a 1011 or at least log the trigger) so operators can tell a gateway-imposed timeout from a network failure — today the gateway closes silently with no log at all.

Workaround

Explicitly widen the values in bootstrap application.yml:

shenyu:
  httpclient:
    strategy: netty
    responseTimeout: 60000
    readerIdleTime: 120000   # aligned with a 15s client heartbeat
    writerIdleTime: 0        # 0 disables
    allIdleTime: 0
    readTimeout: 0
    writeTimeout: 0

With this config, ws sessions survive long silent periods and a large upload flow over ws works end-to-end. It would be great to have the ws proxy not depend on such workarounds.

Related

We hit two websocket-proxying issues in the same integration. The other one — websocket plugin rules matching only the registered path instead of its sub-paths (e.g. a rule path of /data/asset not matching /data/asset/ws/access/upload, because the client registration writes an exact-path rule) — is described in #8372 and addressed by PR #8373.

Chrisp12138 avatar Nov 09 '26 10:11 Chrisp12138