Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions in customizers __jclass_init__ crash the JVM #1198

Open
astrelsky opened this issue Jun 27, 2024 · 8 comments · May be fixed by #1199
Open

Exceptions in customizers __jclass_init__ crash the JVM #1198

astrelsky opened this issue Jun 27, 2024 · 8 comments · May be fixed by #1199

Comments

@astrelsky
Copy link
Contributor

astrelsky commented Jun 27, 2024

Below is a silly example to reproduce the problem. I just picked a class at random that didn't have a customizer provided by jpype. There is no purpose for a customizer for this class other then demonstrating the issue.

import jpype
from jpype.imports import *

jpype.startJVM()

@jpype.JImplementationFor("java.io.Console")
class Console:
    def __jclass_init__(self):
        raise Exception("hi")


from java.io import Console

The crash appears to be from an uncaught exception in PyJPClass_hook around (pyjp_class.cpp:1200)

@Thrameos
Copy link
Contributor

I did not get a crash on my machine. Perhaps we need more details in terms of versions or arch....

Traceback (most recent call last):
  File "test3.py", line 11, in <module>
    from java.io import Console
  File "<frozen importlib._bootstrap>", line 1075, in _handle_fromlist
Exception: hi

@astrelsky
Copy link
Contributor Author

astrelsky commented Jun 27, 2024

I did not get a crash on my machine. Perhaps we need more details in terms of versions or arch....

Traceback (most recent call last):
  File "test3.py", line 11, in <module>
    from java.io import Console
  File "<frozen importlib._bootstrap>", line 1075, in _handle_fromlist
Exception: hi

Windows 10 and 11. JDK 21, 22 and JDK 8. Python 3.11.7, 3.12.1 and 3.8.10. I tried the lowest supported versions of Java and Python that had pre-built wheels for Jpype1 (to eliminate variables), that I could get my hands on.

Also I had to debug the setup.py script and step through it to change the flags to get a release build with debug info. If you do setup.py build_ext -g it will exit as soon as you import jpype and complain about the gil. I did this on a machine I can't share from but if you need it I'd be more than happy to do it on my laptop and provide a copy of the pyd and pdb

@Thrameos
Copy link
Contributor

Thrameos commented Jun 27, 2024

Got it. Will try though I have a few other items in the queue already.

Edit: I was successfully able to replicate the issue with Windows 11 so I have a place to work from.

@Thrameos
Copy link
Contributor

I understand the issue. The problem is to prevent name mangling we are declaring functions as extern "C" but also supplying /EHsc which means that they are not allowed to throw. Because of this we end up with an exception which is going from C++ to C++ failing. Either we have to remove all extern "C" declarations or switch to ``/EHa /EHs" when compiling.

The problem was compounded because the extern "C" was in the header rather than definition. Thus absolutely nothing appears wrong in the code.

Thrameos added a commit to Thrameos/jpype that referenced this issue Jun 27, 2024
@Thrameos Thrameos linked a pull request Jun 27, 2024 that will close this issue
@Thrameos
Copy link
Contributor

A PR with the fix is posted. It will be included in the next release. Though I haven't seen any responses from the maintainer in a while so not sure when it will get reviewed.

@astrelsky
Copy link
Contributor Author

astrelsky commented Jun 28, 2024

A PR with the fix is posted. It will be included in the next release. Though I haven't seen any responses from the maintainer in a while so not sure when it will get reviewed.

Nice, that was quick. Thank you very much.

@Thrameos
Copy link
Contributor

It was a one liner. So not that impressive. The longer job would be verifying all of the other calls in module dont have the same problem.

@astrelsky
Copy link
Contributor Author

It was a one liner. So not that impressive. The longer job would be verifying all of the other calls in module don't have the same problem.

I did take a quick look yesterday and I think everything else is ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants