Source

pypy / pypy / module / thread / os_thread.py

Diff from to

File pypy/module/thread/os_thread.py

 Thread support based on OS-level threads.
 """
 
+import os
 from pypy.module.thread import ll_thread as thread
 from pypy.module.thread.error import wrap_thread_error
 from pypy.interpreter.error import OperationError, operationerrfmt
         # run!
         try:
             bootstrapper.run(space, w_callable, args)
-        finally:
-            bootstrapper.nbthreads -= 1
-            # clean up space.threadlocals to remove the ExecutionContext
-            # entry corresponding to the current thread
+            # done
+        except Exception, e:
+            # oups! last-level attempt to recover.
             try:
-                space.threadlocals.leave_thread(space)
-            finally:
-                thread.gc_thread_die()
+                STDERR = 2
+                os.write(STDERR, "Thread exited with ")
+                os.write(STDERR, str(e))
+                os.write(STDERR, "\n")
+            except OSError:
+                pass
+        bootstrapper.nbthreads -= 1
+        thread.gc_thread_die()
     bootstrap = staticmethod(bootstrap)
 
     def acquire(space, w_callable, args):
                 where = 'thread %d started by ' % ident
                 e.write_unraisable(space, where, w_callable)
             e.clear(space)
+        # clean up space.threadlocals to remove the ExecutionContext
+        # entry corresponding to the current thread
+        space.threadlocals.leave_thread(space)
     run = staticmethod(run)
 
 bootstrapper = Bootstrapper()