]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
dummy_threading when run with -O would raise a KeyError in Thread.__delete()
authorBrett Cannon <bcannon@gmail.com>
Wed, 21 Jul 2004 02:55:54 +0000 (02:55 +0000)
committerBrett Cannon <bcannon@gmail.com>
Wed, 21 Jul 2004 02:55:54 +0000 (02:55 +0000)
when called by atexit thanks to dummy_thread always returning -1 for
dummy_thread.get_ident().  Since exception was not an issue, it is now caught
and tossed.

Closes bug #993394.

Lib/threading.py
Misc/NEWS

index df9748124fd643f77fd696d6b770a7d0d1622d44..2784350d166da31eb2a6348f6531968f787ed5b1 100644 (file)
@@ -492,8 +492,40 @@ class Thread(_Verbose):
         self.__block.release()
 
     def __delete(self):
+        """Remove the current thread from the dict of currently running
+        threads.
+
+        Must take care to not raise an exception if dummy_thread is being used
+        (and thus this module is being used as an instance of dummy_threading).
+        Since dummy_thread.get_ident() always returns -1 since there is only one
+        thread if dummy_thread is being used.  This means that if any Thread
+        instances are created they will overwrite any other threads registered.
+
+        This is an issue with this method, though, since an instance of
+        _MainThread is always created by 'threading'.  This gets overwritten the
+        instant an instance of Thread is created; both threads will have -1 as
+        their value from dummy_thread.get_ident() and thus have the same key in
+        the dict.  This means that when the _MainThread instance created by
+        'threading' tries to clean itself up when atexit calls this method it
+        gets a key error if another Thread instance was created since that
+        removed the only thing with the key of -1.
+
+        This all means that KeyError from trying to delete something from
+        _active if dummy_threading is being used is a red herring.  But since
+        it isn't if dummy_threading is *not* being used then don't hide the
+        exception.  Also don't need to worry about issues from interpreter
+        shutdown and sys not being defined because the call is protected by a
+        blanket try/except block where that could be a problem.
+
+        """
         _active_limbo_lock.acquire()
-        del _active[_get_ident()]
+        if _sys.modules.has_key('dummy_threading'):
+            try:
+                del _active[_get_ident()]
+            except KeyError:
+                pass
+        else:
+            del _active[_get_ident()]
         _active_limbo_lock.release()
 
     def join(self, timeout=None):
index 23ddd63e8675f6c7f0b4e59b9e3257c452455557..033db637534c4a5e54e99b27ceca29ea08045919 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -43,6 +43,11 @@ Extension modules
 Library
 -------
 
+- Bug #993394.  A KeyError was being raised by Thread.__delete() for
+  dummy_threading when called by atexit if an instance of Thread was created in
+  an interpreter running in -O.  The exception was of no importance and thus is
+  now thrown away if raised.
+
 - Bug #930024: posixpath.realpath() now detects loops from symlinks and returns
   the longest path before the loop begins.