-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-42815: Fix issue when thread doesn't copy context of parent thread #24074
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
Conversation
This PR is stale because it has been open for 30 days with no activity. |
# Conflicts: # Lib/test/test_threading.py # Lib/threading.py
Could you possibly explain why do you need to amend decimal tests?. I don't understand it. |
Oh, I understand now. You want to preserve the behaviour of a thread messing with decimal context to affect other threads (including main thread). I find this patch valuable, but the decimal context "interference" in current python could be something production code could depend on. In that situation, this patch is "risky" and we are possibly too late for 3.10, since 3.10rc1 is planned in a couple of days. Maybe Python 3.11? We also need a documentation update talking about the new "context" optional parameter. I requested a review in python-dev. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Test should check that old context is altered when passing the old context as a parameter, and not altered when using an implicit new context.
- The same when providing an explicit new context.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @1st1, author of the contextvars module.
@@ -925,7 +930,7 @@ def run(self): | |||
""" | |||
try: | |||
if self._target is not None: | |||
self._target(*self._args, **self._kwargs) | |||
self._context.run(self._target, *self._args, **self._kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's one why to fix the issue. Another would be to modify the contextvars module to detect that it runs in a new thread. For exemple, this change doesn't cover the case of threads spawned by C libraries which then use the Python C API to run Python code. Or functions calling _thread.start_new_thread() directly.
I don't know which option is the best. At least, this change is reasonable small.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, maybe I add bad description for this issue. I will try to make things more clear. Every thread has it's own context and they do not share same context. This PR tries fix problem when child thread use empty context instead of copy of a parent context.
if context is not None: | ||
self._context = context | ||
else: | ||
self._context = _copy_context() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is _copy_context() fast/efficient when contextvars is not used at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is information from PEP-567:
The Context mapping is implemented using an immutable dictionary. This allows for a O(1) implementation of the copy_context() function.
I believe it is fast/efficient to use copy_context
.
@@ -0,0 +1,2 @@ | |||
Fix issue when new thread doesn't copy context of a parent thread. Patch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This description is not very helpful. I would prefer to rephrase it to explain that threads no longer share contextvars variables with their parent thread, but the context is now copied.
IMO this change is important enough to be documented in a .. versionchanged:: 3.11
markup in https://docs.python.org/dev/library/threading.html#threading.Thread documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually every thread has it's own context and they do not share same context. This PR tries fix problem when child thread use empty context instead of copy of a parent context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rephrase the NEWS entry and add the requested versionchanged markup.
@jcea Actually every thread has it's own context and they do not share same context. This PR tries fix problem when child thread use empty context instead of copy of a parent context. For instead, I can that this problem fixed at cpython/Lib/asyncio/threads.py Lines 22 to 25 in c7ea1e3
|
@uriyyo , your comment doesn't make sense if a thread modifying its context affects what other threads see. That mean "shared context" in my book :-). The comment by @vstinner about threads launched by C extensions is interesting. I was thinking about detecting this case when getting the GIL and copy the context at that time. The problem would be to decide which context to copy, in the general case. Maybe perfection is enemy of good and this PR would be good enough for now. |
# Conflicts: # Lib/test/test_threading.py
Currently, the I suggest to modify Modules/Setup to uncomment |
Closing this PR as far as a related issue has been closed: #86981 (comment) |
https://bugs.python.org/issue42815