From 60b6dd521338952270357e3ec200d7c0aadfaee2 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Mon, 29 Aug 2022 10:55:38 -0500 Subject: [PATCH 1/2] fix: remove `(http.Server).ReadHeaderTimeout` Fixes https://github.com/coder/coder/issues/3710. It caused some race condition for websockets where the server sent the first message. --- cli/server.go | 10 ++++------ coderd/httpmw/workspaceparam.go | 9 +++++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/cli/server.go b/cli/server.go index c2b20b660c81b..251b9ab63d680 100644 --- a/cli/server.go +++ b/cli/server.go @@ -499,9 +499,8 @@ func Server(newAPI func(*coderd.Options) *coderd.API) *cobra.Command { server := &http.Server{ // These errors are typically noise like "TLS: EOF". Vault does similar: // https://github.com/hashicorp/vault/blob/e2490059d0711635e529a4efcbaa1b26998d6e1c/command/server.go#L2714 - ErrorLog: log.New(io.Discard, "", 0), - Handler: coderAPI.Handler, - ReadHeaderTimeout: time.Minute, + ErrorLog: log.New(io.Discard, "", 0), + Handler: coderAPI.Handler, BaseContext: func(_ net.Listener) context.Context { return shutdownConnsCtx }, @@ -1107,9 +1106,8 @@ func serveHandler(ctx context.Context, logger slog.Logger, handler http.Handler, logger.Debug(ctx, "http server listening", slog.F("addr", addr), slog.F("name", name)) srv := &http.Server{ - Addr: addr, - Handler: handler, - ReadHeaderTimeout: time.Minute, + Addr: addr, + Handler: handler, } go func() { err := srv.ListenAndServe() diff --git a/coderd/httpmw/workspaceparam.go b/coderd/httpmw/workspaceparam.go index 1ce23a20d9f40..7c38c41f3227c 100644 --- a/coderd/httpmw/workspaceparam.go +++ b/coderd/httpmw/workspaceparam.go @@ -8,11 +8,12 @@ import ( "net/http" "strings" + "github.com/go-chi/chi/v5" + "github.com/google/uuid" + "github.com/coder/coder/coderd/database" "github.com/coder/coder/coderd/httpapi" "github.com/coder/coder/codersdk" - "github.com/go-chi/chi/v5" - "github.com/google/uuid" ) type workspaceParamContextKey struct{} @@ -57,8 +58,8 @@ func ExtractWorkspaceParam(db database.Store) func(http.Handler) http.Handler { // "workspace_and_agent" URL parameter. `ExtractUserParam` must be called // before this. // This can be in the form of: -// - ".[workspace-agent]" : If multiple agents exist -// - "" : If one agent exists +// - ".[workspace-agent]" : If multiple agents exist +// - "" : If one agent exists func ExtractWorkspaceAndAgentParam(db database.Store) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { From 2eda495518c8ca24c7866694172cf275cad14b4d Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Mon, 29 Aug 2022 11:32:52 -0500 Subject: [PATCH 2/2] comment why disabled --- cli/server.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cli/server.go b/cli/server.go index 251b9ab63d680..b66ce7f690d6c 100644 --- a/cli/server.go +++ b/cli/server.go @@ -496,6 +496,11 @@ func Server(newAPI func(*coderd.Options) *coderd.API) *cobra.Command { shutdownConnsCtx, shutdownConns := context.WithCancel(ctx) defer shutdownConns() + + // ReadHeaderTimeout is purposefully not enabled. It caused some issues with + // websockets over the dev tunnel. + // See: https://github.com/coder/coder/pull/3730 + //nolint:gosec server := &http.Server{ // These errors are typically noise like "TLS: EOF". Vault does similar: // https://github.com/hashicorp/vault/blob/e2490059d0711635e529a4efcbaa1b26998d6e1c/command/server.go#L2714 @@ -1105,6 +1110,10 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al 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 + //nolint:gosec srv := &http.Server{ Addr: addr, Handler: handler,