• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Expose a race in the _warnings module, which is the C backend for the
3warnings module. The "_warnings" module tries to access attributes of the
4"warnings" module (because of the API it has to support), but doing so
5during interpreter shutdown is problematic. Specifically, the call to
6PyImport_GetModuleDict() in Python/_warnings.c:get_warnings_attr will
7abort() if the modules dict has already been cleaned up.
8
9This crasher is timing-dependent, and more threads (NUM_THREADS) may be
10necessary to expose it reliably on different systems.
11"""
12
13import threading
14import warnings
15
16NUM_THREADS = 10
17
18class WarnOnDel(object):
19    def __del__(self):
20        warnings.warn("oh no something went wrong", UserWarning)
21
22def do_work():
23    while True:
24        w = WarnOnDel()
25
26for i in range(NUM_THREADS):
27    t = threading.Thread(target=do_work)
28    t.setDaemon(1)
29    t.start()
30