Skip to content

out_forward: stop a send after send_timeout when the server does not read - #5682

Merged
kenhys merged 4 commits into
fluent:masterfrom
Watson1978:out-forward-send-timeout-bounds-write
Dec 1, 2026
Merged

kenhys merged 4 commits into
fluent:masterfrom
Watson1978:out-forward-send-timeout-bounds-write

Conversation

@Watson1978

@Watson1978 Watson1978 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Which issue(s) this PR fixes:
Fixes #5680

What this PR does / why we need it:
send_timeout was only passed to SO_SNDTIMEO, which does not end a write of Ruby, so a send to a server that stopped reading blocked the flush thread until the server read again or closed the connection. The send now uses write_nonblock and fails with SendTimeoutError when it makes no progress for send_timeout. It is not a limit on the whole send, and a server that reads very slowly can also fail. send_timeout 0 still means no timeout.

IO.copy_stream is replaced by a read and write loop, so the file buffer with plain TCP no longer uses sendfile. Sending a 512 MiB chunk to in_forward on the same host took about 10% longer on my machine (0.47s to 0.52s). The memory buffer was not slower.

SO_LINGER (= send_timeout) stays on the transfer socket, so close still waits until the server has the data, as before. Two things around it change. A plain TCP socket is now closed after shutdown(:WR) and not after close_write, also in the keepalive cache. Ruby keeps the GVL while it closes a socket after close_write, so that wait could stop every other thread, and with the new write loop it did so on every send to a receiver in the same process. Windows also needs the shutdown to close a non-blocking socket that has SO_LINGER and unsent data. And a send_timeout of 0 or below 1 second set SO_LINGER to 0 seconds (not on Windows), which resets the connection on close and can drop data. Now 0 sets no SO_LINGER, and a value below 1 second sets 1 second.

A timed out socket does not wait for an ACK or for SO_LINGER. Without keepalive it is reset, because it only holds the chunk that is sent again. With require_ack_response, its node is also disabled at once, as after other failed sends. A keepalive socket is closed normally by the purge timer, after its unread data is dropped, because it may still hold earlier chunks. Its unsent data then stays in the kernel until the server reads or TCP gives up. Without require_ack_response, an earlier chunk on it can still be lost with TLS 1.3, when data from the server arrives after the close. That can already happen without keepalive.

Docs Changes:
fluent/fluentd-docs-gitbook#731

Release Note:
out_forward: stop a send after send_timeout when the server does not read. send_timeout had no effect on a send before, so a small value can now fail sends to a slow server.

🤖 Generated with Claude Code

@Watson1978 Watson1978 added bug Something isn't working backport to v1.19 We will backport this fix to the LTS branch labels Nov 26, 2026
@Watson1978 Watson1978 added this to the v1.21.0 milestone Nov 26, 2026
…read

send_timeout was only passed to SO_SNDTIMEO, which does not end
IO#write or IO.copy_stream. A send to a server that stopped reading
blocked the flush thread.

* Send with write_nonblock and raise SendTimeoutError when the send
  makes no progress for send_timeout. 0 still means no timeout
* Do not wait for an ACK or for SO_LINGER on a timed out socket. Reset
  it without keepalive, and close it normally with keepalive, because
  it may still hold earlier chunks
* Drop unread data before a failed keepalive socket is closed, so that
  close does not reset the connection
* Close a TCP socket after shutdown(:WR), not after close_write. Ruby
  keeps the GVL while such a close waits for SO_LINGER, and Windows
  cannot close a non-blocking socket with SO_LINGER and unsent data
* Set no SO_LINGER for send_timeout 0, and 1 second for a value below
  1 second. SO_LINGER of 0 seconds resets the connection on close

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
@Watson1978
Watson1978 force-pushed the out-forward-send-timeout-bounds-write branch from 152a03f to b65ed09 Compare November 26, 2026 01:58
@Watson1978
Watson1978 marked this pull request as ready for review November 26, 2026 01:59
@Watson1978
Watson1978 requested a review from kenhys November 26, 2026 03:19
Comment thread lib/fluent/plugin/out_forward.rb Resolved
* Run the tests for a server that does not read, and for send_timeout 0,
  with TLS too
* Cover the wait for a readable socket, which a TLS write can ask for

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
Watson1978 and others added 2 commits November 30, 2026 20:40
…esponse

A timed out send skipped the ack check, so its node stayed available,
unlike after other failed sends. Disable the node at once. Waiting for
the ack check would do it only after ack_response_timeout, because the
server keeps the connection open without reading.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
… comment

Waiting for the ACK of a timed out send can block the ACK thread with
TLS 1.3 when the server starts reading late.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
@kenhys
kenhys merged commit d61897c into fluent:master Dec 1, 2026
20 checks passed
kenhys pushed a commit to Watson1978/fluentd-docs-gitbook that referenced this pull request Dec 1, 2026
send_timeout is a limit on the time without progress, not on the whole
send. fluent/fluentd#5682 makes it stop a send, so describe what it
limits and what changes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
@Watson1978
Watson1978 deleted the out-forward-send-timeout-bounds-write branch December 1, 2026 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport to v1.19 We will backport this fix to the LTS branch bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

out_forward: send_timeout does not stop a write to a destination that stopped reading, and the flush thread stays blocked

2 participants