You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Run runs server until ctx is canceled, then stops it gracefully and exits.funcRunHandler(ctx context.Context) {
s:= http.Server{
BaseContext: func(_ net.Listener) context.Context {
returnctx
},
}
gos.ListenAndServe()
<-ctx.Done()
// use new context for cancelationstopCtx, stopCancel:=context.WithTimeout(context.Background(), time.Second)
deferstopCancel()
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?
The text was updated successfully, but these errors were encountered:
The same I faced with. It is more abstract example
package main
import (
"context"
)
funcmain() {
doSmth(context.Background())
}
funcdoSmth(ctx context.Context) {
doSmth2(ctx)
// I want to use context.Background() herectx2, ctx2Cancel:=context.WithCancel(context.Background())
deferctx2Cancel()
doSmth2(ctx2)
}
funcdoSmth2(ctx context.Context) {
_=ctx
}
main.go:17:9: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
doSmth2(ctx2)
The issue occurs when context is provided into the function but I want to use another context instance intentionally. For example graceful shutdown should be done with Background context.
And currently there is no way to avoid this error from the linter
Simplified example:
stopCtx
can't be inherited fromctx
as the later is already canceled.Maybe this linter can detect that
<-ctx.Done()
was already called by this point?The text was updated successfully, but these errors were encountered: