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

warnings.catch_warnings is not thread-safe #50896

Closed
ggenellina mannequin opened this issue Aug 5, 2009 · 4 comments
Closed

warnings.catch_warnings is not thread-safe #50896

ggenellina mannequin opened this issue Aug 5, 2009 · 4 comments
Assignees
Labels
docs Documentation in the Doc dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ggenellina
Copy link
Mannequin

ggenellina mannequin commented Aug 5, 2009

BPO 6647
Nosy @akuchling, @birkenfeld, @pitrou
Files
  • error-warnings.py: demo code showing the problem
  • warnings.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/birkenfeld'
    closed_at = <Date 2010-04-02.17:54:34.865>
    created_at = <Date 2009-08-05.04:52:59.012>
    labels = ['type-bug', 'library', 'docs']
    title = 'warnings.catch_warnings is not thread-safe'
    updated_at = <Date 2010-04-02.17:54:34.862>
    user = 'https://bugs.python.org/ggenellina'

    bugs.python.org fields:

    activity = <Date 2010-04-02.17:54:34.862>
    actor = 'akuchling'
    assignee = 'georg.brandl'
    closed = True
    closed_date = <Date 2010-04-02.17:54:34.865>
    closer = 'akuchling'
    components = ['Documentation', 'Library (Lib)']
    creation = <Date 2009-08-05.04:52:59.012>
    creator = 'ggenellina'
    dependencies = []
    files = ['14654', '15341']
    hgrepos = []
    issue_num = 6647
    keywords = ['patch']
    message_count = 4.0
    messages = ['91301', '91318', '95285', '102185']
    nosy_count = 4.0
    nosy_names = ['akuchling', 'georg.brandl', 'ggenellina', 'pitrou']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue6647'
    versions = ['Python 2.6', 'Python 3.1', 'Python 2.7', 'Python 3.2']

    @ggenellina
    Copy link
    Mannequin Author

    ggenellina mannequin commented Aug 5, 2009

    warnings.catch_warnings is a context manager supposed to save and
    restore warnings filters, and optionally record all warnings issued.

    But it does so in a completely thread-unsafe way, by replacing the
    module's "showwarning" and "filters" attributes on enter, and restoring
    them on exit. If the __enter__ / __exit__ calls of two threads overlap,
    after leaving the last block the warnings state is not the same as the
    original state, as it should be.

    I don't know how to fix this, other than using locks (that could block
    indefinitely) or severely restricting how catch_warnings may be used.
    At least, this issue should be documented.

    @ggenellina ggenellina mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 5, 2009
    @pitrou
    Copy link
    Member

    pitrou commented Aug 5, 2009

    Any function whose purpose is to mutate global (non thread-local) state
    is inherently "thread-unsafe". The only reasonable possibility is to
    document it as such.

    @pitrou pitrou added the docs Documentation in the Doc dir label Aug 5, 2009
    @ggenellina
    Copy link
    Mannequin Author

    ggenellina mannequin commented Nov 15, 2009

    Document that warnings.catch_warnings is not thread safe.

    @akuchling
    Copy link
    Member

    Added a warning to 2.x trunk in r79607, and Gabriel's doc change in r79608. This thread-unsafety seems specialized and rarely of great importance, so I used the note:: directive, not warning::.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants