Skip to content

FIX a number of race conditions in threadpoolctl's OpenBLAS-pthreads ownership - #300

Merged
ogrisel merged 30 commits into
joblib:masterfrom
itamarst:299-overlapping-restores
Nov 23, 2026
Merged

ogrisel merged 30 commits into
joblib:masterfrom
itamarst:299-overlapping-restores

Conversation

@itamarst

@itamarst itamarst commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #299
Fixes #307

@itamarst
itamarst marked this pull request as ready for review November 5, 2026 20:38
@itamarst

itamarst commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

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.

@itamarst

itamarst commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

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.

@itamarst

itamarst commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

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!

@itamarst

itamarst commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Will continue trying to figure out the joblib race condition next week.

@itamarst itamarst changed the title FIX Don't restore the limits if setting them was a no-op FIX a number of race conditions in threadpoolctl's OpenBLAS-pthreads ownership Nov 9, 2026
@itamarst

itamarst commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

@ogrisel ok I think this is ready for review.

@ogrisel

This comment was marked as resolved.

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some more feedback, this time about API design concerns.

Comment thread threadpoolctl.py Resolved
@itamarst

Copy link
Copy Markdown
Contributor Author

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.

@ogrisel

ogrisel commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

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?

@itamarst

Copy link
Copy Markdown
Contributor Author

Yep, on it.

@itamarst

Copy link
Copy Markdown
Contributor Author

I made the failing test more constrained, so it should hopefully pass even on FlexiBLAS, and just left FlexiBLAS in its previous state of 🤷.

@itamarst

Copy link
Copy Markdown
Contributor Author

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.

@ogrisel ogrisel added this to Labs Nov 16, 2026
@ogrisel ogrisel moved this to PR waiting for reviews in Labs Nov 16, 2026
@itamarst

Copy link
Copy Markdown
Contributor Author

I added an extra commit with a proposed new API to address #307 / review comments in corresponding joblib PR.

@cakedev0 cakedev0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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())  # 2

It 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?

Comment thread tests/test_threadpoolctl.py Resolved
Comment thread threadpoolctl.py Resolved
@itamarst

Copy link
Copy Markdown
Contributor Author

Those both make sense to me.

@itamarst

itamarst commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

I opened new issue #308 for the existing bug(?) in restore_original_limits(), since I think it merits separate discussion, after more thought I'm not completely certain it's a bug.

@itamarst

Copy link
Copy Markdown
Contributor Author

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.

@itamarst
itamarst requested review from cakedev0 and ogrisel November 17, 2026 18:15
@itamarst

Copy link
Copy Markdown
Contributor Author

Ugggh I may have to see if I can just make flexiblas work correctly. Or we could kill flexiblas support? 😀

@itamarst

Copy link
Copy Markdown
Contributor Author

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.

@itamarst

Copy link
Copy Markdown
Contributor Author

OK I've "fixed" FlexiBLAS in that it now overrides all the custom OpenBLAS logic we've added.

@cakedev0 cakedev0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM 👍

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread CHANGES.md Resolved
Comment thread CHANGES.md Resolved
Comment thread threadpoolctl.py Resolved
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
@itamarst

itamarst commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

I've updated the changelog to mention 3.9 by applying @ogrisel 's changes.

@ogrisel
ogrisel merged commit 6c93556 into joblib:master Nov 23, 2026
26 checks passed
@github-project-automation github-project-automation Bot moved this from PR waiting for reviews to Done in Labs Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants