Skip to content

tls: enforce net.io_timeout on synchronous writes - #12884

Open
aarushvan wants to merge 4 commits into
fluent:masterfrom
aarushvan:fix/tls-sync-write-timeout
Open

aarushvan wants to merge 4 commits into
fluent:masterfrom
aarushvan:fix/tls-sync-write-timeout

Conversation

@aarushvan

@aarushvan aarushvan commented Dec 1, 2026 •

Copy link
Copy Markdown

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.

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:

  • 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:
[2026/12/01 13:12:59.851] [debug] [http_client] not using http_proxy for header
[2026/12/01 13:13:01.854] [error] [src/flb_http_client.c:1953 errno=60] Operation timed out
[2026/12/01 13:13:01.854] [debug] [aws_client] 127.0.0.1: http_do=-1, HTTP Status: 0
[2026/12/01 13:13:01.854] [debug] [aws_client] auto-retrying
[2026/12/01 13:13:07.988] [debug] [output:s3:s3.0] PutObject http status=200
[2026/12/01 13:13:07.988] [ info] [output:s3:s3.0] Successfully uploaded object /fluent-bit-logs/test/2026/12/01/07/42/58-objecthDc5YQgG

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):

1/4 Test #124: flb-it-network ...................   Passed    4.82 sec
2/4 Test #128: flb-it-http_client ...............   Passed    0.13 sec
3/4 Test #184: flb-it-upstream_tls ..............   Passed    4.10 sec
4/4 Test #204: flb-it-aws_util ..................   Passed    0.13 sec
  • [N/A] Attached Valgrind output that shows no leaks or memory corruption was found

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:

Process 26710: 10011 nodes malloced for 916 KB
Process 26710: 0 leaks for 0 total leaked bytes.

If this is a change to packaging of containers or native binaries then please confirm it works for all targets.

  • [N/A] Run local packaging test showing all targets (including any new ones) build.
  • [N/A] Set ok-package-test label to test for all targets (requires maintainer to do).

Documentation

  • [N/A] Documentation required for this feature

Backporting

  • Backport to latest stable release.

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

  • Bug Fixes
    • TLS writes now respect configured timeouts, including when a peer stops reading, while allowing progress on slow transfers.
    • Zero-length writes complete successfully, and partial write progress is reported when a write times out.
    • Transfers that continue making progress can finish without being cut short by the timeout.

Signed-off-by: Aarush Vankadari <aarush.mitmpl2026@learner.manipal.edu>
Signed-off-by: Aarush Vankadari <aarush.mitmpl2026@learner.manipal.edu>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-01T08:08:17.335334Z 9f0d8ce PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

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.
TLS write behavior tests
tests/internal/upstream_tls.c
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.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 Passed [#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 Passed 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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9f0d8cedc8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/tls/flb_tls.c Outdated
Comment on lines +1040 to +1041
if (ret == FLB_TLS_WANT_WRITE || ret == FLB_TLS_WANT_READ) {
if (bounded && tls_net_wait_ms(session, ret, deadline_ms) != 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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.

Signed-off-by: Aarush Vankadari <aarush.mitmpl2026@learner.manipal.edu>
Signed-off-by: Aarush Vankadari <aarush.mitmpl2026@learner.manipal.edu>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

out_s3: stalled TLS request-body writes are not bounded by net.io_timeout (5.0.4)

1 participant