Skip to content

Commit

Permalink
TgBot++: android_build: Fixup python GIL and TS again
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Jul 6, 2024
1 parent 4f03bc7 commit dca7f9c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
40 changes: 20 additions & 20 deletions src/android_builder/ForkAndRun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ bool ForkAndRun::execute() {
Pipe stdout_pipe{};
Pipe stderr_pipe{};
Pipe python_pipe{};
auto* mainTS = PythonClass::get()->mainPyThreadState;

if (!stderr_pipe.pipe() || !stdout_pipe.pipe() || !python_pipe.pipe()) {
stderr_pipe.close();
Expand All @@ -32,40 +31,41 @@ bool ForkAndRun::execute() {
return false;
}

PyEval_SaveThread();
auto* mainTS = PyEval_SaveThread();
pid_t pid = fork();
if (pid == 0) {
FDLogSink sink;
auto* ts = PyThreadState_New(mainTS->interp);
PyEval_AcquireThread(ts);

absl::AddLogSink(&sink);
dup2(stdout_pipe.writeEnd(), STDOUT_FILENO);
dup2(stderr_pipe.writeEnd(), STDERR_FILENO);
close(stdout_pipe.readEnd());
close(stderr_pipe.readEnd());
close(python_pipe.readEnd());

PyObject* os = PyImport_ImportModule("os");
PyObject* os_environ = PyObject_GetAttrString(os, "environ");
PyObject* value = PyUnicode_FromString(
std::to_string(python_pipe.writeEnd()).c_str());
PyMapping_SetItemString(os_environ, "PYTHON_LOG_FD", value);

// Clean up
Py_DECREF(value);
Py_DECREF(os_environ);
Py_DECREF(os);

// Clear handlers
signal(SIGINT, [](int) {});
signal(SIGTERM, [](int) {});

int ret = runFunction() ? EXIT_SUCCESS : EXIT_FAILURE;
// Append PYTHON LOG FD
PyEval_RestoreThread(mainTS);
{
GILStateManagement _gil;
PyObject* os = PyImport_ImportModule("os");
PyObject* os_environ = PyObject_GetAttrString(os, "environ");
PyObject* value = PyUnicode_FromString(
std::to_string(python_pipe.writeEnd()).c_str());
PyMapping_SetItemString(os_environ, "PYTHON_LOG_FD", value);

// Clean up
Py_DECREF(value);
Py_DECREF(os_environ);
Py_DECREF(os);
}

int ret = 0;
ret = runFunction() ? EXIT_SUCCESS : EXIT_FAILURE;
PyEval_SaveThread();
absl::RemoveLogSink(&sink);
PyThreadState_Clear(ts);
PyThreadState_Swap(nullptr); // Ensure the current thread state is NULL
PyThreadState_Delete(ts);
_exit(ret);
} else if (pid > 0) {
Pipe program_termination_pipe{};
Expand Down
4 changes: 1 addition & 3 deletions src/android_builder/PythonClass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PythonClass : public std::enable_shared_from_this<PythonClass> {
PyInitHolder() { Py_Initialize(); }
~PyInitHolder() { Py_Finalize(); }
};
explicit PythonClass() : mainPyThreadState(PyThreadState_Get()) {}
explicit PythonClass() = default;

public:
using Ptr = std::shared_ptr<PythonClass>;
Expand Down Expand Up @@ -171,6 +171,4 @@ class PythonClass : public std::enable_shared_from_this<PythonClass> {
*/
bool addLookupDirectory(const std::filesystem::path& directory);
std::shared_ptr<ModuleHandle> importModule(const std::string& name);

PyThreadState *mainPyThreadState {};
};

0 comments on commit dca7f9c

Please sign in to comment.