Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False-positive with a graceful shutdown #2

Open
AlekSi opened this issue Nov 6, 2021 · 2 comments
Open

False-positive with a graceful shutdown #2

AlekSi opened this issue Nov 6, 2021 · 2 comments
Labels
good first issue Good for newcomers

Comments

@AlekSi
Copy link

AlekSi commented Nov 6, 2021

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?

@nawa
Copy link

nawa commented Dec 3, 2021

The same I faced with. It is more abstract example

package main

import (
	"context"
)

func main() {
	doSmth(context.Background())
}

func doSmth(ctx context.Context) {
	doSmth2(ctx)

	// I want to use context.Background() here
	ctx2, ctx2Cancel := context.WithCancel(context.Background())
	defer ctx2Cancel()

	doSmth2(ctx2)
}

func doSmth2(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

@kkHAIKE
Copy link
Owner

kkHAIKE commented May 15, 2022

this case can write func like this to pass

func NoInheritCancel(_ context.Context) (context.Context,context.CancelFunc) {
  return context.WithCancel(context.Background())
}

@kkHAIKE kkHAIKE added the good first issue Good for newcomers label May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants