Skip to content

Acquire GIL around every Python API call#1097

Draft
topolarity wants to merge 3 commits intoJuliaPy:masterfrom
topolarity:ct/with-gil
Draft

Acquire GIL around every Python API call#1097
topolarity wants to merge 3 commits intoJuliaPy:masterfrom
topolarity:ct/with-gil

Conversation

@topolarity
Copy link

This should resolve #1096 by wrapping every Python API usage in a GIL acquire-release.

If this leads to overhead, we can probably re-export a "manual GIL" version of the API that allows consumers to manage the GIL themselves. In any case, it's worth having proper support for multi-threading.

Fixes #1096. Fixes #1095. Fixes #1090. Fixes #1088.
Fixes #1078. Fixes #1077. Fixes #1072. Fixes #1083.
Fixes timholy/Revise.jl#989.

@topolarity topolarity force-pushed the ct/with-gil branch 5 times, most recently from b3aea39 to dd3cc6d Compare March 10, 2026 18:49
@topolarity
Copy link
Author

topolarity commented Mar 10, 2026

Before this can be considered complete, it needs an equivalent to JuliaMath/FFTW.jl#160 to handle:

  1. Task A holds the GIL lock.
  2. Task A waits for Task B to make "progress".
  3. Task B waits in GC to obtain the GIL lock to run finalizers. (deadlocks)

edit: Done!

@topolarity topolarity marked this pull request as draft March 10, 2026 19:16
Majority of the API is covered by updating the `@pycheck(v/n)` macros.
The rest is covered manually by `@with_GIL`. Care should be taken to
avoid yielding to the Julia scheduler, which could cause task migration
and lead to deadlocks.
@topolarity topolarity force-pushed the ct/with-gil branch 3 times, most recently from a395a25 to 25038c7 Compare March 11, 2026 21:09
This is required to avoid deadlocking in the GC as it executes
finalizers.
Required to avoid starving the scheduler or deadlocking against the GC.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment