You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
tls: enforce net.io_timeout on synchronous writes - #12884
flb_tls_net_write() currently loops unconditionally on FLB_TLS_WANT_WRITE and FLB_TLS_WANT_READ. Because synchronous TCP sockets configure SO_SNDTIMEO from net.io_timeout, a stalled TLS write (such as out_s3 uploading a request body when the peer stops reading or ACKing) causes SSL_write() to hit EAGAIN, return SSL_ERROR_WANT_WRITE, and retry forever instead of timing out.
This change aligns flb_tls_net_write() with flb_tls_net_read() and flb_tls_net_handshake():
When net.io_timeout > 0, switch the socket to nonblocking mode for the duration of the call, write in 16 KiB TLS-record-sized chunks, and wait for socket readiness via poll()/WSAPoll() against a monotonic millisecond deadline (tls_monotonic_now_ms() / tls_net_wait_ms()).
Refresh the idle deadline when forward progress (ret > 0) is made so healthy multi-megabyte uploads that take longer than net.io_timeout overall continue uninterrupted.
On timeout, record connection->net_error = ETIMEDOUT, set errno = ETIMEDOUT, report the partial byte count written before the stall in *out_len, restore blocking socket mode, and return -1.
Return the total bytes written on success (matching flb_tls_net_write_async() and fd_io_write()) and handle len == 0 without calling the backend.
Add unit tests in tests/internal/upstream_tls.c covering stalled peer write timeouts, slow-draining multi-chunk writes exceeding io_timeout, and zero-length writes.
Enter [N/A] in the box, if an item is not applicable to your change.
Testing
Before we can approve your change; please submit the following in a comment:
Example configuration file for the change
[SERVICE]
Flush 1
Log_Level debug
[INPUT]
Name tail
Path /tmp/input.log
Read_from_Head true
Tag test
[OUTPUT]
Name s3
Match *
bucket local-test
region us-east-1
endpoint https://127.0.0.1:60279
tls On
tls.verify Off
use_put_object On
total_file_size 2M
upload_timeout 1s
store_dir /tmp/s3store
Retry_Limit 3
workers 1
net.keepalive Off
net.io_timeout 2
Debug log output from testing the change
Tested with ./build/bin/fluent-bit against a local HTTPS receiver that stalls after reading the HTTP headers on connection #1 and drains + returns 200 OK on retry:
Before: connection Fix include paths #1 stalled at +1.24s and never timed out or retried within 9s (SIGTERM found s3.0 still blocked in flb_tls_net_write()).
After: connection Fix include paths #1 timed out after 2.00s and retried cleanly:
No actionable comments were generated in the recent review.
Walkthrough
The synchronous TLS write path now applies a deadline when io_timeout is positive. Tests cover zero-length writes, timeout against a stalled peer, and complete writes when peers make slow or sub-record progress.
Changes: Synchronous TLS write timeout
Layer / File(s)
Summary
Deadline-aware TLS writes src/tls/flb_tls.c
Positive io_timeout enables nonblocking writes in chunks, readiness polling, and a deadline refreshed after successful writes or detected BIO progress. The function reports partial progress, restores blocking mode, and sets ETIMEDOUT when the deadline expires.
Tests cover zero-length writes, timeout with partial progress against a stalled peer, and full payload delivery to slow-reading and sub-record-progress peers. The tests check elapsed time and socket mode restoration.
sequenceDiagram
participant Caller
participant flb_tls_net_write
participant TLSBackend
participant SocketPoll
Caller->>flb_tls_net_write: write data with io_timeout
flb_tls_net_write->>TLSBackend: write a chunk
TLSBackend-->>flb_tls_net_write: return bytes or WANT_READ/WANT_WRITE
flb_tls_net_write->>SocketPoll: wait for readiness until deadline
SocketPoll-->>flb_tls_net_write: return readiness or timeout
flb_tls_net_write-->>Caller: return result and bytes written
Loading
Merge Risk: Minimal · up to 4c8e8
Synchronous TLS writes now honor net.io_timeout, so a stalled peer returns ETIMEDOUT with the partial byte count instead of blocking indefinitely. Tests cover the main cases. No concrete merge-blocking risk was found in the supplied evidence.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "tls: enforce net.io_timeout on synchronous writes" matches the main change. The raw summary shows flb_tls_net_write now applies io_timeout to synchronous TLS writes, using nonblocking mode,…
Linked Issues check
[#12742] The issue requires a stalled synchronous TLS request-body write to be bounded by net.io_timeout instead of waiting for the OS TCP timeout. The whole-PR diff (merge base 984519c to head 4c8e8…
Out of Scope Changes check
The whole-PR diff changes src/tls/flb_tls.c and tests/internal/upstream_tls.c. The flb_tls.c changes are the helpers (tls_monotonic_now_ms, tls_net_wait_ms, tls_deadline_after_ms, tls_openssl_bio_prog…
Full details: Title check
Explanation
The title "tls: enforce net.io_timeout on synchronous writes" matches the main change. The raw summary shows flb_tls_net_write now applies io_timeout to synchronous TLS writes, using nonblocking mode, chunked writes, readiness polling, and an ETIMEDOUT deadline. The objectives describe the same goal of stopping stalled synchronous TLS writes from retrying indefinitely. The title is concise, specific, and uses no vague terms.
Full details: Linked Issues check
Explanation
[#12742] The issue requires a stalled synchronous TLS request-body write to be bounded by net.io_timeout instead of waiting for the OS TCP timeout. The whole-PR diff (merge base 984519c to head 4c8e81d) of src/tls/flb_tls.c implements this in flb_tls_net_write(). When io_timeout > 0, the function sets a monotonic deadline (tls_deadline_after_ms), switches the fd to nonblocking mode, and writes in chunks of at most FLB_TLS_SYNC_WRITE_CHUNK_SIZE (16384) bytes. On FLB_TLS_WANT_WRITE or FLB_TLS_WANT_READ, it waits with tls_net_wait_ms() (poll/WSAPoll) against the deadline. On expiry it sets connection->net_error and errno to ETIMEDOUT, reports the partial count in *out_len, restores blocking mode, and returns -1. The deadline refreshes after each positive write and after detected BIO progress, so healthy long uploads continue. The head code also handles len == 0 without calling the backend. The change does not add a universal HTTP timeout, which the issue excludes. Changes since the previous review (9f0d8ce) add BIO-progress-based deadline refresh; the code at head shows this logic, and it is consistent with the requirement. Limits: I did not run the tests. I did not trace the S3 call path to flb_tls_net_write() in this review. The author's reported test results and the local repro are taken as supporting evidence only.
Full details: Out of Scope Changes check
Explanation
The whole-PR diff changes src/tls/flb_tls.c and tests/internal/upstream_tls.c. The flb_tls.c changes are the helpers (tls_monotonic_now_ms, tls_net_wait_ms, tls_deadline_after_ms, tls_openssl_bio_progress, tls_session_io_progress) and the rewritten flb_tls_net_write(). These directly implement the bounded synchronous write objective of #12742. The success return value now returns the total byte count, and len == 0 is handled without a backend call. Both match the PR description and the sibling write functions. The test additions in tests/internal/upstream_tls.c cover stalled-peer timeout, slow-progress multi-chunk success, and zero-length writes, which support the change. I found no unrelated changes. I did not re-read the test file at head in this review.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: defaults
Review profile: CHILL
Plan: Advanced
Run ID: e0eab2f1-0d3c-425e-a0c4-788e3343141d
Commits
Reviewing files that changed from the base of the PR and between 9f0d8ce and 4c8e81d.
Files selected for processing: 2
src/tls/flb_tls.c
tests/internal/upstream_tls.c
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Refresh the idle deadline on partial socket progress
When backpressure makes one 16-KiB TLS record take longer than net.io_timeout to enqueue, the backend can send part of it and still return FLB_TLS_WANT_WRITE. The deadline is only refreshed after a positive backend return, so this wait can time out a connection that is still making socket progress. A Linux harness with io_timeout=1, 4-KiB socket buffers, and a peer draining 1 KiB every 100 ms fails after approximately 1.7 seconds even though bytes were sent only 0.4 seconds before the timeout; the parent completes the same 64-KiB transfer. Refresh the idle deadline from partial backend I/O as well, otherwise slow but active synchronous uploads, including S3 uploads, fail and retry unnecessarily.
The reason will be displayed to describe this comment to others. Learn more.
Good catch — fixed in 80db0be and 4c8e81d. flb_tls_net_write() now checks OpenSSL underlying BIO read/write byte counters (BIO_number_written() / BIO_number_read(), unwrapping BIO_TYPE_SSL for nested TLS) around net_write() and refreshes the idle deadline whenever bytes moved before FLB_TLS_WANT_WRITE / FLB_TLS_WANT_READ. Added tls_sync_write_subrecord_progress_succeeds covering 4 KiB socket buffers drained at 1 KiB per 100 ms with io_timeout = 1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
flb_tls_net_write()currently loops unconditionally onFLB_TLS_WANT_WRITEandFLB_TLS_WANT_READ. Because synchronous TCP sockets configureSO_SNDTIMEOfromnet.io_timeout, a stalled TLS write (such asout_s3uploading a request body when the peer stops reading or ACKing) causesSSL_write()to hitEAGAIN, returnSSL_ERROR_WANT_WRITE, and retry forever instead of timing out.This change aligns
flb_tls_net_write()withflb_tls_net_read()andflb_tls_net_handshake():net.io_timeout > 0, switch the socket to nonblocking mode for the duration of the call, write in 16 KiB TLS-record-sized chunks, and wait for socket readiness viapoll()/WSAPoll()against a monotonic millisecond deadline (tls_monotonic_now_ms()/tls_net_wait_ms()).ret > 0) is made so healthy multi-megabyte uploads that take longer thannet.io_timeoutoverall continue uninterrupted.connection->net_error = ETIMEDOUT, seterrno = ETIMEDOUT, report the partial byte count written before the stall in*out_len, restore blocking socket mode, and return-1.flb_tls_net_write_async()andfd_io_write()) and handlelen == 0without calling the backend.tests/internal/upstream_tls.ccovering stalled peer write timeouts, slow-draining multi-chunk writes exceedingio_timeout, and zero-length writes.Fixes #12742
Enter
[N/A]in the box, if an item is not applicable to your change.Testing
Before we can approve your change; please submit the following in a comment:
Tested with
./build/bin/fluent-bitagainst a local HTTPS receiver that stalls after reading the HTTP headers on connection #1 and drains + returns200 OKon retry:+1.24sand never timed out or retried within 9s (SIGTERMfounds3.0still blocked inflb_tls_net_write()).Unit tests (
ctest --test-dir build -R "^(flb-it-upstream_tls|flb-it-network|flb-it-http_client|flb-it-aws_util)$" --output-on-failure):Tested on macOS ARM64 with
leaks --atExit -- ./build/bin/flb-it-upstream_tls --no-exec tls_sync_write_stalled_peer_timeout tls_sync_write_slow_progress_succeeds tls_sync_write_zero_len:If this is a change to packaging of containers or native binaries then please confirm it works for all targets.
ok-package-testlabel to test for all targets (requires maintainer to do).Documentation
Backporting
Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.
Summary by CodeRabbit