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

<mutex>: Problem using mutex with pybind. #4875

Closed
zizhang-qiu opened this issue Aug 3, 2024 · 3 comments
Closed

<mutex>: Problem using mutex with pybind. #4875

zizhang-qiu opened this issue Aug 3, 2024 · 3 comments
Labels
external This issue is unrelated to the STL

Comments

@zizhang-qiu
Copy link

Describe the bug

I use std::lock_guard and std::unique_lock in my code and use pybind to generate python library.
The program crashed in python but not in C++.

Command-line test case

C++
#include "pybind11/pybind11.h"
#include <mutex>
class ThreadSafeCounter {
 public:
  ThreadSafeCounter() : count(0) {}

  void increment() {
    std::lock_guard<std::mutex> guard(mutex_);
    ++count;
  }

  int get() const {
    std::lock_guard<std::mutex> guard(mutex_);
    return count;
  }

 private:
  mutable std::mutex mutex_;
  int count;
};

PYBIND11_MODULE(pybindtest, m) {
  py::class_<ThreadSafeCounter>(m, "ThreadSafeCounter")
      .def(py::init<>())
      .def("increment", &ThreadSafeCounter::increment)
      .def("get", &ThreadSafeCounter::get);
}

Python
import pybindtest
counter = pybindtest.ThreadSafeCounter()
counter.increment()
print(counter.get())

Expected behavior

The python program should run successfully and output "1".

STL version

Microsoft Visual Studio Community 2022
Version 17.10.5

Additional context

I switch to visual studio 2019 and this bug disappears.
Microsoft Visual Studio Professional 2019
Version 16.11.38

@zizhang-qiu zizhang-qiu changed the title <mutex>: Problem using mutex wiht pybind. <mutex>: Problem using mutex with pybind. Aug 3, 2024
@AlexGuteniev
Copy link
Contributor

Looks like #4730

You need to make sure Python uses the latest C++ runtime library.

@zizhang-qiu
Copy link
Author

Thanks. I don't know how to make sure Python uses the latest C++ runtime library, but I solved this by adding the macro _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR in my CMakelist.

@StephanTLavavej StephanTLavavej added the external This issue is unrelated to the STL label Aug 5, 2024
@StephanTLavavej
Copy link
Member

Yep, thanks @AlexGuteniev. Ideally this should be reported upstream (to pybind?), if they aren't redistributing a sufficiently new VCRedist (either system-wide or app-local), assuming this issue is in pybind itself and not in the usage of it. While our documented requirement is to have an equal or newer version, in this case one can physically get away with a VCRedist as old as VS 2022 17.8 being used with VS 2022 17.10's headers, so the VCRedist has to be quite old in order for this to be a problem.

@StephanTLavavej StephanTLavavej closed this as not planned Won't fix, can't repro, duplicate, stale Aug 5, 2024
fakufaku added a commit to LCAV/pyroomacoustics that referenced this issue Aug 7, 2024
fakufaku added a commit to LCAV/pyroomacoustics that referenced this issue Aug 7, 2024
fakufaku added a commit to LCAV/pyroomacoustics that referenced this issue Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external This issue is unrelated to the STL
Projects
None yet
Development

No branches or pull requests

3 participants