forked from pybind/pybind11
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix how thread states are created (pybind#1276)
Having pybind11 keep its own internal thread state can lead to an inconsistent situation where the Python interpreter has a thread state but pybind does not, and then when gil_scoped_acquire is called, pybind creates a new thread state instead of using the one created by the Python interpreter. This change gets rid of pybind's internal thread state and always uses the one created by the Python interpreter.
- Loading branch information
1 parent
2d0507d
commit 5b8ef2c
Showing
6 changed files
with
55 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "pybind11_tests.h" | ||
|
||
#if defined(WITH_THREAD) | ||
|
||
#include <thread> | ||
|
||
static bool check_threadstate() { | ||
return PyGILState_GetThisThreadState() == PyThreadState_Get(); | ||
} | ||
|
||
TEST_SUBMODULE(threads, m) { | ||
m.def("check_pythread", []() -> bool { | ||
py::gil_scoped_acquire acquire1; | ||
return check_threadstate(); | ||
}, py::call_guard<py::gil_scoped_release>()) | ||
.def("check_cthread", []() -> bool { | ||
bool result = false; | ||
std::thread thread([&result]() { | ||
py::gil_scoped_acquire acquire; | ||
result = check_threadstate(); | ||
}); | ||
thread.join(); | ||
return result; | ||
}, py::call_guard<py::gil_scoped_release>()); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pytest | ||
|
||
pytestmark = pytest.requires_threading | ||
|
||
with pytest.suppress(ImportError): | ||
import threading | ||
from pybind11_tests import threads as t | ||
|
||
|
||
def test_threads(): | ||
def pythread_routine(): | ||
threading.current_thread()._return = t.check_pythread() | ||
|
||
thread = threading.Thread(target=pythread_routine) | ||
thread.start() | ||
thread.join() | ||
assert thread._return | ||
|
||
assert t.check_cthread() |