Skip to content

Fix thread-count restoration when OpenBLAS libraries share state - #310

Open
cccat6 wants to merge 4 commits into
joblib:masterfrom
cccat6:codex/fix-shared-pool-restore-order
Open

cccat6 wants to merge 4 commits into
joblib:masterfrom
cccat6:codex/fix-shared-pool-restore-order

Conversation

@cccat6

@cccat6 cccat6 commented Nov 30, 2026 •

Copy link
Copy Markdown

I ran into this with the two ROCm host OpenBLAS libraries loaded in the same process. Both exposed the same thread count: limiting BLAS to one thread worked, but leaving the context left it at one instead of restoring the original count.

The initial snapshot is correct. When a thread takes control of OpenBLAS, the limiter refreshes the saved counts in case a previous owner changed them. If the first setter has already changed a shared pool, that refresh can overwrite a later controller's original count with the temporary limit. Reversing restoration order repairs the final count in my reproducer, but leaves get_original_num_threads() reporting the temporary limit.

This change claims control and refreshes the selected counts before calling any setter. The regression test uses FlexiBLAS and its pthreads OpenBLAS backend in the existing Linux FlexiBLAS CI job. It checks both the value reported inside the context and the actual thread count after exit.

Retrigger CI after the same access violation tracked in upstream issue joblib#261. No code changes.
@ogrisel

ogrisel commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the report and fix @cccat6. This seems to be very similar in scope with the recently merged #309 PR by @itamarst. Have your checked that this is still needed?

@ogrisel

ogrisel commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Could you please include a non-regression test? Maybe this problem can be reproduced when installing the two numpy and scipy vendored openblas copies from pypi for instance?

@itamarst

itamarst commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Yeah my guess is it's already fixed (master branch has code that 3.8 does not). As first step maybe test with master? And then if it's still broken can try to get a reproducer.

@cccat6

cccat6 commented Dec 3, 2026

Copy link
Copy Markdown
Author

Thanks for pointing me to #309 and suggesting a regression test. I retested master at c65ccdd with both ROCm host OpenBLAS libraries loaded, using the same discovery prefix in both runs. The thread count still went from 16 to 1 and stayed at 1 after leaving the context on master; with this PR it went back to 16.

I added a small regression test for two OpenBLAS controllers that share one thread count. It fails on master and passes with this PR. It doesn't depend on whether the NumPy and SciPy wheels happen to share that state.

@itamarst

itamarst commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

I'll take a look tomorrow, or if I run out of time, Monday.

@ogrisel

ogrisel commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Note that once #311 is finalized and merged, we should update this PR to add a non regression test.

EDIT: actually, I had not seen that youd added 24571eb in the mean time. Not sure if we need another non-regression test or not. I tend to prefer actual integration tests rather than unit test that rely on non-trivial monkeypatch logic because maintaining such tests is non-trivial and potentially brittle in case of refactoring: they could pass silently for wrong reasons after changes in implementation details.

@itamarst

itamarst commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

I wonder if FlexiBLAS would give the same effect?

@itamarst

itamarst commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

I don't really understand the claimed diagnosis: "The two libraries share the same thread-count state. Applying the limit saves 16 for the first controller and then 1 for the second."

But the code has two passes, first recording limits and then changing limits. It's not interleaved.

@cccat6

cccat6 commented Dec 4, 2026

Copy link
Copy Markdown
Author

Thanks for pointing that out. Please give me a little time to double-check my explanation and try the FlexiBLAS case.

@cccat6

cccat6 commented Dec 4, 2026

Copy link
Copy Markdown
Author

I double-checked this, and you were right about the two passes. Both entries are 16 in the initial snapshot. I missed the later refresh when OpenBLAS takes control. By the time it reads the second entry, the first setter has already changed the shared count to 1. My previous patch restored 16 on exit, but I found that get_original_num_threads() still returned 1.

I reproduced it on current master with the ROCm libraries once both were detected. I could also reproduce it with FlexiBLAS and its pthreads OpenBLAS backend when the wrapper was visited first. The NumPy and SciPy wheels I tested had independent counts.

I’ve updated the fix so the refresh happens before any setter runs. I verified both the reported and restored counts, including when another thread changes the count before handing over control. I also replaced the mock test with a real-library regression test for the existing Linux FlexiBLAS CI job. It fails locally on both master and my previous patch, and passes with this change. Thanks for helping me catch what I’d missed.

@itamarst

itamarst commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Did a coding agent write that last comment?

In any case I suspect the initial reversing approach is the correct one, given there's already a hard-coded solution that kinda-sorta does that for FlexiBLAS. And after reminding myself of the code I can see how the "by the time it gets to second one" comment came about.

@cccat6

cccat6 commented Dec 4, 2026

Copy link
Copy Markdown
Author

Did a coding agent write that last comment?

In any case I suspect the initial reversing approach is the correct one, given there's already a hard-coded solution that kinda-sorta does that for FlexiBLAS. And after reminding myself of the code I can see how the "by the time it gets to second one" comment came about.

Yes, it is AI assisted. I did the checking and writing.

You’re right about the FlexiBLAS ordering: the normal path puts the wrapper last and restores correctly on my machine. My test explicitly put the wrapper first. I should have made that distinction clearer.

The ROCm case doesn’t need that reordering. With both libraries detected and sharing a thread count, my original patch restored 4 on exit, just as you suggest. The remaining issue was that get_original_num_threads() still returned 1 inside the context. That’s why I changed the patch. I’d be happy to keep reverse restoration if there’s a simpler way to get both values right. What way would you like to move on?

Thanks again for your review.

@itamarst

itamarst commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

I do not want a massive rewrite of this (annoyingly subtle) logic without thinking it through very hard, so I will get back to this next week. And again, post-#312, since that fixes some of the issues (unnecessarily setting too many things).

With FlexiBLAS you would have:

  1. Set FlexiBLAS from 12 to 1.
  2. Set OpenBLAS successfully, so take ownership and record updated current number of threads of 1.

This was fixed by moving FlexiBLAS to end, so it's always restored last, but restoring in reverse order fixes it too and probably more generically, including the case you mention. It does mean get_original_num_threads() is wrong in some cases, or at least has inconsistent values... but if last one wins that gets you something accurate-ish.

So no need for updates to this PR, I will take a look next week.

This branch has not been deployed

No deployments
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.

3 participants