8000 BUG: Remove data race in mtrand: two threads could mutate the state. by ssbr · Pull Request #7305 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Remove data race in mtrand: two threads could mutate the state. #7305

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

Merged
merged 1 commit into from
Feb 22, 2016

Conversation

ssbr
Copy link
Contributor
@ssbr ssbr commented Feb 22, 2016

E.g.:

np.random.random_sample()   # uses the GIL for synchronization
np.random.random_sample(1)  # releases the GIL and uses a separate lock.

This means that both can run simultaneously, causing a data race when mutating the random number generator's state, which could lead to buffer overflow (from incrementing state->pos).

The fix here is to always use the separate lock, so that exactly one thread at a time is mutating the random number generator's state.

E.g.:

np.random.random_sample() uses the GIL for synchronization
np.random.random_sample(1) releases the GIL and uses a separate lock.

This means that both can run simultaneously, causing a data race when mutating
the random number generator's state, which could lead to buffer overflow (from
incrementing state->pos).

The fix here is to always use the separate lock, so that exactly one thread at
a time is mutating the random number generator's state.
@charris
Copy link
Member
charris commented Feb 22, 2016

Hmm, tests aren't getting scheduled...

@@ -158,7 +158,9 @@ cdef object cont0_array(rk_state *state, rk_cont0 func, object size,
cdef npy_intp i

if size is None:
return func(state)
with lock, nogil:
rv = func(state)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to just do

with lock, nogil:
    return func(state)

? Ditto for the other nine or so cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I wrote at first, but Cython forbids returning a PyObject without the GIL, apparently. (Seems like a bug in Cython, but whatever).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a deliberate choice, not a bug. Either way, it's certainly a good reason to write the code the way you did. LGTM

@juliantaylor
Copy link
Contributor

nice catch, thank you

juliantaylor added a commit that referenced this pull request Feb 22, 2016
BUG: Remove data race in mtrand: two threads could mutate the state.
@juliantaylor juliantaylor merged commit a516555 into numpy:master Feb 22, 2016
@ssbr ssbr deleted the ssbr-racy-mtrand branch February 22, 2016 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0