Skip to content

Commit d3c548e

Browse files
JuliaCall: lock the GIL when the IPython extension copies Julia output
In Jupyter the extension copies Julia's stdout and stderr to Python from background tasks. If Julia printed while the GIL was unlocked, for example in `_jl_call_nogil`, those tasks called Python without the GIL and the process segfaulted (#542). Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
1 parent 4ed01ff commit d3c548e

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Improved TTFX in JuliaCall.
66
* Minimum supported Python version is now 3.11.
77
* JuliaCall option `compiled_modules` now accepts `existing` and `strict` (Julia 1.11+).
8+
* Fixed a crash when Julia prints while the GIL is unlocked (such as in `_jl_call_nogil`)
9+
with the JuliaCall IPython extension loaded in Jupyter.
810

911
## 0.9.37 (2026-10-12)
1012
* Compatibility fix with Python 3.15 from conda-forge, which no longer bundles libpython.

‎pysrc/juliacall/ipython.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,15 @@ def load_ipython_extension(ip):
8383
const _redirected_stderr = redirect_stderr()
8484
const _py_stdout = PyIO(pyimport("sys" => "stdout"); line_buffering=true)
8585
const _py_stderr = PyIO(pyimport("sys" => "stderr"); line_buffering=true)
86-
const _redirect_stdout_task = @async write($_py_stdout, $_redirected_stdout[1])
87-
const _redirect_stderr_task = @async write($_py_stderr, $_redirected_stderr[1])
86+
# These tasks can run while the GIL is unlocked, such as during _jl_call_nogil.
87+
function _copy_to_python(py, pipe)
88+
while !eof(pipe)
89+
data = readavailable(pipe)
90+
PythonCall.GIL.@lock write(py, data)
91+
end
92+
end
93+
const _redirect_stdout_task = @async _copy_to_python(_py_stdout, _redirected_stdout[1])
94+
const _redirect_stderr_task = @async _copy_to_python(_py_stderr, _redirected_stderr[1])
8895
function _flush_stdio()
8996
flush(stderr)
9097
flush(stdout)

‎pytest/test_all.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,39 @@ def test_call_nogil(yld, raw):
201201
assert 0.9 < t2 < 1.5
202202

203203

204+
def test_ipython_output_nogil():
205+
"""Tests that Julia output under the IPython extension is safe while the GIL is unlocked."""
206+
import subprocess
207+
import sys
208+
209+
pytest.importorskip("IPython")
210+
code = """
211+
import io, os, sys
212+
from IPython.core.interactiveshell import InteractiveShell
213+
from juliacall import Main as jl, PythonCall
214+
import juliacall.ipython
215+
# Julia redirects file descriptors 1 and 2, so keep copies to report through.
216+
out = os.fdopen(os.dup(1), "w")
217+
err = os.fdopen(os.dup(2), "w")
218+
# Like ipykernel, sys.stdout and sys.stderr are not file descriptors 1 and 2.
219+
sys.stdout, sys.stderr = io.StringIO(), io.StringIO()
220+
class KernelShell(InteractiveShell):
221+
pass
222+
juliacall.ipython.load_ipython_extension(KernelShell.instance())
223+
try:
224+
jl.seval('() -> (println("hello"); sleep(0.1))')._jl_call_nogil()
225+
PythonCall._ipython._flush_stdio()
226+
finally:
227+
err.write(sys.stderr.getvalue())
228+
out.write(sys.stdout.getvalue())
229+
"""
230+
result = subprocess.run(
231+
[sys.executable, "-c", code], capture_output=True, text=True, timeout=180
232+
)
233+
assert result.returncode == 0, result.stderr
234+
assert result.stdout == "hello\n"
235+
236+
204237
@pytest.mark.parametrize("mode,value", [("existing", 2), ("strict", 3)])
205238
def test_compiled_modules_option(mode, value):
206239
import os

0 commit comments

Comments
 (0)