# Request: Can we get a c-api hook into PyContext\_Enter and PyContext\_Exit

**URL:** <https://discuss.python.org/t/request-can-we-get-a-c-api-hook-into-pycontext-enter-and-pycontext-exit/51730>\
**Category:** Async-SIG\
**Tags:** asyncio\
**Created:** [April 23, 2024, 6:05pm UTC](https://discuss.python.org/t/request-can-we-get-a-c-api-hook-into-pycontext-enter-and-pycontext-exit/51730 "2024-04-23T18:05:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fried](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fried/32/91_2.png) [@fried](https://discuss.python.org/u/fried)\
**Post date:** [April 23, 2024, 6:05pm UTC](https://discuss.python.org/t/request-can-we-get-a-c-api-hook-into-pycontext-enter-and-pycontext-exit/51730/1 "2024-04-23T18:05:49Z")

</div>

Some Background:  
Ok so at Meta we have this large C++ codebase that we call into from python a ton. That C++ framework “folly” has a concept of a context like python’s context vars. Back in 3.6, someone internally to Meta/Facebook started modifying the `asyncio.Task._step` at runtime so they could check to see if the C++ contextvar system needed to be updated every time a task is run. Well `Task._step` became `Task.__step` in 3.7 and ever since we have been forward porting local changes so we can hook into Task switching.

The Request:  
Now hooking into tasks is probably the wrong place to do this, and I think we could be better served integrating to the Python ContextVar concept. Now All I need is a way to hook into contextvar enter/exits so we can use that to switch the C++ level contextvar, right now I’m just thinking of a void\* callback that we pass the ctx\* object to if its not null.

I don’t think we need more than one callback since nobody has ever asked for this before. Maybe a second api for enter/exit to get the value of the current callback so they could be chained programmatically in the unlikely event there are two things wanting the hook into contextvar switching.

@itamaro is working on forwarding the \_step functionality to our new internal 3.12 and its segfaulting and I was wondering is this the signal we need to do this right.

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [April 24, 2024, 1:18am UTC](https://discuss.python.org/t/request-can-we-get-a-c-api-hook-into-pycontext-enter-and-pycontext-exit/51730/2 "2024-04-24T01:18:23Z")

</div>

Hi Jason, it’s bad timing, as the feature freeze for 3.13 (aka beta 1) is May 7, and everyone on the core team is already busy getting their stuff in / polished.

If you’re okay with aiming for 3.14, we should talk. Have you looked at the code for `PyContext_Enter()` yet? Do you need separate enter/exit hooks? What would the scope of the hook be? Thread state, or per interpreter? Can the hook raise exceptions?

---

<div class="post-metadata">

**Author:** ![itamaro](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/itamaro/32/20137_2.png) [@itamaro](https://discuss.python.org/u/itamaro)\
**Post date:** [April 24, 2024, 9:48pm UTC](https://discuss.python.org/t/request-can-we-get-a-c-api-hook-into-pycontext-enter-and-pycontext-exit/51730/3 "2024-04-24T21:48:35Z")

</div>

It came up in an offline conversation, so repeating what I (tried to) say here:

1. I got our `Task._step` patch working with 3.12 - segfault is gone 🙂
2. No pressure to have something in 3.13. We are perfectly comfortable backporting things from 3.14 to our internal copy as needed.
3. Since PyCon US this year is _after_ 3.13 beta cut – shall we use that opportunity to flesh out the details of the desired hook?

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [April 24, 2024, 10:29pm UTC](https://discuss.python.org/t/request-can-we-get-a-c-api-hook-into-pycontext-enter-and-pycontext-exit/51730/4 "2024-04-24T22:29:31Z")

</div>

I’d totally be amenable to getting together e.g. at the post-conference sprints and doing a bit of a design brainstorm. @yselivanov (contextvars inventor) will also be at PyCon, though I don’t know if he’s planning to stay for the sprints.

---

<div class="post-metadata">

**Author:** ![brettlangdon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brettlangdon/32/12344_2.png) [@brettlangdon](https://discuss.python.org/u/brettlangdon)\
**Post date:** [May 9, 2024, 3:02pm UTC](https://discuss.python.org/t/request-can-we-get-a-c-api-hook-into-pycontext-enter-and-pycontext-exit/51730/5 "2024-05-09T15:02:10Z")

</div>

This is similar to a topic I started in the Ideas section, and I was just about to try and start conversations again for it. Maybe the context I have is useful for this discussion?

(this is less about context enter/exit, but more for task enter/exit, so understand if we aren’t really in the same ballpark here)

> [@Add asyncio monitoring hooks](https://discuss.python.org/t/add-asyncio-monitoring-hooks/26426):
>
> [Similar previous discussion](https://discuss.python.org/t/adding-monitoring-hooks-to-the-event-loop/3448) As an APM vendor (Datadog), I’d love to be able to provide deeper insights/metrics about the asyncio event loop for our customers. Some of the metrics I have been thinking about so far are: Over the lifetime of the process total number of tasks created/finished total number of context switches min/avg/p95/p99/max/etc blocking time per-task run Per-task total number of context switches total blocking vs idle time I’d also like to be able to associate the per…
