diff --git a/cli/agent.go b/cli/agent.go index bf189a4fc57c2..18c4542a6c3a0 100644 --- a/cli/agent.go +++ b/cli/agent.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "net" "net/http" "net/http/pprof" "net/url" @@ -491,8 +492,6 @@ func (r *RootCmd) workspaceAgent() *serpent.Command { } func ServeHandler(ctx context.Context, logger slog.Logger, handler http.Handler, addr, name string) (closeFunc func()) { - logger.Debug(ctx, "http server listening", slog.F("addr", addr), slog.F("name", name)) - // ReadHeaderTimeout is purposefully not enabled. It caused some issues with // websockets over the dev tunnel. // See: https://github.com/coder/coder/pull/3730 @@ -502,9 +501,15 @@ func ServeHandler(ctx context.Context, logger slog.Logger, handler http.Handler, Handler: handler, } go func() { - err := srv.ListenAndServe() - if err != nil && !xerrors.Is(err, http.ErrServerClosed) { - logger.Error(ctx, "http server listen", slog.F("name", name), slog.Error(err)) + ln, err := net.Listen("tcp", addr) + if err != nil { + logger.Error(ctx, "http server listen", slog.F("name", name), slog.F("addr", addr), slog.Error(err)) + return + } + defer ln.Close() + logger.Info(ctx, "http server listening", slog.F("addr", ln.Addr()), slog.F("name", name)) + if err := srv.Serve(ln); err != nil && !xerrors.Is(err, http.ErrServerClosed) { + logger.Error(ctx, "http server serve", slog.F("addr", ln.Addr()), slog.F("name", name), slog.Error(err)) } }() diff --git a/cli/server_test.go b/cli/server_test.go index c6f8231a1a1f9..e4d71e0c3f794 100644 --- a/cli/server_test.go +++ b/cli/server_test.go @@ -22,6 +22,7 @@ import ( "os" "path/filepath" "reflect" + "regexp" "runtime" "strconv" "strings" @@ -1217,24 +1218,31 @@ func TestServer(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - randPort := testutil.RandomPort(t) - inv, cfg := clitest.New(t, + inv, _ := clitest.New(t, "server", "--in-memory", "--http-address", ":0", "--access-url", "http://example.com", "--provisioner-daemons", "1", "--prometheus-enable", - "--prometheus-address", ":"+strconv.Itoa(randPort), + "--prometheus-address", ":0", // "--prometheus-collect-db-metrics", // disabled by default "--cache-dir", t.TempDir(), ) + pty := ptytest.New(t) + inv.Stdout = pty.Output() + inv.Stderr = pty.Output() + clitest.Start(t, inv) - _ = waitAccessURL(t, cfg) + + // Wait until we see the prometheus address in the logs. + addrMatchExpr := `http server listening\s+addr=(\S+)\s+name=prometheus` + lineMatch := pty.ExpectRegexMatchContext(ctx, addrMatchExpr) + promAddr := regexp.MustCompile(addrMatchExpr).FindStringSubmatch(lineMatch)[1] testutil.Eventually(ctx, t, func(ctx context.Context) bool { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d/metrics", randPort), nil) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://%s/metrics", promAddr), nil) if err != nil { t.Logf("error creating request: %s", err.Error()) return false @@ -1272,24 +1280,31 @@ func TestServer(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - randPort := testutil.RandomPort(t) - inv, cfg := clitest.New(t, + inv, _ := clitest.New(t, "server", "--in-memory", "--http-address", ":0", "--access-url", "http://example.com", "--provisioner-daemons", "1", "--prometheus-enable", - "--prometheus-address", ":"+strconv.Itoa(randPort), + "--prometheus-address", ":0", "--prometheus-collect-db-metrics", "--cache-dir", t.TempDir(), ) + pty := ptytest.New(t) + inv.Stdout = pty.Output() + inv.Stderr = pty.Output() + clitest.Start(t, inv) - _ = waitAccessURL(t, cfg) + + // Wait until we see the prometheus address in the logs. + addrMatchExpr := `http server listening\s+addr=(\S+)\s+name=prometheus` + lineMatch := pty.ExpectRegexMatchContext(ctx, addrMatchExpr) + promAddr := regexp.MustCompile(addrMatchExpr).FindStringSubmatch(lineMatch)[1] testutil.Eventually(ctx, t, func(ctx context.Context) bool { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d/metrics", randPort), nil) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://%s/metrics", promAddr), nil) if err != nil { t.Logf("error creating request: %s", err.Error()) return false
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: