Jep.close() does not close daemon thread? error: [Py_EndInterpreter: not the last thread]

154 views
Skip to first unread message

Xianda Ke

unread,
Aug 20, 2018, 11:47:52 PM8/20/18
to Jep Project
Hi folks,

java code:
try(Jep jep = new Jep()) {
jep.runScript("./src/main/python/thread_test.py");
} catch (JepException e) {
e.printStackTrace();
}
}

python code:
import threading
import time
import sys
# import thread
def f1():
for i in range(5):
print ("f1...\n")
sys.stdout.flush()
time.sleep(1)

if __name__ == "__main__":

t = threading.Thread(target=f1, args=())
t.setDaemon(True)
t.start()

for t in threading.enumerate():
print (t.getName())
#if t.getName() != "MainThread":
#t._Thread__stop()
#t._stop()
print (threading.enumerate())


When Jep.close() is called,  there is a fatal error:
Fatal Python error: Py_EndInterpreter: not the last thread.

it seems that pyembed_thread_close() should not call Py_EndInterpreter() before all the daemon threads are close.

    if (jepThread->tstate->interp == mainThreadState->interp) {
        PyThreadState_Clear(jepThread->tstate);
        PyThreadState_Swap(NULL);
        PyThreadState_Delete(jepThread->tstate);
    } else {
        Py_EndInterpreter(jepThread->tstate);
    }


Ben Steffensmeier

unread,
Aug 21, 2018, 9:31:43 PM8/21/18
to Jep Project
In general if you use any system resources, such as threads, from python using jep you are responsible for ensuring that they are cleaned up so I would recommend you add a wait before the close. I do not think jep should wait for all daemon threads because they may never complete causing a freeze Last time this issue was seen it was in a third party library and the thread would never complete without intervention. I do not like that this results in a fatal error, I will investigate further and see if we can detect this and throw a JepException and avoid the fatal error.

Ben

Xianda Ke

unread,
Aug 21, 2018, 9:41:24 PM8/21/18
to Ben Steffensmeier, Jep Project
Thank Ben for the reply. 

--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/32bbdc22-0f8a-42e4-99b5-1c1a66db4353%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Ke, Xianda

Nathan Jensen

unread,
Aug 22, 2018, 12:04:04 PM8/22/18
to Jep Project
I opened a github issue for this.  https://github.com/ninia/jep/issues/161  At this time I'm not sure what release this will go into.

To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.

To post to this group, send email to jep-p...@googlegroups.com.



--
Ke, Xianda

--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.

To post to this group, send email to jep-p...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages