mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-27 15:42:01 +00:00
b07d08f600
* Add option to force the use of the PYPY GIL scoped acquire/release logic to support nested gil access, see https://github.com/pybind/pybind11/issues/1276 and https://github.com/pytorch/pytorch/issues/83101 * Apply suggestions from code review * Update CMakeLists.txt * docs: update upgrade guide * Update docs/upgrade.rst * All bells & whistles. * Add Reminder to common.h, so that we will not forget to purge `!WITH_THREAD` branches when dropping Python 3.6 * New sentence instead of semicolon. * Temporarily pull in snapshot of PR #4246 * Add `test_release_acquire` * Add more unit tests for nested gil locking * Add test_report_builtins_internals_keys * Very minor enhancement: sort list only after filtering. * Revert change in docs/upgrade.rst * Add test_multi_acquire_release_cross_module, while also forcing unique PYBIND11_INTERNALS_VERSION for cross_module_gil_utils.cpp * Hopefully fix apparently new ICC error. ``` 2022-10-28T07:57:54.5187728Z -- The CXX compiler identification is Intel 2021.7.0.20220726 ... 2022-10-28T07:58:53.6758994Z icpc: remark #10441: The Intel(R) C++ Compiler Classic (ICC) is deprecated and will be removed from product release in the second half of 2023. The Intel(R) oneAPI DPC++/C++ Compiler (ICX) is the recommended compiler moving forward. Please transition to use this compiler. Use '-diag-disable=10441' to disable this message. 2022-10-28T07:58:54.5801597Z In file included from /home/runner/work/pybind11/pybind11/include/pybind11/detail/../detail/type_caster_base.h(15), 2022-10-28T07:58:54.5803794Z from /home/runner/work/pybind11/pybind11/include/pybind11/detail/../cast.h(15), 2022-10-28T07:58:54.5805740Z from /home/runner/work/pybind11/pybind11/include/pybind11/detail/../attr.h(14), 2022-10-28T07:58:54.5809556Z from /home/runner/work/pybind11/pybind11/include/pybind11/detail/class.h(12), 2022-10-28T07:58:54.5812154Z from /home/runner/work/pybind11/pybind11/include/pybind11/pybind11.h(13), 2022-10-28T07:58:54.5948523Z from /home/runner/work/pybind11/pybind11/tests/cross_module_gil_utils.cpp(13): 2022-10-28T07:58:54.5949009Z /home/runner/work/pybind11/pybind11/include/pybind11/detail/../detail/internals.h(177): error #2282: unrecognized GCC pragma 2022-10-28T07:58:54.5949374Z PYBIND11_TLS_KEY_INIT(tstate) 2022-10-28T07:58:54.5949579Z ^ 2022-10-28T07:58:54.5949695Z ``` * clang-tidy fixes * Workaround for PYPY WIN exitcode None * Revert "Temporarily pull in snapshot of PR #4246" This reverts commit 23ac16e859150f27fda25ca865cabcb4444e0770. * Another workaround for PYPY WIN exitcode None * Clean up how the tests are run "run in process" Part 1: uniformity * Clean up how the tests are run "run in process" Part 2: use `@pytest.mark.parametrize` and clean up the naming. * Skip some tests `#if defined(THREAD_SANITIZER)` (tested with TSAN using the Google-internal toolchain). * Run all tests again but ignore ThreadSanitizer exitcode 66 (this is less likely to mask unrelated ThreadSanitizer issues in the future). * bug fix: missing common.h include before using `PYBIND11_SIMPLE_GIL_MANAGEMENT` For the tests in the github CI this does not matter, because `PYBIND11_SIMPLE_GIL_MANAGEMENT` is always defined from the command line, but when monkey-patching common.h locally, it matters. * if process.exitcode is None: assert t_delta > 9.9 * More sophisiticated `_run_in_process()` implementation, clearly reporting `DEADLOCK`, additionally exercised via added `intentional_deadlock()` * Wrap m.intentional_deadlock in a Python function, for `ForkingPickler` compatibility. ``` > ForkingPickler(file, protocol).dump(obj) E TypeError: cannot pickle 'PyCapsule' object ``` Observed with all Windows builds including mingw but not PyPy, and macos-latest with Python 3.9, 3.10, 3.11 but not 3.6. * Add link to potential solution for WOULD-BE-NICE-TO-HAVE feature. * Add `SKIP_IF_DEADLOCK = True` option, to not pollute the CI results with expected `DEADLOCK` failures while we figure out what to do about them. * Add COPY-PASTE-THIS: gdb ... command (to be used for debugging the detected deadlock) * style: pre-commit fixes * Do better than automatic pre-commit fixes. * Add `PYBIND11_SIMPLE_GIL_MANAGEMENT` to `pytest_report_header()` (so that we can easily know when harvesting deadlock information from the CI logs). Co-authored-by: Arnim Balzer <arnim@seechange.ai> Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com> Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
145 lines
5.2 KiB
C++
145 lines
5.2 KiB
C++
/*
|
|
tests/test_gil_scoped.cpp -- acquire and release gil
|
|
|
|
Copyright (c) 2017 Borja Zarco (Google LLC) <bzarco@google.com>
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
BSD-style license that can be found in the LICENSE file.
|
|
*/
|
|
|
|
#include <pybind11/functional.h>
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
#define CROSS_MODULE(Function) \
|
|
auto cm = py::module_::import("cross_module_gil_utils"); \
|
|
auto target = reinterpret_cast<void (*)()>(PyLong_AsVoidPtr(cm.attr(Function).ptr()));
|
|
|
|
class VirtClass {
|
|
public:
|
|
virtual ~VirtClass() = default;
|
|
VirtClass() = default;
|
|
VirtClass(const VirtClass &) = delete;
|
|
virtual void virtual_func() {}
|
|
virtual void pure_virtual_func() = 0;
|
|
};
|
|
|
|
class PyVirtClass : public VirtClass {
|
|
void virtual_func() override { PYBIND11_OVERRIDE(void, VirtClass, virtual_func, ); }
|
|
void pure_virtual_func() override {
|
|
PYBIND11_OVERRIDE_PURE(void, VirtClass, pure_virtual_func, );
|
|
}
|
|
};
|
|
|
|
TEST_SUBMODULE(gil_scoped, m) {
|
|
m.attr("defined_THREAD_SANITIZER") =
|
|
#if defined(THREAD_SANITIZER)
|
|
true;
|
|
#else
|
|
false;
|
|
#endif
|
|
|
|
m.def("intentional_deadlock",
|
|
[]() { std::thread([]() { py::gil_scoped_acquire gil_acquired; }).join(); });
|
|
|
|
py::class_<VirtClass, PyVirtClass>(m, "VirtClass")
|
|
.def(py::init<>())
|
|
.def("virtual_func", &VirtClass::virtual_func)
|
|
.def("pure_virtual_func", &VirtClass::pure_virtual_func);
|
|
|
|
m.def("test_callback_py_obj", [](py::object &func) { func(); });
|
|
m.def("test_callback_std_func", [](const std::function<void()> &func) { func(); });
|
|
m.def("test_callback_virtual_func", [](VirtClass &virt) { virt.virtual_func(); });
|
|
m.def("test_callback_pure_virtual_func", [](VirtClass &virt) { virt.pure_virtual_func(); });
|
|
m.def("test_cross_module_gil_released", []() {
|
|
CROSS_MODULE("gil_acquire_funcaddr")
|
|
py::gil_scoped_release gil_release;
|
|
target();
|
|
});
|
|
m.def("test_cross_module_gil_acquired", []() {
|
|
CROSS_MODULE("gil_acquire_funcaddr")
|
|
py::gil_scoped_acquire gil_acquire;
|
|
target();
|
|
});
|
|
m.def("test_cross_module_gil_inner_custom_released", []() {
|
|
CROSS_MODULE("gil_acquire_inner_custom_funcaddr")
|
|
py::gil_scoped_release gil_release;
|
|
target();
|
|
});
|
|
m.def("test_cross_module_gil_inner_custom_acquired", []() {
|
|
CROSS_MODULE("gil_acquire_inner_custom_funcaddr")
|
|
py::gil_scoped_acquire gil_acquire;
|
|
target();
|
|
});
|
|
m.def("test_cross_module_gil_inner_pybind11_released", []() {
|
|
CROSS_MODULE("gil_acquire_inner_pybind11_funcaddr")
|
|
py::gil_scoped_release gil_release;
|
|
target();
|
|
});
|
|
m.def("test_cross_module_gil_inner_pybind11_acquired", []() {
|
|
CROSS_MODULE("gil_acquire_inner_pybind11_funcaddr")
|
|
py::gil_scoped_acquire gil_acquire;
|
|
target();
|
|
});
|
|
m.def("test_cross_module_gil_nested_custom_released", []() {
|
|
CROSS_MODULE("gil_acquire_nested_custom_funcaddr")
|
|
py::gil_scoped_release gil_release;
|
|
target();
|
|
});
|
|
m.def("test_cross_module_gil_nested_custom_acquired", []() {
|
|
CROSS_MODULE("gil_acquire_nested_custom_funcaddr")
|
|
py::gil_scoped_acquire gil_acquire;
|
|
target();
|
|
});
|
|
m.def("test_cross_module_gil_nested_pybind11_released", []() {
|
|
CROSS_MODULE("gil_acquire_nested_pybind11_funcaddr")
|
|
py::gil_scoped_release gil_release;
|
|
target();
|
|
});
|
|
m.def("test_cross_module_gil_nested_pybind11_acquired", []() {
|
|
CROSS_MODULE("gil_acquire_nested_pybind11_funcaddr")
|
|
py::gil_scoped_acquire gil_acquire;
|
|
target();
|
|
});
|
|
m.def("test_release_acquire", [](const py::object &obj) {
|
|
py::gil_scoped_release gil_released;
|
|
py::gil_scoped_acquire gil_acquired;
|
|
return py::str(obj);
|
|
});
|
|
m.def("test_nested_acquire", [](const py::object &obj) {
|
|
py::gil_scoped_release gil_released;
|
|
py::gil_scoped_acquire gil_acquired_outer;
|
|
py::gil_scoped_acquire gil_acquired_inner;
|
|
return py::str(obj);
|
|
});
|
|
m.def("test_multi_acquire_release_cross_module", [](unsigned bits) {
|
|
py::set internals_ids;
|
|
internals_ids.add(PYBIND11_INTERNALS_ID);
|
|
{
|
|
py::gil_scoped_release gil_released;
|
|
auto thread_f = [bits, &internals_ids]() {
|
|
py::gil_scoped_acquire gil_acquired;
|
|
auto cm = py::module_::import("cross_module_gil_utils");
|
|
auto target = reinterpret_cast<std::string (*)(unsigned)>(
|
|
PyLong_AsVoidPtr(cm.attr("gil_multi_acquire_release_funcaddr").ptr()));
|
|
std::string cm_internals_id = target(bits >> 3);
|
|
internals_ids.add(cm_internals_id);
|
|
};
|
|
if ((bits & 0x1u) != 0u) {
|
|
thread_f();
|
|
}
|
|
if ((bits & 0x2u) != 0u) {
|
|
std::thread non_python_thread(thread_f);
|
|
non_python_thread.join();
|
|
}
|
|
if ((bits & 0x4u) != 0u) {
|
|
thread_f();
|
|
}
|
|
}
|
|
return internals_ids;
|
|
});
|
|
}
|