-
Notifications
You must be signed in to change notification settings - Fork 515
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
(5) Add reset()
to thread local ContextVar and no-op copy_context()
#2570
(5) Add reset()
to thread local ContextVar and no-op copy_context()
#2570
Conversation
try: | ||
from aiocontextvars import ContextVar | ||
from aiocontextvars import ContextVar, copy_context | ||
|
||
return True, ContextVar | ||
return True, ContextVar, copy_context | ||
except ImportError: | ||
pass | ||
else: | ||
# On Python 3.7 contextvars are functional. | ||
try: | ||
from contextvars import ContextVar | ||
from contextvars import ContextVar, copy_context | ||
|
||
return True, ContextVar | ||
return True, ContextVar, copy_context | ||
except ImportError: | ||
pass | ||
|
||
# Fall back to basic thread-local usage. | ||
|
||
from threading import local | ||
|
||
return False, _make_threadlocal_contextvars(local) | ||
return False, _make_threadlocal_contextvars(local), _make_noop_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.
Looks good to me
Following this MR 👍🏼 looking forward to the updates |
reset()
to thread local ContextVar and no-op copy_context()
reset()
to thread local ContextVar and no-op 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.
I don't yet know how you will use copy_context
but this change is fine self-contained
sentry_sdk/utils.py
Outdated
def reset(self, token): | ||
# type: (Any) -> None | ||
self._local.value = getattr(self._original_local, token) | ||
setattr(self._original_local, token, None) |
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.
isn't it better to delete here so that it gets GCd?
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.
Hi @sl0thentr0py, please can you go into the detail about this tradeoff?
Thank you.
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.
@50-Course If it is not garbage collected it stays in memory. With a lot sets and resets the memory usage could grow without memory being freed.
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.
Oh okay got it!
Improves the capabilities of our threadlocal context variables, we use when no "real" context variables are available (for example in old Python versions)
This is preparation work for refactoring how we deal with Hubs and Scopes in the future.