Skip to content

tunnel: report a connector that exits on its own, keep quick tunnels on their URL - #949

Open
ersinkoc wants to merge 2 commits into
google:mainfrom
ersinkoc:audit/tunnel
Open

ersinkoc wants to merge 2 commits into
google:mainfrom
ersinkoc:audit/tunnel

Conversation

@ersinkoc

Copy link
Copy Markdown

Two Cloudflare tunnel fixes, each with a regression test that failed on the previous code.

  • Report a connector that exits on its own. Tunnel.Err is documented as "nil after Close", but a cloudflared that exited with status 0 without Close also left it nil, so agentmesh-one's watcher said nothing when the public URL died, and Open wrapped the nil into cloudflared exited before publishing a URL: %!w(<nil>). Any exit not caused by Close is now an error. Because the connector shares the terminal's process group, Ctrl-C stops it too, so the watcher in agentmesh-one now returns quietly once shutdown has begun instead of logging that exit.
  • Keep a quick tunnel on its assigned URL. ExternalURL names the hostname of a named tunnel and applies only with a Token, but it also switched the output parser to named-tunnel mode for a quick tunnel, which then published ExternalURL instead of its trycloudflare.com URL. The CLI already refuses that flag combination; this fixes the exported API.

go test -race ./internal/tunnel/ ./cmd/agentmesh-one/ and go vet pass on Linux.

…els on their URL

A cloudflared that exited with status 0 without Close left Err nil, the
value Close leaves, so agentmesh-one's watcher said nothing when the
public URL died, and Open wrapped the nil into
"cloudflared exited before publishing a URL: %!w(<nil>)". Any exit not
caused by Close is now an error. The watcher in agentmesh-one returns
quietly once shutdown has begun: the connector shares the terminal's
process group, so the signal that stops the process stops it too.

ExternalURL names the hostname of a named tunnel and applies only with a
Token, but it switched the output parser to named-tunnel mode even for a
quick tunnel, which then published ExternalURL instead of its assigned
trycloudflare URL.
@ersinkoc
ersinkoc requested a review from aojea as a code owner November 25, 2026 20:30

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request improves Cloudflare tunnel handling by treating unexpected status 0 exits as errors, ignoring ExternalURL for quick tunnels when no token is provided, and preventing spurious "lost tunnel" logs during shutdown. The review feedback suggests validating that the named URL is not empty after trimming to prevent silent fallbacks, and using a more descriptive error message for unexpected status 0 exits.

Comment on lines +139 to +141
if c.Token != "" {
t.namedURL = strings.TrimRight(strings.TrimSpace(c.ExternalURL), "/")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

If c.ExternalURL consists only of slashes and spaces (e.g., "/"), the check at line 106 passes, but t.namedURL becomes empty after trimming trailing slashes. This causes the tunnel to fall back to quick tunnel mode in scan and eventually time out. We should validate that t.namedURL is not empty after trimming.

	if c.Token != "" {
		t.namedURL = strings.TrimRight(strings.TrimSpace(c.ExternalURL), "/")
		if t.namedURL == "" {
			return nil, fmt.Errorf("cloudflare named tunnel requires a valid ExternalURL")
		}
	}

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, taken in 0d7885e, with one change: the check goes before cloudflared starts rather than after it, since returning at that point would leave the process running. ExternalURL is now trimmed once up front, and a named tunnel whose hostname trims to nothing is refused with the existing requires ExternalURL error. TestCloudflareNamedTunnelRefusesAnEmptyExternalURL covers "", spaces, "/" and " // "; the slash cases failed on the previous code.

if !p.closed && err != nil {
if !p.closed {
if err == nil {
err = errors.New("exit status 0")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using a more descriptive error message like "connector exited unexpectedly with status 0" instead of "exit status 0" makes it clearer in the logs that this was an abnormal termination, especially since status 0 normally indicates success.

Suggested change
err = errors.New("exit status 0")
err = errors.New("connector exited unexpectedly with status 0")

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.

I'd keep exit status 0. It is the text exec.ExitError gives for any other status (exit status 3), so Err() reads the same whichever way the connector exits, and it never appears alone. The watcher logs Tunnel cloudflare closed: exit status 0; remote devices can no longer reach <url>, and Open returns cloudflared exited before publishing a URL: exit status 0, so the line already says that the exit was unexpected.

Open checked ExternalURL for spaces only, so a named tunnel given "/"
started cloudflared with an empty published URL and waited for a
quick-tunnel line until it timed out. The hostname is trimmed once,
before cloudflared starts, and an empty one is refused.
}
t := &process{cancel: cancel, done: make(chan struct{}), namedURL: strings.TrimRight(strings.TrimSpace(c.ExternalURL), "/")}
t := &process{cancel: cancel, done: make(chan struct{})}
if c.Token != "" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for fixing the quick-tunnel URL! How about refusing ExternalURL without a Token instead of ignoring it? The CLI already refuses that combination. The check at the top of Open already refuses the opposite case, Token without ExternalURL. Something like:

an early exit pla

if c.Token == "" && namedURL != "" {
	return nil, fmt.Errorf("cloudflare ExternalURL applies only to a named tunnel (Token)")
}

as an early exit placed right after the check in line 107

func TestCloudflareReportsUnrequestedCleanExit(t *testing.T) {
bin := stubCloudflared(t, `echo "INF starting" >&2; exit 0`)
_, err := (&Cloudflare{Binary: bin, Timeout: 5 * time.Second}).Open(context.Background(), "http://127.0.0.1:1")
if err == nil || strings.Contains(err.Error(), "%!") {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: Thanks for covering both the Open path and the after-Open path in one test! This check passes for any error that does not contain %!, a failed start for example. How about checking for the reason itself?

Suggested change
if err == nil || strings.Contains(err.Error(), "%!") {
if err == nil || !strings.Contains(err.Error(), "exit status 0") {

@kaisoz kaisoz self-assigned this Nov 26, 2026

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants