Message 91301 - Python tracker (original) (raw)

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.