From 03546cc5e0340fe25d23e98723778291194c3ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ersin=20KO=C3=87?= Date: Wed, 25 Nov 2026 22:05:04 +0200 Subject: [PATCH 1/2] tunnel: report a connector that exits on its own, and keep quick tunnels 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()". 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. --- cmd/agentmesh-one/main.go | 6 ++++ internal/tunnel/cloudflare.go | 12 +++++-- internal/tunnel/cloudflare_test.go | 53 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/cmd/agentmesh-one/main.go b/cmd/agentmesh-one/main.go index cf2147b51..f390569f3 100644 --- a/cmd/agentmesh-one/main.go +++ b/cmd/agentmesh-one/main.go @@ -189,6 +189,12 @@ func main() { if tun != nil { go func() { <-tun.Done() + // The connector shares the terminal's process group, so the + // signal that stops this process stops it too: an exit during + // shutdown is expected, not a lost tunnel. + if cmd.Context().Err() != nil { + return + } if err := tun.Err(); err != nil { logger.Errorf("Tunnel %s closed: %v; remote devices can no longer reach %s", tunnelProvider, err, tun.URL()) } diff --git a/internal/tunnel/cloudflare.go b/internal/tunnel/cloudflare.go index 9834e917b..1419b6917 100644 --- a/internal/tunnel/cloudflare.go +++ b/internal/tunnel/cloudflare.go @@ -135,7 +135,10 @@ func (c *Cloudflare) Open(ctx context.Context, target string) (Tunnel, error) { cancel() return nil, fmt.Errorf("failed to start cloudflared: %w", err) } - 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 != "" { + t.namedURL = strings.TrimRight(strings.TrimSpace(c.ExternalURL), "/") + } urlCh := make(chan string, 1) go t.scan(pr, urlCh) go func() { @@ -308,10 +311,13 @@ func (p *process) Close() error { } // finish records the exit and releases Done; an exit caused by Close is -// not an error. +// not an error, any other exit is, even with status 0. func (p *process) finish(err error) { p.mu.Lock() - if !p.closed && err != nil { + if !p.closed { + if err == nil { + err = errors.New("exit status 0") + } p.err = err } p.mu.Unlock() diff --git a/internal/tunnel/cloudflare_test.go b/internal/tunnel/cloudflare_test.go index d3ee86747..91b100e14 100644 --- a/internal/tunnel/cloudflare_test.go +++ b/internal/tunnel/cloudflare_test.go @@ -181,6 +181,39 @@ func TestCloudflareOpenReportsEarlyExit(t *testing.T) { } } +// A connector that stops on its own reports why even when its exit status +// is 0: only Close leaves Err nil, and the caller logs the outage only when +// Err is set. +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(), "%!") { + t.Errorf("Open error = %v, want an early-exit error with a reason", err) + } + + gate := filepath.Join(t.TempDir(), "exit") + bin = stubCloudflared(t, `echo "INF | https://installed-quick-1234.trycloudflare.com |" >&2 +while [ ! -e "`+gate+`" ]; do sleep 0.01; done +exit 0 +`) + tun, err := (&Cloudflare{Binary: bin, Timeout: 5 * time.Second, LookupHost: resolved}).Open(context.Background(), "http://127.0.0.1:1") + if err != nil { + t.Fatalf("Open: %v", err) + } + t.Cleanup(func() { _ = tun.Close() }) + if err := os.WriteFile(gate, nil, 0o600); err != nil { + t.Fatal(err) + } + select { + case <-tun.Done(): + case <-time.After(10 * time.Second): + t.Fatal("Done not closed after the connector exited") + } + if tun.Err() == nil { + t.Fatal("Err = nil after the connector exited without Close, want an error") + } +} + func TestCloudflareOpenTimesOutWithoutURL(t *testing.T) { bin := stubCloudflared(t, `trap 'exit 0' TERM; while :; do sleep 1; done`) p := &Cloudflare{Binary: bin, Timeout: 200 * time.Millisecond} @@ -434,3 +467,23 @@ func TestLookup(t *testing.T) { t.Fatalf("Lookup unknown = %v", err) } } + +// ExternalURL names the hostname of a named tunnel; without a Token the +// connector runs a quick tunnel, and the URL published is the one it was +// assigned, not ExternalURL. +func TestCloudflareQuickTunnelIgnoresExternalURL(t *testing.T) { + bin := stubCloudflared(t, `echo "INF | https://quick-ext-1234.trycloudflare.com |" >&2 +echo "INF Registered tunnel connection connIndex=0 location=ams01 protocol=quic" >&2 +trap 'exit 0' TERM +while :; do sleep 1; done +`) + p := &Cloudflare{Binary: bin, Timeout: 5 * time.Second, LookupHost: resolved, ExternalURL: "https://mesh.example.com"} + tun, err := p.Open(context.Background(), "http://127.0.0.1:1") + if err != nil { + t.Fatalf("Open: %v", err) + } + t.Cleanup(func() { _ = tun.Close() }) + if got, want := tun.URL(), "https://quick-ext-1234.trycloudflare.com"; got != want { + t.Fatalf("URL = %q, want the assigned quick-tunnel URL %q", got, want) + } +} From 0d7885e429fbf5cf155e7b7d447ff17c1cb2c827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ersin=20KO=C3=87?= Date: Wed, 25 Nov 2026 22:33:38 +0200 Subject: [PATCH 2/2] tunnel: refuse a named tunnel whose ExternalURL trims to nothing 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. --- internal/tunnel/cloudflare.go | 5 +++-- internal/tunnel/cloudflare_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/tunnel/cloudflare.go b/internal/tunnel/cloudflare.go index 1419b6917..9d56519a3 100644 --- a/internal/tunnel/cloudflare.go +++ b/internal/tunnel/cloudflare.go @@ -103,7 +103,8 @@ func (c *Cloudflare) Name() string { return "cloudflare" } // Open implements Provider. func (c *Cloudflare) Open(ctx context.Context, target string) (Tunnel, error) { - if c.Token != "" && strings.TrimSpace(c.ExternalURL) == "" { + namedURL := strings.TrimRight(strings.TrimSpace(c.ExternalURL), "/") + if c.Token != "" && namedURL == "" { return nil, fmt.Errorf("cloudflare named tunnel requires ExternalURL (--external-url)") } binary, err := c.resolveBinary(ctx) @@ -137,7 +138,7 @@ func (c *Cloudflare) Open(ctx context.Context, target string) (Tunnel, error) { } t := &process{cancel: cancel, done: make(chan struct{})} if c.Token != "" { - t.namedURL = strings.TrimRight(strings.TrimSpace(c.ExternalURL), "/") + t.namedURL = namedURL } urlCh := make(chan string, 1) go t.scan(pr, urlCh) diff --git a/internal/tunnel/cloudflare_test.go b/internal/tunnel/cloudflare_test.go index 91b100e14..9c5ed10c4 100644 --- a/internal/tunnel/cloudflare_test.go +++ b/internal/tunnel/cloudflare_test.go @@ -487,3 +487,15 @@ while :; do sleep 1; done t.Fatalf("URL = %q, want the assigned quick-tunnel URL %q", got, want) } } + +// A named tunnel needs a hostname: an ExternalURL that trims to nothing is +// refused before cloudflared is started. +func TestCloudflareNamedTunnelRefusesAnEmptyExternalURL(t *testing.T) { + for _, externalURL := range []string{"", " ", "/", " // "} { + p := &Cloudflare{Binary: filepath.Join(t.TempDir(), "never-started"), Token: "token", ExternalURL: externalURL} + _, err := p.Open(context.Background(), "http://127.0.0.1:1") + if err == nil || !strings.Contains(err.Error(), "requires ExternalURL") { + t.Errorf("Open with ExternalURL %q: err = %v, want the ExternalURL refusal", externalURL, err) + } + } +}