pyjulia icon indicating copy to clipboard operation
pyjulia copied to clipboard

Segfault when using pytest and FastAPI

Open algorithmich opened this issue 2 years ago • 3 comments

I have a Python rest API (FastAPI) that calls julia functions.

When trying to run tests, I get the following error:

  • In Ubuntu 20.04
test_test.py 
signal (11): Segmentation fault
in expression starting at none:0
Allocations: 19557974 (Pool: 19550307; Big: 7667); GC: 24
Segmentation fault (core dumped)
  • In Windows
self = <julia.core.Julia object at 0x000002673F4D8220>src = '\n            PyCall.pyfunctionret(\n                (x) -> Base.eval(Main, :(spectra = $x)),\n                Any,\n                PyCall.PyAny)\n            '
    def _call(self, src):
        """
        Low-level call to execute a snippet of Julia source.
        This only raises an exception if Julia itself throws an error, but it
        does NO type conversion into usable Python objects nor any memory
        management. It should never be used for returning the result of Julia
        expressions, only to execute statements.
        """
        # logger.debug("_call(%s)", src)
>       ans = self.api.jl_eval_string(src.encode('utf-8'))
E       OSError: exception: access violation reading 0x0000000000000018
..\..\..\Anaconda3\envs\test\lib\site-packages\julia\core.py:554: OSError
============================================================================= short test summary info ============================================================================== 
FAILED tests/test_prediction.py::test_prediction - OSError: exception: access violation reading 0x0000000000000018
================================================================================ 1 failed in 42.62s ================================================================================

How to reproduce:

# main.py
from fastapi import FastAPI
from julia.api import Julia
jl = Julia(compiled_modules=False, debug=True)
from julia import Main

app = FastAPI()

@app.post("/test/")
def testtest():
    jl.eval("println(cos(0))")
    return 0

Test called with pytest -s -p julia.pytestplugin --julia-compiled-modules=no

# test_test.py
from fastapi.testclient import TestClient
import json
from main import app

client = TestClient(app)

def test_prediction():
    response = client.post("/test/")
    print(response)

PyJulia 0.6.0 Python 3.9.5 Julia 1.7.2 pytest-7.2.0 pluggy-1.0.0

Any help is much appreciated!

algorithmich avatar Jan 26 '23 09:01 algorithmich

On what operating system did this occur?

mkitti avatar Feb 27 '23 22:02 mkitti

As mentioned in the output snippets:

  • Ubuntu 20.04
  • Windows 10

algorithmich avatar Mar 02 '23 10:03 algorithmich

Does this occur with https://pypi.org/project/juliacall/ ?

mkitti avatar Mar 03 '23 04:03 mkitti