jpype-project / jpype

JPype is cross language bridge to allow Python programs full access to Java class libraries.
http://www.jpype.org
Apache License 2.0
1.12k stars 182 forks source link

RuntimeError: Unable to attach to thread #1244

Open boravinod145 opened 3 days ago

boravinod145 commented 3 days ago

Attaching a new thread through jpype.attachThreadToJVM after the JVM has started works perfectly for each API call. However, after a few days of running the instance, it failed to attach a new thread to the JVM and returned the following error.

File "/src/api/views.py", line 130, in convert
  core.initialise_jpype()
File "/src/app/core.py", line 34, in initialise_jpype
  jpype.attachThreadToJVM()
File "/usr/local/lib/python3.10/site-packages/jpype/_core.py", line 76, in func2
  return func2._real(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/jpype/_core.py", line 388, in attachThreadToJVM
  _jpype.attachThreadToJVM()
RuntimeError: Unable to attach to thread

Even in jpype.isJVMStarted() gives true.

Thrameos commented 2 days ago

It sounds like ot ran out of handles. JPype automatically attaches to threads so there is no reason to explicitely call attach. It isn't harmful, but hasn't been necessary for years. There is however a reason to detach. If you are making lots of threads, you must detach them at the end of life or eventually you will exhaust the handle pool.

I am fairly sure this appears in the JPype documentation.