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

Jep should throw exception when closing a Jep sub-interpreter instance that has threads still running #161

Closed
ndjensen opened this issue Aug 22, 2018 · 2 comments

Comments

@ndjensen
Copy link
Member

ndjensen commented Aug 22, 2018

See https://groups.google.com/forum/#!topic/jep-project/9qnpnNBipXA

If a Jep instance is closed while a thread is still running inside that sub-interpreter, the process/application will exit and shut down.

Example:

try(Jep jep = new Jep()) {
    jep.runScript("./src/main/python/thread_test.py");
} catch (JepException e) {
    e.printStackTrace();
}
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())

Current C code in pyembed.c's pyembed_thread_close():

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

When Jep.close() is called, there is a fatal error:
Fatal Python error: Py_EndInterpreter: not the last thread. Py_EndInterpreter(jepThread->tstate); eventually uses Py_FatalError which will end the process. https://github.com/python/cpython/blob/46dc4e34ed8005a688d7f3512844ef227a3465f4/Python/pylifecycle.c#L1506

Jep should attempt to detect this situation and throw an exception indicating the developer needs to manage their threads, preventing an exit and shutdown of the process.

@bsteffensmeier
Copy link
Member

This is fixed in dev_3.9 with d36a4b9.

@bsteffensmeier
Copy link
Member

Jep 3.9.0 has been released

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

No branches or pull requests

2 participants