Skip to content

Ports: a published TCP connection stays open however long it is quiet (#388) - #406

Open
sorenhoang wants to merge 1 commit into
libracoder:mainfrom
sorenhoang:fix/388-idle-tcp-ports
Open

sorenhoang wants to merge 1 commit into
libracoder:mainfrom
sorenhoang:fix/388-idle-tcp-ports

Conversation

@sorenhoang

@sorenhoang sorenhoang commented Nov 16, 2026 •

Copy link
Copy Markdown

What this changes and why

Every socket the port relay uses (crates/kiri/src/daemon/ports.rs) had a 300 s read timeout (TCP_IDLE). A published TCP connection with no traffic in one direction for five minutes was therefore closed: the read failed, the copy loop ended and the relay shut the connection down. Docker's docker-proxy and DNAT never close a quiet connection, and Valkey with timeout 0, database pools on the host and .NET Aspire's tunnel rely on that, as the issue shows. The same timeout also cut a long upload to a server that sends nothing back for five minutes.

  • The read timeout is gone on all three paths: vsock (relay), the NAT network (relay_tcp) and the Linux host (relay_direct).
  • Every relayed TCP socket gets keepalive with Go's defaults, which docker-proxy runs with: 15 s idle, 15 s interval, 9 probes. A peer that went away without closing (a laptop that slept, a LAN host that left) is still found, and its session permit is freed.
  • A direction that ends in an error now shuts the other socket down both ways (pass_on) instead of only half-closing it. Without a read deadline, the opposite direction would otherwise wait forever on a quiet peer and hold the session. A clean end of stream is still passed on as a half-close, as before.
  • The 300 s write timeout stays (renamed TCP_STALL), for a peer that takes no bytes at all.

Fixes #388

How you tested it

$ cargo test -p kiri --bin kiri ports::
test daemon::ports::tests::relayed_tcp_waits_out_quiet_peers_and_probes_for_gone_ones ... ok
...
test result: ok. 11 passed; 0 failed
$ cargo test --workspace   # passes on main and on this branch

The new test checks that a relayed socket has no read timeout, has the write timeout and has keepalive on with the 15 s idle time. I wrote it first and saw it fail.

Not yet tested on a running Kiri. I haven't run the issue's reproduction (valkey/valkey:9.1 published on 127.0.0.1, PING after 320 s of silence) before and after this change. In particular, a full shutdown waking a read blocked on the vsock stream has only been reasoned about, not observed. I'll add that output here once I have a guest build running. The Linux-only constants (TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT) exist in the locked libc 0.2.191, but I couldn't cross-build the Linux host here.

Tested on: macOS (Apple silicon), unit tests only

How you found it

  • I use Kiri and ran into this myself
  • I found it by reading the code or comparing with Docker
  • Someone else reported it (link the issue above)

AI tools used to write this change: yes. Claude Code helped trace the cause, write the change and the test, and draft this description; I reviewed it.

Checklist

  • I have at most 3 pull requests open on Kiri, this one included
  • cargo fmt, cargo clippy and the tests pass. No new clippy warnings. New code follows the file's existing formatting, since running cargo fmt on ports.rs would reformat lines this change doesn't touch.
  • It works on macOS, Windows and Linux, or the change only touches one of them. The same code runs on all three; only the unit test has run so far.

…libracoder#388)

Every relayed socket had a 300 s read timeout, so a connection with no
traffic in one direction for five minutes was closed. Docker's
docker-proxy and DNAT never close a quiet connection, and clients such as
database pools and .NET Aspire's tunnel rely on that.

The read timeout goes. Keepalive (Go's defaults, which docker-proxy runs
with: 15 s idle, 15 s interval, 9 probes) now finds a peer that went
away, and a direction that ends in an error shuts the other socket down
both ways, so the opposite direction never waits on a quiet peer forever
and the session is still freed. The 300 s write timeout stays, for a peer
that takes no bytes at all.
@sorenhoang
sorenhoang force-pushed the fix/388-idle-tcp-ports branch from 8d22233 to 30f7504 Compare November 16, 2026 19:23
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.

Published ports close TCP connections after ~300 s of inactivity

1 participant