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

gh-124872: Replace enter/exit events with "switched" #124776

Merged
merged 1 commit into from
Oct 14, 2024

Conversation

rhansen
Copy link
Contributor

@rhansen rhansen commented Sep 30, 2024

Users want to know when the current context switches to a different context object. Right now this happens when and only when a context is entered or exited, so the enter and exit events are synonymous with "switched". However, if the changes proposed for gh-99633 are implemented, the current context will also switch for reasons other than context enter or exit. Since users actually care about context switches and not enter or exit, replace the enter and exit events with a single switched event.

The former exit event was emitted just before exiting the context. The new switched event is emitted after the context is exited to match the semantics users expect of an event with a past-tense name. If users need the ability to clean up before the switch takes effect, another event type can be added in the future. It is not added here because YAGNI.

This commit amends a feature that is new to v3.14 so I did not include a NEWS blurb (the existing blurb should suffice).

cc @fried


📚 Documentation preview 📚: https://cpython-previews--124776.org.readthedocs.build/

@rhansen rhansen force-pushed the gh-119333-context-switched branch 4 times, most recently from 8242dce to 7606b67 Compare October 1, 2024 23:23
@rhansen rhansen changed the title gh-119333, gh-99633: Replace enter/exit events with "switched" gh-124872: Replace enter/exit events with "switched" Oct 1, 2024
@rhansen rhansen force-pushed the gh-119333-context-switched branch 3 times, most recently from e68cb7f to d04d9c0 Compare October 2, 2024 22:04
@rhansen rhansen force-pushed the gh-119333-context-switched branch from d04d9c0 to f3f17b3 Compare October 10, 2024 08:45
Doc/c-api/contextvars.rst Outdated Show resolved Hide resolved
Modules/_testcapi/watchers.c Outdated Show resolved Hide resolved
Modules/_testcapi/watchers.c Outdated Show resolved Hide resolved
Modules/_testcapi/watchers.c Outdated Show resolved Hide resolved
@rhansen rhansen force-pushed the gh-119333-context-switched branch from f3f17b3 to b4cff77 Compare October 12, 2024 21:37
Users want to know when the current context switches to a different
context object.  Right now this happens when and only when a context
is entered or exited, so the enter and exit events are synonymous with
"switched".  However, if the changes proposed for pythongh-99633 are
implemented, the current context will also switch for reasons other
than context enter or exit.  Since users actually care about context
switches and not enter or exit, replace the enter and exit events with
a single switched event.

The former exit event was emitted just before exiting the context.
The new switched event is emitted after the context is exited to match
the semantics users expect of an event with a past-tense name.  If
users need the ability to clean up before the switch takes effect,
another event type can be added in the future.  It is not added here
because YAGNI.

I skipped 0 in the enum as a matter of practice.  Skipping 0 makes it
easier to troubleshoot when code forgets to set zeroed memory, and it
aligns with best practices for other tools (e.g.,
https://protobuf.dev/programming-guides/dos-donts/#unspecified-enum).
@rhansen rhansen force-pushed the gh-119333-context-switched branch from b4cff77 to d898525 Compare October 12, 2024 21:40
@rhansen rhansen marked this pull request as ready for review October 12, 2024 21:42
@ZeroIntensity
Copy link
Member

This is sort of reviewed already, per discussion in the original issue, so I'm approving it.

Copy link
Contributor

@fried fried left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM on behavioral changes. @1st1 as requested

case Py_CONTEXT_EVENT_EXIT:
return "Py_CONTEXT_EVENT_EXIT";
case Py_CONTEXT_SWITCHED:
return "Py_CONTEXT_SWITCHED";
default:
return "?";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, please you use Py_UNREACHABLE() here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ? was @gvanrossum idea in my pr.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, alright, I'm not gonna overturn Guido's review :) I'll push this as is.

@1st1 1st1 merged commit 843d28f into python:main Oct 14, 2024
42 checks passed
@1st1
Copy link
Member

1st1 commented Oct 14, 2024

Thank you Richard!

@rhansen rhansen deleted the gh-119333-context-switched branch October 15, 2024 00:03
Eclips4 added a commit to Eclips4/cpython that referenced this pull request Oct 15, 2024
Eclips4 added a commit to Eclips4/cpython that referenced this pull request Oct 15, 2024
Eclips4 pushed a commit to Eclips4/cpython that referenced this pull request Oct 16, 2024
…4776)

Users want to know when the current context switches to a different
context object.  Right now this happens when and only when a context
is entered or exited, so the enter and exit events are synonymous with
"switched".  However, if the changes proposed for pythongh-99633 are
implemented, the current context will also switch for reasons other
than context enter or exit.  Since users actually care about context
switches and not enter or exit, replace the enter and exit events with
a single switched event.

The former exit event was emitted just before exiting the context.
The new switched event is emitted after the context is exited to match
the semantics users expect of an event with a past-tense name.  If
users need the ability to clean up before the switch takes effect,
another event type can be added in the future.  It is not added here
because YAGNI.

I skipped 0 in the enum as a matter of practice.  Skipping 0 makes it
easier to troubleshoot when code forgets to set zeroed memory, and it
aligns with best practices for other tools (e.g.,
https://protobuf.dev/programming-guides/dos-donts/#unspecified-enum).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants