Skip to content

Improve ContextVar in closures documentation #131837

@lysnikolaou

Description

@lysnikolaou

Documentation

In the [documentation for contextvars.ContextVar], there is the following warning:

Important: Context Variables should be created at the top module level and never in closures. Context objects hold strong references to context variables which prevents context variables from being properly garbage collected.

@kumaraditya303 and I talked about it and he claimed that this is not the case any more (that was at least my understanding) and ContextVars can be defined as class/instance attributes for example. If that's the case, we should probably update the docs. Otherwise, it might make sense to emphasize on it even more, now that ContextVars will be even more widely used due to free-threading.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions