This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Remove race condition in std::async
ClosedPublic

Authored by ldionne on Aug 23 2018, 9:20 AM.

Details

Summary

The state associated to the future was set in one thread (with synchronization)
but read in another thread without synchronization, which led to a data race.

https://bugs.llvm.org/show_bug.cgi?id=38682
rdar://problem/42548261

Diff Detail

Repository
rL LLVM

Event Timeline

ldionne created this revision.Aug 23 2018, 9:20 AM
jfb accepted this revision.Aug 23 2018, 4:27 PM
jfb added inline comments.
libcxx/include/future
556 ↗(On Diff #162200)

I'm not auditing everything, but it seems like code above can still access state_ without holding mut_? Like in the dtor.

Generally this patch lgtm because it's a step forward, but maybe we should separately refactor the code to make it so that accesses to state_ require passing in a reference to lock_guard to show we actually hold mut_. It would ignore that reference, but that's a way to enforce, in the type system, that __state_ is only touched when the lock is held.

WDYT?

This revision is now accepted and ready to land.Aug 23 2018, 4:27 PM
ldionne added inline comments.Aug 24 2018, 6:59 AM
libcxx/include/future
556 ↗(On Diff #162200)

I think you're right, and I filed this bug to keep track of the issue: https://bugs.llvm.org/show_bug.cgi?id=38688

Not all of them need a lock (some are in the constructor where only one thread has a reference to the data, for example), but most of them probably do.

This revision was automatically updated to reflect the committed changes.