Skip to content

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

Closed
@rostan-t

Description

@rostan-t

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions