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

Fix race condition allowing duplicate unbuffered reads #24

Merged
merged 1 commit into from
Jan 23, 2016

Conversation

dmac
Copy link
Contributor

@dmac dmac commented Jan 23, 2016

Previously, this sequence of events would result in the same
value being read twice from an unbuffered channel:

Thread S                     Thread R
--------                     --------
unbuffered_chan_send()
  chan->data = data
  chan->w_waiting++
  cond_wait on chan->m_mu
                             unbuffered_chan_recv()
                               mutex_lock chan->m_mu
                               chan->w_waiting > 0
                               *data = chan->data
                               cond_signal chan->w_cond
                               mutex_unlock chan->m_mu

                             unbuffered_chan_recv()
                               mutex_lock chan->m_mu
                               chan->w_waiting > 0
                               *data = chan->data
                               cond_signal chan->w_cond
                               mutex_unlock chan->m_mu
  chan->w_waiting--
  mutex_unlock chan->m_mu

Moving the chan->w_waiting-- to immediately after the data
is read in unbuffered_chan_recv() prevents this double read.

Fixes #23

Previously, this sequence of events would result in the same
value being read twice from an unbuffered channel:

    Thread S                     Thread R
    --------                     --------
    unbuffered_chan_send()
      chan->data = data
      chan->w_waiting++
      cond_wait on chan->m_mu
                                 unbuffered_chan_recv()
                                   mutex_lock chan->m_mu
                                   chan->w_waiting > 0
                                   *data = chan->data
                                   cond_signal chan->w_cond
                                   mutex_unlock chan->m_mu

                                 unbuffered_chan_recv()
                                   mutex_lock chan->m_mu
                                   chan->w_waiting > 0
                                   *data = chan->data
                                   cond_signal chan->w_cond
                                   mutex_unlock chan->m_mu
      chan->w_waiting--
      mutex_unlock chan->m_mu

Moving the chan->w_waiting-- to immediately after the data
is read in unbuffered_chan_recv() prevents this double read.

Fixes tylertreat#23
@tylertreat
Copy link
Owner

Thanks!

tylertreat added a commit that referenced this pull request Jan 23, 2016
Fix race condition allowing duplicate unbuffered reads
@tylertreat tylertreat merged commit d737162 into tylertreat:master Jan 23, 2016
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

Successfully merging this pull request may close these issues.

2 participants