alibaba / pemja

Apache License 2.0
91 stars 26 forks source link

Recursive callbacks cause a deadlock #14

Closed ljeub-pometry closed 2 years ago

ljeub-pometry commented 2 years ago

Hi,

I am trying to use PEMJA to build an interface between python and scala code. This is working fine for simple cases, however I run into a deadlock in the following situation:

  1. Call python code from jvm
  2. Python code calls a jvm method
  3. The jvm method tries to call python again (deadlock)

Things I have tried to work around this issue:

Do you have any guidance on how to work around this issue?

HuangXingBo commented 2 years ago

@ljeub-pometry Thanks for your usage. We don't have step 3 such use cases before, I will try your case to see where the problem is.

HuangXingBo commented 2 years ago

@ljeub-pometry I have figured out the cause of the error. The reason is that the PythonInterperter you use in the main thread will hold the GIL of Python during the calling process. When the calling process has not finished, you try to call to create a new PythonInterperter. During the creation process, will go to lock GIL, which leads to deadlock.

HuangXingBo commented 2 years ago

I'll do some experimenting and see how to meet this need

ljeub-pometry commented 2 years ago

Thank you for looking into it. Ideally, this situation shouldn't require more than one python thread at all, i.e., when calling a jvm method, it probably should release the GIL while python is waiting for the result, such that the interpreter is free to execute the step 3 callback above.

HuangXingBo commented 2 years ago

@ljeub-pometry Yes. You are right. Before calling Java Function, we need to release GIL just like numpy do. I will fix this issue asap.

HuangXingBo commented 2 years ago

I have created PR https://github.com/alibaba/pemja/pull/15 to fix it

HuangXingBo commented 2 years ago

I have merged the fix into master branch and release-0.2 branch

ljeub-pometry commented 2 years ago

Thank you for the quick fix! I just tested it and this does indeed fixes the deadlock!

HuangXingBo commented 2 years ago

@ljeub-pometry Thanks for your verification.

HuangXingBo commented 2 years ago

Hi @ljeub-pometry , do you plan to take use of pemja in Raphtory?

ljeub-pometry commented 2 years ago

We are still in the early stages of adding python support to Raphtory but it does look like pemja is the most viable option for executing python code in the backend in terms of performance.

HuangXingBo commented 2 years ago

Ok. If you have any needs and ideas for pemja, welcome to exchange. According to the plan, in the next version, pemja will provide support on numpy and windows