Skip to content

Fix threading-related deadlocks, and also add support for Python's built-in listing of libraries - #243

Merged
ogrisel merged 13 commits into
joblib:masterfrom
itamarst:239-fix-deadlock
Sep 11, 2026
Merged

Fix threading-related deadlocks, and also add support for Python's built-in listing of libraries#243
ogrisel merged 13 commits into
joblib:masterfrom
itamarst:239-fix-deadlock

Conversation

@itamarst

@itamarst itamarst commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Fixes #239

Also adds usage of Python's built-in logic for listing shared libraries.

@itamarst
itamarst marked this pull request as ready for review September 8, 2026 16:38
@itamarst itamarst changed the title Fix deadlocks, and also add support for Python's built-in listing of libraries Fix threading-related deadlocks, and also add support for Python's built-in listing of libraries Sep 9, 2026
@itamarst
itamarst marked this pull request as draft September 10, 2026 14:33
@itamarst

Copy link
Copy Markdown
Contributor Author

May have figured how to test deadlocks; it only happens on Conda!

@itamarst

Copy link
Copy Markdown
Contributor Author

So the test is demonstrating I didn't solve deadlocks on Python 3.13 and earlier (3.14 I'm using the new built-in API which is written in C, so GIL doesn't get released at in-opportune times).

I'm starting to think having a Python callback (as opposed to C callback) to dl_iterate_phdr isn't viable. Which is fine in theory, but runs up against the desire to make threadpoolctl a single, manually-vendorable file with no dependencies.

@itamarst

itamarst commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

uOK, came up with another solution: completely bypassing dl_iterate_phdr on Linux.

@itamarst
itamarst marked this pull request as ready for review September 10, 2026 17:01
@itamarst

Copy link
Copy Markdown
Contributor Author

@ogrisel OK this should be ready for review and hopefully allow for a release.

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hope you don't mind the drive-by review. Itamar linked this PR to me privately.

Comment thread threadpoolctl.py
Comment thread threadpoolctl.py
Uses a Linux-specific mechanism:
https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html
"""
with open("/proc/self/maps") as f:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think you probably want to open in "rb" mode, otherwise a non-UTF-8 filename would break here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This matches previous behavior I think. And to be fair one could do better, but... not sure how one would know the actual encoding. And also in practice probably no one ever does that?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

UNIX paths don’t have any encoding, so just leaving everything as bytes should be fine. But also if it’s a preexisting issue it’s no biggie.

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

LGTM.

Comment thread threadpoolctl.py
Comment thread CHANGES.md Outdated
Comment thread tests/test_threadpoolctl.py Outdated
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
@ogrisel
ogrisel merged commit c686683 into joblib:master Sep 11, 2026
21 checks passed
@ogrisel

ogrisel commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Note that this PR does not fix #225 directly but it will likely reduce the size of the diff if we want to fix it.

ogrisel added a commit to ogrisel/threadpoolctl that referenced this pull request Sep 11, 2026
joblib#243 already lists libraries from /proc/self/maps. Keep that path and
only avoid importing ctypes.util (load libc via CDLL(None), lazy-import
dllist off Linux) so the libffi abort after fork from joblib#225 stays fixed.

Co-authored-by: Cursor <cursoragent@cursor.com>
cursor Bot pushed a commit to ogrisel/threadpoolctl that referenced this pull request Sep 11, 2026
Resolve conflicts with joblib#243 by keeping Windows on the Toolhelp snapshot
enumerator. ctypes.util.dllist still uses EnumProcessModules, which is
the race this PR fixes.

Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
@itamarst

Copy link
Copy Markdown
Contributor Author

Thank you!

@itamarst
itamarst deleted the 239-fix-deadlock branch September 11, 2026 18:13
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.

Deadlock when running dl_iterate_phdr in parallel with GIL Python

3 participants