Skip to content

Deadlock with std::function argument #1525

@davidhewitt

Description

@davidhewitt

Issue description

I can easily reproduce a deadlock which seems to be related to gil_scoped_acquire and std::function<> arguments, when being called from a thread other than the main thread.

Reproducible example code

In particular, this binding code:

#include <pybind11/pybind11.h>
#include <pybind11/functional.h>

PYBIND11_MODULE(deadlock, m)
{
  m.def("deadlock", [] (std::function<void ()> f) { f(); });
}

Deadlocks with the following Python code:

import deadlock
from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor(max_workers=2) as e:
    e.submit(deadlock.deadlock, lambda: None)
    e.submit(deadlock.deadlock, lambda: None)

Or also with this:

import deadlock
from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor(max_workers=1) as e:
    e.submit(deadlock.deadlock, lambda: None)

with ThreadPoolExecutor(max_workers=1) as e:
    e.submit(deadlock.deadlock, lambda: None)

Something about two calls seems significant, I can't reproduce with just one.

I can spend some time on the weekend writing a patch if you are able to point me to what is probably going wrong :)

Thanks
David

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions