pybind / pybind/pybind11

Feature Request: Cancelling a Nurse/Patient Relationship for keep_alive

Open
#1,708 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

I would like to request a feature be made that cancels the keep_alive<nurse, patient> relationship. I have found a use case for callbacks which require a way to cancel the keep_alive functionality upon a python function call bound with pybind11 (unregister_callback). Please see example below:

Example:

.def("register_callback", &registerCallback, py::keep_alive<1, 2>())
.def("unregister_callback", &unregisterCallback);

class CallBack():
    ....

def func(connection):
    callback = CallBack()
    connection.regsiter_callback(callback)
    # do something
    connection.unregister_callback(callback)

func(conn)
func(conn)
func(conn)

None of the callback objects will be destroyed until the connection object is destroyed. These callbacks could be holding resources that the user expects to be freed upon exiting func(). I would like to be able to add something to the unregister_callback pybind11 definition that would allow me to cancel keep_alive when unregister_callback is called.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the py::keep_alive relationship from the register_callback binding and the unregister_callback entry point shown in the example. Define how cancellation should be requested and verify that unregister_callback allows the callback object and its resources to be released before the connection is destroyed.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.