JuliaPy / JuliaPy/PyCall.jl

thread safety

Offen
#882 30 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
bug
Vorherrschende Sprache
Julia
Sterne
1.5k
Forks
186
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

If Julia's `Threads.nthreads() > 1`, we may want to do some more work to ensure thread safety. In particular:

* Call `PyEval_InitThreads()` in `__init__` (this is only needed for Python ≤ 3.6, and is a no-op in Python ≥ 3.7).
* Acquire the GIL in the PyObject `finalizer`, by calling `PyGILState_Ensure` (returns an [`enum`](https://github.com/python/cpython/blob/63298930fb531ba2bb4f23bc3b915dbf1e17e9e1/Include/pystate.h#L95-L96), i.e. a `Cint` in Julia) and `PyGILState_Release`. This is because the Julia GC may be called from threads other than the main thread, so we need to ensure that we hold Python's GIL before decref-ing.

We might also expose an API to acquire the GIL, but in general I would recommend that user code should only call Python from the main thread.

See [this forum post](https://discourse.julialang.org/t/help-with-pycall-related-segfault/55998?u=stevengj) and #881 for a possible test case.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Start by inspecting the package __init__ path and the PyObject finalizer, then review the possible test case referenced in #881. Check the Python-version behavior around thread initialization and the Julia GC running finalizers from non-main threads. Done means the relevant thread-safety cases no longer risk Python API calls without the GIL.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
julia, python
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.