Skip to content

Fix memory usage with the non-stdlib ContextVar#2212

Merged
davidism merged 1 commit into2.0.xfrom
context
Aug 8, 2021
Merged

Fix memory usage with the non-stdlib ContextVar#2212
davidism merged 1 commit into2.0.xfrom
context

Conversation

@pgjones
Copy link
Copy Markdown
Member

@pgjones pgjones commented Aug 8, 2021

The release functionality used before this commit would replace the
value in the storage for a given greenlet ident with an empty
dictionary, which was problematic as it means the storage would grow
with each greenlet used until eventually consisting of many empty
dictionaries. The fix is to restore the Werkzeug 1.0 behaviour of
poping from the storage instead.

Note that stdlib ContextVars (as I understand them) are released when
the relevant task completes, and hence assigning an empty dictionary
releases references to anything stored whilst in the context as
desired (with the dictionary being release when the task completed).

It will be nice when we can drop Python 3.6 and exclusively use the stdlib ContextVars.

Checklist:

  • Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
  • Add or update relevant docs, in the docs folder and in code.
  • Add an entry in CHANGES.rst summarizing the change and linking to the issue.
  • Add .. versionchanged:: entries in any relevant code docs.
  • Run pre-commit hooks and fix any issues.
  • Run pytest and tox, no tests failed.

The release functionality used before this commit would replace the
value in the storage for a given greenlet ident with an empty
dictionary, which was problematic as it means the storage would grow
with each greenlet used until eventually consisting of many empty
dictionaries. The fix is to restore the Werkzeug 1.0 behaviour of
poping from the storage instead.

Note that stdlib ContextVars (as I understand them) are released when
the relevant task completes, and hence assigning an empty dictionary
releases references to anything stored whilst in the context as
desired (with the dictionary being release when the task completed).
@davidism davidism changed the base branch from main to 2.0.x August 8, 2021 14:38
@davidism davidism merged commit 490e49f into 2.0.x Aug 8, 2021
@davidism davidism deleted the context branch August 8, 2021 14:39
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants