8000 staticcheck: detect invalid server shutdown calls from handler · Issue #913 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content

staticcheck: detect invalid server shutdown calls from handler #913

Open

Description

@ainar-g

(This a tricky one, so feel free to close if you feel like there is nothing that can be done here.)

s := &http.Server{
	Addr:              ":8080",
	ReadTimeout:       5 * time.Second,
	ReadHeaderTimeout: 5 * time.Second,
	IdleTimeout:       5 * time.Second,
	WriteTimeout:      5 * time.Second,
}

done := make(chan struct{})
h := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
	log.Println("shutdown")
	log.Println("handler", s.Shutdown(r.Context()))
	close(done)
})

s.Handler = h

log.Println("serve", s.ListenAndServe())

<-done

https://play.golang.org/p/_OGyCHl3G4m

Calling this handler, for example with curl, will hang forever, because http.(*Server).Shutdown will block until all handlers finish… including the one that currently waits for it to return.

I'm not sure if (1) this error is common or (2) if this can even be reliably detected. The analyser would need to know that this particular server serves this particular handler, including through third-party routers and middlewares, and also uses the deafult context. Perhaps, a less precise version that only checks for calls of Shutdown in HTTP handlers it's a better fit for @dgryski's semgrep?

Activity

  1. added
    needs-decisionWe have to decide if this check is feasible and desirable
    and removed
    needs-triageNewly filed issue that needs triage
    on Jan 22, 2021
  2. dominikh commented on Jan 22, 2021

    @dominikh
    Owner

    I wonder if this would be better suited to dynamic analysis, e.g. by net/http itself.

  3. ainar-g commented on Jan 26, 2021

    @ainar-g
    Author

    I will probably eventually file an issue in the Go issue tracker as well.

  4. added
    aggressiveA set of checks that is more prone to false positives but is helpful during code review
    on Oct 17, 2021
  5. added this to the Staticcheck 2022.2 milestone on Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    aggressiveA set of checks that is more prone to false positives but is helpful during code reviewneeds-decisionWe have to decide if this check is feasible and desirablenew-check

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0