Skip to content

Commit

Permalink
Working on stack corruption issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrameos committed Mar 2, 2024
1 parent 73ad4dd commit 07e8575
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
3 changes: 2 additions & 1 deletion native/common/jp_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ void JPContext::shutdownJVM(bool destroyJVM, bool freeJVM)
JP_TRACE_IN("JPContext::shutdown");
if (m_JavaVM == nullptr)
JP_RAISE(PyExc_RuntimeError, "Attempt to shutdown without a live JVM");
// if (m_Embedded)
if (m_Embedded)
return;
// JP_RAISE(PyExc_RuntimeError, "Cannot shutdown from embedded Python");

// Wait for all non-demon threads to terminate
Expand Down
34 changes: 30 additions & 4 deletions native/common/jp_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extern "C" JNIEXPORT void JNICALL Java_org_jpype_Main_launch(
jobjectArray args)
{
try {
int rc = 0;
// Fetch the Python modules
JPPyObject publicModule = JPPyObject::use(PyImport_ImportModule("jpype"));
JPPyObject privateModule = JPPyObject::use(PyImport_ImportModule("_jpype"));
Expand Down Expand Up @@ -73,10 +74,35 @@ extern "C" JNIEXPORT void JNICALL Java_org_jpype_Main_launch(
env->ReleaseStringUTFChars(str, c);
}

// Redirect to Python
printf("Python main start\n"); fflush(stdout);
Py_BytesMain(argc, argv);
printf("Python main end\n"); fflush(stdout);
// Set isolated mode so that Python doesn't call exit
PyConfig config;
PyConfig_InitPythonConfig(&config);
config.isolated = 1;

PyStatus status = PyConfig_SetBytesArgv(&config, argc, argv);
if (PyStatus_Exception(status)) {
PyConfig_Clear(&config);
goto exception;
}

status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
PyConfig_Clear(&config);
goto exception;
}
PyConfig_Clear(&config);

// Call Python from Java
rc = Py_RunMain();

// Problem: Python doesn't exist the main loop and return here.
// instead it finalizes and shutsdown everything even if there are
// other Java threads still using Python. This is apparently inherent
// in Python design. So it may not be possible to get them cleanly
// working together until Python splits there main loop like Java
// did

exception:

// Dump the memory
for (int i = 0; i<argc; ++i)
Expand Down
1 change: 1 addition & 0 deletions native/java/org/jpype/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ static void mainX(String[] args, String nativeLib)
// Launch Python in a new thread
Thread thread = new Thread(() -> launch(args), "Python");
thread.start();
// launch(args);
// Return control to C so we can call Destroy and wait for shutdown.
}
}
15 changes: 7 additions & 8 deletions native/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ int main(int argc, char** argv)
exit(1);
}



Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
PyObject *jpype = PyImport_ImportModule("jpype._bootstrap");
Expand Down Expand Up @@ -202,7 +200,13 @@ int main(int argc, char** argv)
env->SetObjectArrayElement(stra, j++, (env)->NewStringUTF(argv[i]));
}
jobject native = get_native(env);


/* Python may be in bad state upon returning so best to release resources early */
if (jpype_imports)
Py_DECREF(jpype_imports);
if (jpype_private)
Py_DECREF(jpype_private);

/* Call jpype main method */
env->CallStaticVoidMethod(main, mid, stra, native);

Expand All @@ -212,11 +216,6 @@ int main(int argc, char** argv)

printf("free Python resources.\n");

if (jpype_imports)
Py_DECREF(jpype_imports);
if (jpype_private)
Py_DECREF(jpype_private);

/* Java is done so we can free remaining Python resources */
if (Py_FinalizeEx() < 0) {
exit(120);
Expand Down
16 changes: 15 additions & 1 deletion setupext/makefile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import os

class Makefile:
compiler_type = "unix"
Expand All @@ -8,8 +8,10 @@ def __init__(self, actual):
self.compile_command = None
self.compile_pre = None
self.compile_post = None
self.include_dirs = actual.include_dirs
self.objects = []
self.sources = []
self.exe_extension = actual.exe_extension

def captureCompile(self, x):
command = x[0]
Expand Down Expand Up @@ -62,6 +64,18 @@ def link_shared_object(self, *args, **kwargs):
def detect_language(self, x):
return self.actual.detect_language(x)

def add_library(self, *args):
self.actual.add_library(*args)

def add_library_dir(self, *args):
self.actual.add_library_dir(*args)

def link(self, *args, **kwargs):
self.actual.spawn = self.captureCompile
rc = self.actual.link(*args, **kwargs)
return rc


def write(self):
print("Write makefile")
library = os.path.basename(self.library)
Expand Down

0 comments on commit 07e8575

Please sign in to comment.