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

NilAway reports false positive for a nil channel with context.WithCancel(context.Background()) #124

Closed
tyree731 opened this issue Nov 20, 2023 · 3 comments

Comments

@tyree731
Copy link

Ran NilAway on some code of ours, and got a few messages to this effect:

broadcast_receiver.go:66:10: error: Potential nil panic detected. Observed nil flow from source to dereference point:
        -> context/context.go:184:9: literal `nil` returned from `Done()` in position 0
        -> context/context.go:105:2: returned as result 0 from interface method `Context.Done()` (implemented by `emptyCtx.Done()`)
        -> broadcast_receiver.go:66:10: result 0 of `Done()` uninitialized; nil channel accessed****

The code in question is selecting on .Done() for a context. That context is created as such:

ctx, cancel := context.WithCancel(context.Background())

Said selection happening in a separate function:

	for {
		select {
		case <-b.ctx.Done():
			return
		... more cases here...
		}
	}

While it is true that context.Background() creates an emptyCtx, that which can return a nil channel, when wrapping that emptyCtx with context.WithCancel, the resulting context is replaced with a cancelCtx, cancels being propagated (needlessly) to the emptyCtx, with cancelCtx not returning a nil channel.

This to me suggests this is a false positive.

@ioppermann
Copy link

Same here. I get this false positive all over our codebase.

@dolmen
Copy link

dolmen commented Nov 22, 2023

Duplicate of #98.

@yuxincs
Copy link
Contributor

yuxincs commented Nov 22, 2023

Thanks for reporting such cases! We were aware of this issue (generally channels are not handled gracefully for now) and are brewing some PRs for a fix.

Closing this issue as a duplicate of #98, let's continue the discussion there!

@yuxincs yuxincs closed this as completed Nov 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants