Repository navigation
FIX a number of race conditions in threadpoolctl's OpenBLAS-pthreads ownership - #300
Conversation
|
Plausibly there's a second half missing, where the top-level restore of threadpoolctl, the one where it relinquishes control, should always be to the original level of the process, because when it grabs the original level it might grab the wrong one as a race condition. |
|
Or, another way of saying it, if thread A stores values to restore to at a time when thread B is the controlling thread, the restore value is wrong, and should instead be "all cores" (zero? or a level stored at startup). Will do that or equivalent tomorrow. |
|
The above is a real race condition (I have implemented a test and fix) but it is still not enough to fix the joblib race condition reproducer! |
|
Will continue trying to figure out the joblib race condition next week. |
|
@ogrisel ok I think this is ready for review. |
This comment was marked as resolved.
This comment was marked as resolved.
ogrisel
left a comment
There was a problem hiding this comment.
Some more feedback, this time about API design concerns.
|
The reproducer seems like basically the known limitation, that using the imperative API doesn't participate in automatic process? Last time it came up we decided to just document it. And it's why joblib has to increment/decrement manually itself. |
|
Yeah sorry, I forgot about that. Let's ignore this part of my review. The second part on the public API could be fixed though. What do you think? |
|
Yep, on it. |
|
I made the failing test more constrained, so it should hopefully pass even on FlexiBLAS, and just left FlexiBLAS in its previous state of 🤷. |
|
My basic thought is, to summarize: none of this is perfect but it's probably better than what we have now, and the edge cases (which are real!) probably don't affect that many people. And edge cases are probably no worse than the status quo before 3.8.0. |
|
I added an extra commit with a proposed new API to address #307 / review comments in corresponding joblib PR. |
cakedev0
left a comment
There was a problem hiding this comment.
Thanks for the PR! Claude has two findings:
The first one is the snapshot race with two OpenBLAS copies loaded, see my inline comments. I didn't looked in details at the proposed fix, but the modified test that fails convinces me it's indeed worth fixing.
The second one is the bug of #299 in a sibling code path: restore_original_limits also restores the libraries that this limiter never set. A thread that only limits OpenMP (or passes limits=None) still holds an OpenBLAS snapshot, and on exit it takes control to restore it:
from threading import Event, Thread
import numpy # noqa: F401
from threadpoolctl import ThreadpoolController, threadpool_limits
lib = ThreadpoolController().select(internal_api="openblas").lib_controllers[0]
print(lib.get_num_threads()) # 14
a_in, b_in, a_out = Event(), Event(), Event()
def A():
with threadpool_limits(2):
a_in.set()
b_in.wait()
a_out.set()
def B():
a_in.wait()
with threadpool_limits(1, user_api="openmp"): # or threadpool_limits()
b_in.set()
a_out.wait()
threads = [Thread(target=A), Thread(target=B)]
for t in threads:
t.start()
for t in threads:
t.join()
print(lib.get_num_threads()) # 2It behaves the same on master, but I think the fix belongs here since it is the same bug. Keeping in _original_info only the libraries that _set_threadpool_limits did set (dropping the entry on the continue path and when num_threads is None) should cover it. WDYT?
|
Those both make sense to me. |
|
I opened new issue #308 for the existing bug(?) in |
|
I suggest doing #308 separately, if at all, because it feels like a different issue. And still requires some thought to decide what the right thing to do is. |
|
Ugggh I may have to see if I can just make flexiblas work correctly. Or we could kill flexiblas support? 😀 |
|
Looking at flexiblas API there is no easy way to say "this backend is this shared library", so if there are multiple OpenBLAS loaded it's hard to tell which one FlexiBLAS is using. And it's not impossible, probably, but I don't want to go down that route. So gonna try to think of solution that isn't fixing FlexiBLAS to do the right thing. |
|
OK I've "fixed" FlexiBLAS in that it now overrides all the custom OpenBLAS logic we've added. |
ogrisel
left a comment
There was a problem hiding this comment.
Thanks for the follow @itamarst and the review @cakedev0. I did another pass of review, of the new test and updated doc in particular, and this LGTM.
However since this PR also introduce a new limit_in_worker_thread public method to cleanly expose OpenBLAS specific limitting semantics, I would rather make this PR target a new feature release rather than a bugfix release.
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
|
I've updated the changelog to mention 3.9 by applying @ogrisel 's changes. |
Fixes #299
Fixes #307