Skip to content

[bug] Fix crash when using custom java object in python async thread#95

Open
bgeng777 wants to merge 1 commit intoalibaba:release-0.5from
bgeng777:pemja-93
Open

[bug] Fix crash when using custom java object in python async thread#95
bgeng777 wants to merge 1 commit intoalibaba:release-0.5from
bgeng777:pemja-93

Conversation

@bgeng777
Copy link
Collaborator

For #93 . When a Java object is passed from a pemja thread to a Python async thread, calling a
method that returns a custom Java object crashes the JVM with SIGSEGV. Methods
returning built-in types (String, List, etc.) work normally.

# SIGSEGV (0xb) — JcpPyJObject_New+0x158
# NULL dereference at 0x40 (offset of name_to_attrs in JcpThread)

Cause

pyjobject_init (pyjobject.c) calls JcpThread_Get() to access the per-thread
attribute cache. On non-pemja threads, this returns NULL — which is immediately
dereferenced without a NULL check:

jcpThread = JcpThread_Get();           // NULL on async threads
if (jcpThread->name_to_attrs == NULL)  // SIGSEGV

Built-in types bypass this path — they have dedicated converters in
JcpPyObject_FromJObject that never call JcpThread_Get(). Only custom objects,
Collection, Iterable, and Iterator go through JcpPyJObject_Newpyjobject_init.

Fix

File: src/main/c/pemja/core/python_class/pyjobject.c

When JcpThread_Get() returns NULL, clear the Python error and skip the per-thread
cache — build the attrs dict directly and manage reference counts without the cache.

  • pemja threads: unchanged, cache works as before.
  • Non-pemja threads: custom Java objects now work, with a minor cost of repeated
    JNI reflection (no cache).

@wenjin272
Copy link

if (!ret && !PyErr_Occurred()) {

I tested this patch locally and found that an exception is thrown first when JcpThread is NULL. After commenting out this logic, it appears to work.

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 this pull request may close these issues.

2 participants