Skip to content

Fix TestTunnelE2ECLI and properly close listener in tunnelTraffic#6024

Open
benjirewis wants to merge 2 commits into
viamrobotics:mainfrom
benjirewis:tunnel-fixes
Open

Fix TestTunnelE2ECLI and properly close listener in tunnelTraffic#6024
benjirewis wants to merge 2 commits into
viamrobotics:mainfrom
benjirewis:tunnel-fixes

Conversation

@benjirewis
Copy link
Copy Markdown
Member

RSDK-13993
RSDK-13994

What

Fixes two bugs in the CLI tunnel implementation and its test:

tunnelTraffic couldn't be stopped via context cancellation. The accept loop checks ctx.Err() only at the top of each iteration, but net.Listener.Accept() blocks indefinitely. Cancelling the context had no effect while the listener was waiting for a new connection. Fixed by spawning a goroutine that closes the listener when the context is done, which causes Accept() to return an error and lets the loop exit cleanly.

TestTunnelE2ECLI wasn't actually testing the tunnel. sourcePort and destPort were both set to 23657. Since the destination listener was already bound to localhost:23657 before tunnelTraffic started, the CLI listener failed to bind the same port (silently, in a goroutine). The test then dialed localhost:23657 and connected directly to the destination listener, bypassing the tunnel entirely and passing vacuously. Fixed by setting sourcePort to 23659 (matching the port shown in the test's own comment) and using testutils.WaitForAssertion to retry the dial until tunnelTraffic's listener is ready.

Why

The code fix makes tunnelTraffic behave correctly when a user cancels a tunnel session — without it, the CLI listener would hang indefinitely after cancellation. In production, this wasn't a huge deal since the OS (at least in my testing) seems to reclaim the port after the CLI is ctrl-ced 🤷🏻 .

The test fix ensures we actually have coverage of the CLI tunnel path rather than a test that silently passes without exercising anything.

Testing

TestTunnelE2ECLI now passes and exercises the full tunnel path: test-process <-> source-listener(localhost:23659) <-> machine(localhost:23658) <-> dest-listener(localhost:23657). I also manually verified that viam machine part tunnel still works as expected.

@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label May 19, 2026
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels May 19, 2026
@allisonschiang
Copy link
Copy Markdown
Member

allisonschiang commented May 19, 2026

    client_test.go:1638: 2026-05-19T14:30:34.120-0400   INFO    TestTunnelE2ECLI        cli/client_test.go:1638 Listening on localhost:23657 for tunnel message
    dial.go:188: 2026-05-19T14:30:34.121-0400   DEBUG   TestTunnelE2ECLI.networking.webrtc      rpc/dial.go:188 trying WebRTC   {"signaling_server":"localhost:23658","host
":"localhost:23658"}
    wrtc_client.go:133: 2026-05-19T14:30:34.121-0400    DEBUG   TestTunnelE2ECLI.networking.webrtc      rpc/wrtc_client.go:133  connecting to signaling server  {"signaling
_server":"localhost:23658","host":"localhost:23658"}
2026-05-19T18:30:34.124Z        INFO    rdk.config      config/logging_level.go:45      Log level initialized: info
2026-05-19T18:30:34.131Z        INFO    rdk     server/entrypoint.go:88 viam-server built from source; version unknown
2026-05-19T18:30:34.131Z        INFO    rdk     utils/env.go:219        Started with the following Viam environment variables   {"environment":["HOME=C:\\Users\\admin"]}
2026-05-19T18:30:34.132Z        INFO    rdk.config      server/entrypoint.go:554        Processing initial robot config...
2026-05-19T18:30:34.132Z        INFO    rdk     config/config.go:1236   Noisy log deduplication is now enabled
2026-05-19T18:30:34.133Z        INFO    rdk.framesystem framesystem/framesystem.go:213  Reconfigured. Known components: map[]
2026-05-19T18:30:34.135Z        INFO    rdk.config      server/entrypoint.go:635        Robot created with minimal config       {"time_to_create":"2.6035ms"}
2026-05-19T18:30:34.135Z        INFO    rdk.config      server/entrypoint.go:669        Config watcher started
2026-05-19T18:30:34.135Z        INFO    rdk.config      server/entrypoint.go:389        Robot constructed with full config      {"time_to_construct":"531.2µs"}
2026-05-19T18:30:34.139Z        WARN    rdk.networking  rpc/server.go:550       mDNS setup failed; continuing with mDNS disabled        {"error":"no positive MTU found"}
2026-05-19T18:30:34.139Z        INFO    rdk.networking  rpc/server.go:724       Running internal signaling      {"signaling_address":"127.0.0.1:56937","for_hosts":["c3b2bf
33-7dec-43a5-a6b3-5d9393b48240","127.0.0.1:23658","localhost:23658"]}
2026-05-19T18:30:34.140Z        INFO    rdk     web/web.go:560  serving {"url":"http://127.0.0.1:23658"}
    dialer.go:418: 2026-05-19T14:30:44.123-0400 WARN    TestTunnelE2ECLI.networking     rpc/dialer.go:418       downgrading from TLS to plaintext       {"address":"localho
st:23658","with_credentials":false}
    client.go:495: 2026-05-19T14:30:44.127-0400 INFO    TestTunnelE2ECLI        client/client.go:495    successfully (re)connected to remote at address {"address":"localho
st:23658"}
    client.go:1360: 2026-05-19T14:30:44.182-0400        INFO    TestTunnelE2ECLI        client/client.go:1360   creating tunnel to server       {"port":23657}
2026-05-19T18:30:44.183Z        INFO    rdk     server/server.go:104    dialing to destination port     {"port":"23657","timeout":10000000000}
2026-05-19T18:30:44.185Z        INFO    rdk     server/server.go:109    successfully dialed to destination port, creating tunnel        {"port":"23657"}
    client_test.go:1650: 2026-05-19T14:30:44.185-0400   INFO    TestTunnelE2ECLI        cli/client_test.go:1650 Received expected tunnel message at localhost:23657
2026-05-19T18:30:44.186Z        INFO    rdk.networking  rpc/server.go:929       stopping
2026-05-19T18:30:44.186Z        INFO    rdk     web/web.go:197  viam-server shutting down
    tunnel.go:137: 2026-05-19T14:30:44.186-0400 DEBUG   TestTunnelE2ECLI        tunnel/tunnel.go:137    exiting receiver/writer loop    {"loop":"recv/writer"}
    tunnel.go:27: 2026-05-19T14:30:44.187-0400  DEBUG   TestTunnelE2ECLI        tunnel/tunnel.go:27     ignoring non-anomalous error    {"error":"read tcp 127.0.0.1:23659-
>127.0.0.1:56960: use of closed network connection","loop":"reader/sender"}
    tunnel.go:92: 2026-05-19T14:30:44.187-0400  DEBUG   TestTunnelE2ECLI        tunnel/tunnel.go:92     exiting reader/sender loop      {"loop":"reader/sender"}
2026-05-19T18:30:44.187Z        INFO    rdk     server/server.go:141    tunnel to client closed {"port":"23657"}
2026-05-19T18:30:44.187Z        INFO    rdk.networking  rpc/wrtc_server.go:120  waiting for handlers to complete
2026-05-19T18:30:44.187Z        INFO    rdk.networking  rpc/wrtc_server.go:125  handlers complete
2026-05-19T18:30:44.187Z        INFO    rdk.networking  rpc/wrtc_server.go:126  closing lingering peer connections
2026-05-19T18:30:44.187Z        INFO    rdk.networking  rpc/wrtc_server.go:144  lingering peer connections closed
2026-05-19T18:30:44.187Z        INFO    rdk.networking  rpc/server.go:961       stopped cleanly
    client.go:1418: 2026-05-19T14:30:44.187-0400        INFO    TestTunnelE2ECLI        client/client.go:1418   tunnel to server closed {"port":23657}
2026-05-19T18:30:44.188Z        INFO    rdk.resource_manager    impl/resource_manager.go:558    Now removing resource   {"resource":"rdk-internal:service:frame_system/buil
tin"}
2026-05-19T18:30:44.188Z        INFO    rdk.resource_manager    impl/resource_manager.go:558    Now removing resource   {"resource":"rdk-internal:service:packagemanager/bu
iltin"}
2026-05-19T18:30:44.188Z        INFO    rdk.resource_manager    impl/resource_manager.go:558    Now removing resource   {"resource":"rdk-internal:service:cloud_connection/
builtin"}
2026-05-19T18:30:44.188Z        INFO    rdk.job_manager jobmanager/jobmanager.go:164    JobManager is shutting down.
--- PASS: TestTunnelE2ECLI (10.07s)
PASS
ok      go.viam.com/rdk/cli     10.467s

Copy link
Copy Markdown
Member

@allisonschiang allisonschiang left a comment

Choose a reason for hiding this comment

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

lgtm in terms of it passes on windows, didn't look into implementation!

@viamrobotics-overwatch
Copy link
Copy Markdown

Hey @benjirewis — this PR has been approved and CI has been green for 3+ business days. Ready to merge?

Auto-comment from overwatch. Will not re-nudge for 7 days.

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

Labels

safe to test This pull request is marked safe to test from a trusted zone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants