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

[BUG]: Data race when using static variables with free-threaded Python #5489

Open
2 of 3 tasks
rostan-t opened this issue Jan 7, 2025 · 0 comments
Open
2 of 3 tasks
Labels
triage New bug, unverified

Comments

@rostan-t
Copy link

rostan-t commented Jan 7, 2025

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

v2.13.6

Problem description

With free-threaded Python, I get data races when using static class variables defined like this:

#include <pybind11/pybind11.h>

namespace py = pybind11;

class Foo {
};

Foo Bar;

PYBIND11_MODULE(foo, m, py::mod_gil_not_used()) {
    py::class_<Foo>(m, "Foo")
        .def_readonly_static("Bar", &Bar);
}

I get multiple kinds of errors when accessing Foo.Bar from multiple threads, from segmentation faults to "pybind11_object_dealloc(): Tried to deallocate unregistered instance!" exceptions.

Further investigation suggested that it might come from Foo.Bar being freed too many times. I'm unsure if it's a pybind11 or CPython issue so I'm posting it here.

Reproducible example code

import threading

from foo import Foo


def create_foo():
    for _ in range(1000):
        Foo.Bar


# keeping a reference to Foo.Bar prevents the issue from happening
# bar = Foo.Bar

nb_threads = 10
threads = [threading.Thread(target=create_foo) for _ in range(nb_threads)]

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

Is this a regression? Put the last known working version here if it is.

Not a regression

@rostan-t rostan-t added the triage New bug, unverified label Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage New bug, unverified
Projects
None yet
Development

No branches or pull requests

1 participant