Skip to content

False-positive with a graceful shutdown #2

@AlekSi

Description

@AlekSi

Simplified example:

// Run runs server until ctx is canceled, then stops it gracefully and exits.
func RunHandler(ctx context.Context) {
	s := http.Server{
		BaseContext: func(_ net.Listener) context.Context {
			return ctx
		},
	}

	go s.ListenAndServe()
	<-ctx.Done()

	// use new context for cancelation
	stopCtx, stopCancel := context.WithTimeout(context.Background(), time.Second) 
	defer stopCancel()
	s.Shutdown(stopCtx)

	s.Close()
}

stopCtx can't be inherited from ctx as the later is already canceled.

Maybe this linter can detect that <-ctx.Done() was already called by this point?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions