Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX generator unordered lock - #1789

Merged
tomMoral merged 30 commits into
joblib:mainfrom
Nanored4498:FIX-generator_unordered-lock
Jun 8, 2026
Merged

FIX generator unordered lock#1789
tomMoral merged 30 commits into
joblib:mainfrom
Nanored4498:FIX-generator_unordered-lock

Conversation

@Nanored4498

Copy link
Copy Markdown
Contributor

#1788 reported some issues with the set of jobs (for unordered generators) being concurrently modified and accessed. So I just added a lock to avoid that.

Closes: #1788

@codecov

codecov Bot commented Apr 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.18%. Comparing base (6e9a70e) to head (241bae8).
⚠️ Report is 26 commits behind head on main.

Files with missing lines Patch % Lines
joblib/parallel.py 60.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1789      +/-   ##
==========================================
- Coverage   94.21%   94.18%   -0.04%     
==========================================
  Files          46       46              
  Lines        7922     7926       +4     
==========================================
+ Hits         7464     7465       +1     
- Misses        458      461       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fcharras

Copy link
Copy Markdown
Contributor

@Nanored4498 thank you for the PR

I suggest not using a lock there. Instead, is there a way to select an item from the set, without removing said item, and without this iter trick ?

Maybe by pop'ing the item and putting it back afterward ? or having an additional attribute to joblib.Parallel ?

@fcharras

Copy link
Copy Markdown
Contributor

Reading through the retrieve function I think poping (and controlling for KeyError instead of having the None default) and adding back afterward will work.

@Nanored4498

Copy link
Copy Markdown
Contributor Author

Reading through the retrieve function I think poping (and controlling for KeyError instead of having the None default) and adding back afterward will work.

I'm not sufficiently used to python ^^ It would work because of the GIL, right? pop and add are both atomic operations, and only one thread could execute one of these methods at the same time?

@fcharras

Copy link
Copy Markdown
Contributor

TY for the update @Nanored4498

Yes pop and add are atomics, but it's not required for the logic to be sound here. This bug is related to concurrency, but is less caused by concurrency, than by the wrong usage of iter. The issue is that implementing selection of an arbitrary element of a set with next(iter(... was in insight wrong, I thought it was a cool syntax shortcut at the time, but the implementation of iter itself makes it subject to an exception triggered by a race condition.

I'd be +1 on this change, could it ship in a patch release ? The bug can really trigger at any time for any user workload :-/

@tomMoral tomMoral 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

Definitely better than adding an extra lock.
Not sure if we can have a test that triggers a lot of exception for non-regression here, so I am +1 to merge and ship.

@tomMoral

tomMoral commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

@Nanored4498 could you add an entry in the changelog?

The windows failure seems to be unrelated, so we should merge this one.

@Nanored4498

Copy link
Copy Markdown
Contributor Author

@Nanored4498 could you add an entry in the changelog?

This is done ;)

@tomMoral
tomMoral merged commit c6a05d5 into joblib:main Jun 8, 2026
36 of 37 checks passed
@Nanored4498
Nanored4498 deleted the FIX-generator_unordered-lock branch June 8, 2026 14:31
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.

Parallel(return_as="generator_unordered") can crash with RuntimeError: Set changed size during iteration

3 participants