Skip to content

fix(intra): don't discard rwext read/write deadlines when TCP_USER_TIMEOUT is set - #249

Closed
varunagarwal-pro wants to merge 1 commit into
celzero:n2from
varunagarwal-pro:fix/idle-read-hang-forward
Closed

fix(intra): don't discard rwext read/write deadlines when TCP_USER_TIMEOUT is set#249
varunagarwal-pro wants to merge 1 commit into
celzero:n2from
varunagarwal-pro:fix/idle-read-hang-forward

Conversation

@varunagarwal-pro

@varunagarwal-pro varunagarwal-pro commented Sep 12, 2026

Copy link
Copy Markdown

forward() unwraps the remote conn from its rwext deadline wrapper whenever rwext.SetTimeout() returns didSet=true, treating a successfully-applied low-level sockopt as equivalent to a software deadline.

SetTimeout() only sets TCP_USER_TIMEOUT via core.SetTimeoutSockOpt(). TCP_USER_TIMEOUT bounds how long unacknowledged outbound data may go unacked before the kernel force-closes the connection - it has no effect on a blocking Read() simply waiting for more data from a peer that has gone idle without sending RST/FIN. It is not a receive/idle timeout.

Once unwrapped, the only mechanism that could bound such a Read() - rwext's extendr()/extendw(), which apply real per-call SetReadDeadline/SetWriteDeadline via settings.DialerOpts - is discarded entirely. A relayed TCP connection whose peer silently stops sending (idle CDN/load-balancer connection reuse, or a NAT/middlebox timeout that never surfaces an RST) then blocks forward()'s Read() forever. The socket stays ESTABLISHED with zero rx/tx queue activity indefinitely, with no path to recovery short of killing the connection/process.

Reproduced consistently on-device: a live TCP socket to a video CDN entered this idle-ESTABLISHED state with zero queue bytes and never recovered, while process CPU stayed idle (ruling out a busy loop) and DNS/WAN connectivity stayed healthy - pointing squarely at a stuck blocking Read() in the relay path.

Fix: only unwrap remote from rwext when timeoutsecs <= 0 (i.e. no deadline configured at all, so rwext.Read/Write is a true no-op). When a positive timeout is configured, keep remote wrapped so extendr()/extendw() continue enforcing a real per-call deadline in addition to (not instead of) the TCP_USER_TIMEOUT sockopt optimization.

Verified: go build/go vet clean for linux/arm64 (matches Android's kernel ABI for the syscalls touched).

Summary by CodeRabbit

  • Bug Fixes
    • Improved connection forwarding when read/write timeouts are configured.
    • Operations now respect configured per-call deadlines, preventing stalled connections from blocking indefinitely.
    • Preserved optimized connection handling when no timeout is set.

…MEOUT is set

forward() unwrapped the remote conn from its rwext deadline wrapper
whenever rwext.SetTimeout() reported didSet=true, treating a
successfully-applied low-level sockopt as equivalent to having a
software read/write deadline in place.

SetTimeout() only sets TCP_USER_TIMEOUT via core.SetTimeoutSockOpt().
TCP_USER_TIMEOUT bounds how long unacknowledged outbound data may go
unacked before the kernel force-closes the connection - it has no
effect on a blocking Read() that is simply waiting to receive more
data from a peer that has gone idle without sending RST/FIN. It does
not implement a receive/idle timeout.

Once remote was unwrapped, the only mechanism that could bound such a
Read() - rwext's extendr()/extendw(), which apply Go's real per-call
SetReadDeadline/SetWriteDeadline via settings.DialerOpts - was
discarded entirely. As a result, a relayed TCP connection to a peer
that silently stops sending (common with some CDN/load-balancer
behavior on idle keep-alive connections, or after a NAT/middlebox
timeout that never surfaces an RST) blocks forward()'s Read() forever.
The socket stays visibly ESTABLISHED with zero rx/tx queue activity
indefinitely, and the app-level effect is a permanent hang (e.g., a
media player stuck in a buffering state) with no path to recovery
short of killing the connection/process.

This was reproduced consistently on-device: a live TCP socket to a
video CDN would enter this idle-ESTABLISHED state with zero queue
bytes and never recover, while process CPU stayed idle (ruling out a
busy loop) and DNS/WAN connectivity remained healthy throughout -
pointing squarely at a stuck blocking Read() in the relay path.

Fix: only unwrap remote from rwext when timeoutsecs <= 0, i.e. when
no read/write deadline is configured at all and rwext.Read/Write
would be a true no-op. When a positive timeout is configured, keep
remote wrapped so extendr()/extendw() continue to enforce a genuine
per-call deadline in addition to (not instead of) the TCP_USER_TIMEOUT
sockopt optimization.
@coderabbitai

coderabbitai Bot commented Sep 12, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: ccdaf160-af2a-4674-9e87-fe7bc63a1c54

📥 Commits

Reviewing files that changed from the base of the PR and between 8677a52 and 2beeb63.

📒 Files selected for processing (1)
  • intra/common.go

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.

📜 Recent review details
⏰ Context from checks skipped due to timeout. (1)
  • GitHub Check: 🧭 OCR
🔇 Additional comments (1)
intra/common.go (1)

315-325: LGTM!

Also applies to: 328-328


📝 Walkthrough

Walkthrough

The forwarding path now retains the rwext wrapper when timeoutsecs is positive. This preserves per-call read deadlines and prevents a stalled Read() from blocking forward() indefinitely.

Changes

Forwarding timeout handling

Layer / File(s) Summary
Retain wrappers for timed forwarding
intra/common.go
baseHandler.forward unwraps the remote connection only when timeoutsecs <= 0. Timed connections retain rwext so extendr and extendw can enforce per-call deadlines.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: ignoramous

Merge Risk: ⚪ Minimal · up to 2beeb

The change preserves deadline enforcement for timed forwarding and is ready to merge after normal checks.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preserving rwext read/write deadline enforcement when TCP_USER_TIMEOUT is set.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Sep 12, 2026

Copy link
Copy Markdown

🔍 OpenCodeReview found 1 issue(s) in this PR.

  • ✅ Successfully posted inline: 1 comment(s)

Comment thread intra/common.go
if r, ok := remote.(rwext); ok {
isrwext = true
if timeoutsecs, didSet = r.SetTimeout(); didSet || timeoutsecs <= 0 {
if timeoutsecs, didSet = r.SetTimeout(); timeoutsecs <= 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.

[bug · medium]
Dropping the didSet clause means remote now stays wrapped in rwext for every positive-timeout case (previously TCP with a successfully-set sockopt was unwrapped to *net.TCPConn). This has a side effect beyond the intended deadline fix: rwext embeds net.Conn but does not implement core.DuplexCloser/core.TCPConn (no CloseRead/CloseWrite; see the TODO? var _ core.DuplexCloser = (*rwext)(nil) in rwconn.go). Consequently core.CloseOp(remote, core.CopW/CopR) in upload/download falls through to the io.Closer case and performs a full Close() instead of a TCP half-close, which can abort the opposite direction of the relay (e.g. a client that half-closes after its request body while the server is still sending the response). Suggest making rwext implement CloseRead/CloseWrite (delegating to the underlying conn when it supports it) so per-call deadline extension and graceful half-close can coexist.

@varunagarwal-pro

Copy link
Copy Markdown
Author

Consolidating into #250, which contains this fix (rwext read/write deadline preservation) together with the proxyFor lock-read timeout fix, for a single combined, easier-to-review PR. Closing this one in favor of #250.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant