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

dtChannel.close appears to have a lock condition in it #327

Open
hannahhoward opened this issue May 23, 2022 · 1 comment
Open

dtChannel.close appears to have a lock condition in it #327

hannahhoward opened this issue May 23, 2022 · 1 comment

Comments

@hannahhoward
Copy link
Collaborator

While working on transport code, I noticed the code for dtchannel.Close() appears to have a potential lock condition.

func (c *dtChannel) close(ctx context.Context) error {
	var errch chan error
	c.lk.Lock()
	{
		// Check if the channel was already cancelled
		if c.requestID != nil {
			errch = c.cancel(ctx)
		}
	}
	c.lk.Unlock()

	// Wait for the cancel message to complete
	select {
	case err := <-errch:
		return err
	case <-ctx.Done():
		return ctx.Err()
	}
}

If requestID==nil, as I read this code, then errch is left nil, and the select will not return until the context completes, which seems off. I would think if requestid == nil, the channel is already closed, and we can return immediately. Plus if for some reason context never closes, this routine would never return.

Am I wrong? Am I reading this wrong.

@rvagg
Copy link
Member

rvagg commented May 23, 2022

yeah, looks very dodgy but maybe depends on a bunch of contextual things:

  • What are the cases where requestID could be nil here? Can we even have a case where it's nil and close() gets called? Maybe that's why this hasn't caused a problem.
  • Does ctx get used for channels? Maybe we close contexts elsewhere, so the Done() gets triggered here?

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

2 participants