| 58 | |
| 59 | class DumpThreads(threading.Thread): |
| 60 | def run(self): |
| 61 | time.sleep(20) |
| 62 | |
| 63 | thread_id_to_name = {} |
| 64 | try: |
| 65 | for t in threading.enumerate(): |
| 66 | thread_id_to_name[t.ident] = "%s (daemon: %s)" % (t.name, t.daemon) |
| 67 | except: |
| 68 | pass |
| 69 | |
| 70 | stack_trace = [ |
| 71 | "===============================================================================", |
| 72 | "pydev pyunit runner: Threads still found running after tests finished", |
| 73 | "================================= Thread Dump =================================", |
| 74 | ] |
| 75 | |
| 76 | for thread_id, stack in sys._current_frames().items(): |
| 77 | stack_trace.append("\n-------------------------------------------------------------------------------") |
| 78 | stack_trace.append(" Thread %s" % thread_id_to_name.get(thread_id, thread_id)) |
| 79 | stack_trace.append("") |
| 80 | |
| 81 | if "self" in stack.f_locals: |
| 82 | sys.stderr.write(str(stack.f_locals["self"]) + "\n") |
| 83 | |
| 84 | for filename, lineno, name, line in traceback.extract_stack(stack): |
| 85 | stack_trace.append(' File "%s", line %d, in %s' % (filename, lineno, name)) |
| 86 | if line: |
| 87 | stack_trace.append(" %s" % (line.strip())) |
| 88 | stack_trace.append("\n=============================== END Thread Dump ===============================") |
| 89 | sys.stderr.write("\n".join(stack_trace)) |
| 90 | |
| 91 | # Force thread run to finish |
| 92 | import os |
| 93 | |
| 94 | os._exit(123) |
| 95 | |
| 96 | dump_current_frames_thread = DumpThreads() |
| 97 | dump_current_frames_thread.daemon = True # Daemon so that this thread doesn't halt it! |