optapy / optapy

OptaPy is an AI constraint solver for Python to optimize planning and scheduling problems.

Home Page:https://www.optapy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Access violation when running with pytest

djromberg opened this issue · comments

Without pytest, everything works like a charm. However, when running some tests with pytest, I get the following error:

========================================================= test session starts ==========================================================
platform win32 -- Python 3.9.12, pytest-7.1.2, pluggy-1.0.0
rootdir: C:\Users\dromberg\Projects\Schedool\schedool-service
plugins: anyio-3.5.0
collecting ... Windows fatal exception: access violation

Current thread 0x00001634 (most recent call first):
  File "C:\Users\dromberg\AppData\Local\pypoetry\Cache\virtualenvs\schedool-service-gPSd8MaG-py3.9\lib\site-packages\jpype\_core.py", line 218 in startJVM
  File "C:\Users\dromberg\AppData\Local\pypoetry\Cache\virtualenvs\schedool-service-gPSd8MaG-py3.9\lib\site-packages\optapy\optaplanner_java_interop.py", line 334 in init
  File "C:\Users\dromberg\AppData\Local\pypoetry\Cache\virtualenvs\schedool-service-gPSd8MaG-py3.9\lib\site-packages\optapy\optaplanner_java_interop.py", line 395 in ensure_init
  File "C:\Users\dromberg\AppData\Local\pypoetry\Cache\virtualenvs\schedool-service-gPSd8MaG-py3.9\lib\site-packages\optapy\types\types.py", line 4 in <module>
...

The stack trace shown here is shortened for readability. I was able to reproduce the issue with the following code and command:

Python

# file tests/test_code.py
from optapy.types import HardSoftScore

Command

pytest .\tests\test_code.py

It seems that starting the JVM fails somehow. Let me know if I can add anything more to help, I am no Java expert though.

What happens when you invoke pytest as pytest --import-mode=importlib .\tests\test_code.py?

PyTest is used for OptaPy tests:

pytest --import-mode=importlib
; Without changing import-mode, I get a ClassNotFoundException. Changing import-mode to importlib, all tests pass successfully and no error is logged.
JPype has a JImport module that adds an additional module loader for java packages and classes: https://jpype.readthedocs.io/en/latest/imports.html#jimport . This module is used with OptaPy, so there is a chance the standard import-mode of Pytest does not like it.

Thanks for the hint, but this option does not change anything. However, while reading through the JPype documentation, I found the section Errors reported by Python fault handler. It suggests to deactivate the Python fault handler when working with the JVM.

When I add the suggested fixture, my test runs without any error. Maybe OptaPy also makes use of this or something similar? Feel free to close this issue as it works for me now.