From d3c548e2ab8adad6f20e9d5523109e4c3387a84a Mon Sep 17 00:00:00 2001 From: MilesCranmerBot Date: Fri, 27 Nov 2026 13:50:53 +0000 Subject: [PATCH] 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 --- CHANGELOG.md | 2 ++ pysrc/juliacall/ipython.py | 11 +++++++++-- pytest/test_all.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1506de11..eed59c43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ * Improved TTFX in JuliaCall. * Minimum supported Python version is now 3.11. * JuliaCall option `compiled_modules` now accepts `existing` and `strict` (Julia 1.11+). +* Fixed a crash when Julia prints while the GIL is unlocked (such as in `_jl_call_nogil`) + with the JuliaCall IPython extension loaded in Jupyter. ## 0.9.37 (2026-10-12) * Compatibility fix with Python 3.15 from conda-forge, which no longer bundles libpython. diff --git a/pysrc/juliacall/ipython.py b/pysrc/juliacall/ipython.py index 7edc43a4..002ec2d5 100644 --- a/pysrc/juliacall/ipython.py +++ b/pysrc/juliacall/ipython.py @@ -83,8 +83,15 @@ def load_ipython_extension(ip): const _redirected_stderr = redirect_stderr() const _py_stdout = PyIO(pyimport("sys" => "stdout"); line_buffering=true) const _py_stderr = PyIO(pyimport("sys" => "stderr"); line_buffering=true) - const _redirect_stdout_task = @async write($_py_stdout, $_redirected_stdout[1]) - const _redirect_stderr_task = @async write($_py_stderr, $_redirected_stderr[1]) + # These tasks can run while the GIL is unlocked, such as during _jl_call_nogil. + function _copy_to_python(py, pipe) + while !eof(pipe) + data = readavailable(pipe) + PythonCall.GIL.@lock write(py, data) + end + end + const _redirect_stdout_task = @async _copy_to_python(_py_stdout, _redirected_stdout[1]) + const _redirect_stderr_task = @async _copy_to_python(_py_stderr, _redirected_stderr[1]) function _flush_stdio() flush(stderr) flush(stdout) diff --git a/pytest/test_all.py b/pytest/test_all.py index 8ceaf9df..9b81de92 100644 --- a/pytest/test_all.py +++ b/pytest/test_all.py @@ -201,6 +201,39 @@ def test_call_nogil(yld, raw): assert 0.9 < t2 < 1.5 +def test_ipython_output_nogil(): + """Tests that Julia output under the IPython extension is safe while the GIL is unlocked.""" + import subprocess + import sys + + pytest.importorskip("IPython") + code = """ +import io, os, sys +from IPython.core.interactiveshell import InteractiveShell +from juliacall import Main as jl, PythonCall +import juliacall.ipython +# Julia redirects file descriptors 1 and 2, so keep copies to report through. +out = os.fdopen(os.dup(1), "w") +err = os.fdopen(os.dup(2), "w") +# Like ipykernel, sys.stdout and sys.stderr are not file descriptors 1 and 2. +sys.stdout, sys.stderr = io.StringIO(), io.StringIO() +class KernelShell(InteractiveShell): + pass +juliacall.ipython.load_ipython_extension(KernelShell.instance()) +try: + jl.seval('() -> (println("hello"); sleep(0.1))')._jl_call_nogil() + PythonCall._ipython._flush_stdio() +finally: + err.write(sys.stderr.getvalue()) +out.write(sys.stdout.getvalue()) +""" + result = subprocess.run( + [sys.executable, "-c", code], capture_output=True, text=True, timeout=180 + ) + assert result.returncode == 0, result.stderr + assert result.stdout == "hello\n" + + @pytest.mark.parametrize("mode,value", [("existing", 2), ("strict", 3)]) def test_compiled_modules_option(mode, value): import os