All jobs

Add HTTP Server Timeouts to a chi API

Yes. An agent sets read, write and idle timeouts on the http.Server behind your chi router, lines them up with your handler timeout, proves them, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent starts with what is bounded today. In an API laid out like dhax/go-base, api/api.go builds the router with middleware.Recoverer, middleware.RequestID, middleware.Timeout(15 * time.Second) and a structured logger, so a handler that runs past fifteen seconds gets a 503 from chi; middleware.RealIP is commented out. But api/server.go builds the server as http.Server{Addr: addr, Handler: api} and nothing more, so ReadHeaderTimeout, ReadTimeout, WriteTimeout and IdleTimeout are all zero: a client that opens a connection and sends headers one byte a minute holds a goroutine forever, a slow upload is never cut off, and keep-alive connections never expire. gosec, which the repository does not run but many do, flags exactly this as G112. Start() runs ListenAndServe in a goroutine, waits for os.Interrupt, and calls Shutdown(context.Background()) with no deadline, and cmd/serve.go sets port and the auth expiries as viper defaults, which .env.example mirrors.

It sets the four timeouts in the one place the server is made. NewServer reads server_read_header_timeout, server_read_timeout, server_write_timeout and server_idle_timeout from viper, with defaults registered in cmd/serve.go next to port: 5 s to read headers, 10 s to read a body, 30 s to write, 120 s idle. The write timeout is the one that has to agree with the router: middleware.Timeout at 15 s cancels the request context, and WriteTimeout at 30 s is the hard stop after it, so a handler that ignores its context still cannot hold the connection; the pull request says in a comment why 30 is not 15. ReadTimeout covers the body for the JSON endpoints here, none of which stream, and the agent notes that a future upload route needs http.MaxBytesReader and a per-route value rather than a bigger global one. .env.example gains the four variables with their defaults.

Shutdown gets a deadline too, from server_shutdown_timeout with a 10 s default, so a stop signal during a slow request ends in bounded time instead of waiting on a client. The signal list stays os.Interrupt only; adding SIGTERM is the graceful-shutdown change and is not part of this one. routes.md, which cmd/gendoc.go generates, does not change because no route does.

In its sandbox the agent builds with go build ./... and runs go test ./... with the Go version .tool-versions names, then writes api/server_test.go: one test starts NewServer on a free port with a 200 ms header timeout, opens a raw TCP connection, writes half a request line, and asserts the server closes the socket within a second; another sets a 200 ms write timeout, hits a test handler that sleeps 500 ms, and asserts the client sees the connection reset. It runs gosec ./... once to show G112 no longer fires, and pastes both into the pull request.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add HTTP server timeouts to this chi API. Read api/server.go, api/api.go, cmd/serve.go, cmd/root.go, .env.example and the workflows under .github first. In NewServer set ReadHeaderTimeout, ReadTimeout, WriteTimeout and IdleTimeout on the http.Server from viper keys server_read_header_timeout, server_read_timeout, server_write_timeout and server_idle_timeout, with defaults 5s, 10s, 30s and 120s registered in cmd/serve.go beside port. Keep middleware.Timeout at 15 s and comment why WriteTimeout is longer. Give Shutdown a deadline from server_shutdown_timeout, default 10s; do not change the signal handling. Add the variables to .env.example. Write api/server_test.go with a slow-headers test that asserts the socket is closed and a slow-handler test that asserts the write timeout, run go test ./... and gosec ./..., and quote the results in the pull request. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request touching api/server.go, cmd/serve.go, .env.example and a new api/server_test.go. The description lists the five keys with their defaults and what each one bounds, quotes the go test ./... summary with the two new tests and their durations, and the gosec output before and after showing the G112 finding gone.

The run's record keeps the test transcript, including the raw socket test's timing and the exact error the client got when the write timeout fired. A reviewer can read the existing auth/pwdless/api_test.go still passing against its httptest.Server, which sets no timeouts and is unaffected.

Before merging, a reviewer checks the defaults against the proxy in front of the service: the idle timeout should be longer than the load balancer's so the balancer closes connections first, and the write timeout should exceed the slowest expected response plus the 15 s handler timeout.

Run it on every latency alert

This is the job to run when the monitor reports goroutines or open connections climbing with no matching traffic. In the Project for the repository, an automation with the prompt above and a webhook trigger takes the alert body from Grafana or the host's metrics; the handler sends the trigger's URL an Idempotency-Key made from the alert id, so a re-fired alert does not start a second run. The trigger adds the payload after the prompt, so the run reads which number crossed the line.

The first run adds the timeouts. A later run on a repository that already has them reads the alert, reproduces the slow client in the sandbox, and opens a pull request that changes one value with the reason in the description, each on its own branch and linked to the alert.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

Four fields on a struct, and the hard part is the relationships: a write timeout shorter than the handler timeout cuts off responses the router would have finished, a read timeout that covers the body breaks the first upload route someone adds, and a shutdown with no deadline turns a deploy into a hang. Testing them means a raw socket and a clock, which is why they are usually left at zero. In a Rush sandbox the agent opens the socket, sends half a request, and the pull request quotes when the server closed it.

The tests stay with the run, so when a route needs a longer limit later the next person can see what the global values protect and give that route its own. Across many Go services on the same layout the job runs in parallel, one sandbox each, with the timing in every pull request.

Frequently asked questions

Isn't middleware.Timeout enough?

No. It cancels the request context after the handler starts, so a client that never finishes sending headers never reaches it, and a handler that ignores its context keeps writing. ReadHeaderTimeout and WriteTimeout are enforced by the server itself, before and after the handler.

Why is the write timeout longer than the handler timeout?

Because WriteTimeout counts from the end of reading the request headers and is the last resort. With the router's 15 s timeout doing the normal work, 30 s only fires for a handler that ignored its context, and it still bounds the connection. Setting both to 15 would cut off responses the router was about to send.

Does this affect the existing tests that use httptest.Server?

No. auth/pwdless/api_test.go builds its own httptest.NewServer around the auth router and never calls NewServer, so it keeps its unbounded defaults. The new tests in api/server_test.go call NewServer with short values set through viper.

What about a route that uploads a large file?

ReadTimeout would cut it off. The pull request's comment says to wrap that route's body in http.MaxBytesReader and use http.ResponseController to extend the deadline per request rather than raising the global value. None of the current routes stream a body.

Why not also handle SIGTERM in Start?

Because that is the graceful-shutdown change, which has its own page and its own test. This pull request gives the existing Shutdown call a deadline so it cannot hang, and leaves the signal list alone.

More chi jobs

The same job on other stacks

More jobs like this: Fix a bug from an alert

Keep reading