#5549·frp

Control dispatcher ignores write errors and can leave a TCP-mux session alive with no usable work connections

Author: Zheng-Yu7463Created Sep 26, 2026Updated Sep 28, 2026

Bug Description

msg.Dispatcher.sendLoop ignores errors returned by WriteMsg. If the v1 encrypted control stream suffers a write error, golib/crypto.Writer retains that error and returns it on subsequent writes without retrying the underlying transport. The dispatcher continues accepting messages, but messages such as ReqWorkConn are silently lost.

With TCP multiplexing enabled, session keepalives can remain healthy even though the control stream cannot send messages. When explicit frp Ping/Pong heartbeats are disabled, this can leave the process and proxy listeners alive while requests time out waiting for work connections. Restarting frpc creates a fresh control stream and can restore forwarding once the underlying transport is usable.

Expected: close the failed control connection on a message write error and let the existing control lifecycle/reconnect logic recover.

Reproduced in code: a deterministic regression using FRP's real message codec and golib's real encrypted writer fails on unmodified dev at d20a232996007dfe6ab425abc0a39a3ae9a0889b and passes with the proposed fix. The bottom transport fails exactly one write and then accepts writes again; the encrypted writer remains unusable. Clearing the writer's retained error is unsafe because a failed/partial encrypted write may have corrupted the stream.

Production observation: local HTTP backends were healthy, proxy listeners remained up, requests timed out waiting for work connections, and restarting frpc restored forwarding. The initial discarded control-write error was not recorded in the production Info logs, so this report does not claim that every historical outage had the same cause.

frpc Version

0.70.1 when observed in production. The deterministic regression also reproduces on current dev (d20a232996007dfe6ab425abc0a39a3ae9a0889b).

frps Version

0.70.1 when observed in production. The affected dispatcher code is shared by the client and server. The checked v0.71.0 dispatcher also still ignores WriteMsg errors.

System Architecture

linux/amd64 on both ends. Current-dev regression tests use Go 1.25.0, golib v0.8.2, and the race detector. The original v0.70.1 reproduction uses golib v0.8.1.

Configurations

Relevant original settings: TCP transport, TCP multiplexing enabled, token authentication, default TLS, no explicit transport.heartbeatInterval or transport.heartbeatTimeout, and ordinary TCP proxies to local HTTP services.

Representative local-only configuration follows. This configuration by itself does not inject the failure; use the regression test below for deterministic reproduction.

frps.toml:

toml
bindAddr = "127.0.0.1"
bindPort = 17000
proxyBindAddr = "127.0.0.1"
auth.method = "token"
auth.token = "public-test-token-only"

frpc.toml:

toml
serverAddr = "127.0.0.1"
serverPort = 17000
auth.method = "token"
auth.token = "public-test-token-only"
transport.tcpMux = true

[[proxies]]
name = "test-http"
type = "tcp"
localIP = "127.0.0.1"
localPort = 18000
remotePort = 18001

Logs

Representative production symptoms, with identifiers removed:

failed to send message to work connection from pool: connection write timeout
failed to get work connection: timeout trying to get work connection

These messages alone do not identify the swallowed control write. The deterministic regression against the unmodified dispatcher fails with:

TestDispatcherWriteErrorClosesControlAndFreshSessionWorks
    timed out waiting for failed control connection close
--- FAIL: TestDispatcherWriteErrorClosesControlAndFreshSessionWorks

Steps to reproduce

The regression test uses the actual FRP encoder and golib encrypted writer/reader. Only the bottom transport and incoming-message source are controlled. A valid ReqWorkConn is sent and decrypted before the one-shot write failure is injected.

bash
git clone https://github.com/fatedier/frp.git frp-write-error-repro
cd frp-write-error-repro
git checkout d20a232996007dfe6ab425abc0a39a3ae9a0889b
curl -fL 'https://raw.githubusercontent.com/Zheng-Yu7463/frp/ad24600b9534659b3963a37f7dc121dc8c727f74/pkg/msg/handler_write_error_test.go' -o pkg/msg/handler_write_error_test.go

# Fails on the unmodified dispatcher:
go test -race -v ./pkg/msg -run '^TestDispatcherWriteErrorClosesControlAndFreshSessionWorks$'

# Replace only the dispatcher with the proposed fix:
curl -fL 'https://raw.githubusercontent.com/Zheng-Yu7463/frp/ad24600b9534659b3963a37f7dc121dc8c727f74/pkg/msg/handler.go' -o pkg/msg/handler.go

# Passes, including shutdown-ordering and concurrent-failure tests:
go test -race -v ./pkg/msg

The repaired dispatcher closes the failed connection, rejects further sends, and lets its owner perform normal cleanup/reconnect. The test then constructs a fresh control connection and verifies that five valid ReqWorkConn messages are sent and decrypted.

Proposed fix and validation

Handle the first WriteMsg failure by stopping outgoing messages and closing the connection to interrupt ReadMsg. Keep Done owned by the read loop: any currently running synchronous message handler must finish before the control owner can clean up proxies. Closing Done directly from the writer could otherwise race a handler that is still registering a proxy.

The patch and tests are available in this commit.

Validation on the current dev base:

  • The write-error regression fails on the unmodified dispatcher and passes with the fix.
  • go test -race -tags noweb ./pkg/msg ./client/... ./server/... ./pkg/transport/... passes. noweb avoids requiring dashboard assets for these Go package tests.
  • Regression coverage includes the retained crypto-writer error after transport recovery, connection closure, a working fresh stream, synchronous-handler completion ordering, a blocked sender on a full queue, rejection after shutdown, and concurrent read/write errors closing the connection once.

Earlier validation on v0.70.1 included 222 localhost HTTP requests across patched/patched and both mixed-version client/server combinations, plus 72 successful HTTP health checks after deployment. These checks validate forwarding; they do not establish that physical network loss was fixed.

Temporary mitigation: explicitly enable client transport.heartbeatInterval = 10 and transport.heartbeatTimeout = 45 so a stalled control channel can be detected. Handling the write error directly avoids waiting for that timeout.

Related: #5541 changes work-connection refill scheduling; it does not change the dispatcher write-error handling described here.

Affected area

  • Performance and Scalability
  • Others (control connection lifecycle and recovery)