Repository navigation
Conversation
…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.
There was a problem hiding this comment.
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.
| if c.Token != "" { | ||
| t.namedURL = strings.TrimRight(strings.TrimSpace(c.ExternalURL), "/") | ||
| } |
There was a problem hiding this comment.
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")
}
}There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
| err = errors.New("exit status 0") | |
| err = errors.New("connector exited unexpectedly with status 0") |
There was a problem hiding this comment.
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 != "" { |
There was a problem hiding this comment.
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(), "%!") { |
There was a problem hiding this comment.
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?
| if err == nil || strings.Contains(err.Error(), "%!") { | |
| if err == nil || !strings.Contains(err.Error(), "exit status 0") { |
Two Cloudflare tunnel fixes, each with a regression test that failed on the previous code.
Tunnel.Erris documented as "nil after Close", but acloudflaredthat exited with status 0 withoutClosealso left it nil, soagentmesh-one's watcher said nothing when the public URL died, andOpenwrapped the nil intocloudflared exited before publishing a URL: %!w(<nil>). Any exit not caused byCloseis now an error. Because the connector shares the terminal's process group, Ctrl-C stops it too, so the watcher inagentmesh-onenow returns quietly once shutdown has begun instead of logging that exit.ExternalURLnames the hostname of a named tunnel and applies only with aToken, but it also switched the output parser to named-tunnel mode for a quick tunnel, which then publishedExternalURLinstead of itstrycloudflare.comURL. The CLI already refuses that flag combination; this fixes the exported API.go test -race ./internal/tunnel/ ./cmd/agentmesh-one/andgo vetpass on Linux.