issue #4096: import log_fd_calls() helper.

issue260
David Wilson 6 years ago
parent 8a0b343760
commit 9b3cb55a8b

@ -6,7 +6,9 @@ import re
import socket import socket
import subprocess import subprocess
import sys import sys
import threading
import time import time
import traceback
import psutil import psutil
import unittest2 import unittest2
@ -175,6 +177,46 @@ def sync_with_broker(broker, timeout=10.0):
sem.get(timeout=10.0) sem.get(timeout=10.0)
def log_fd_calls():
mypid = os.getpid()
l = threading.Lock()
real_pipe = os.pipe
def pipe():
with l:
rv = real_pipe()
if mypid == os.getpid():
print
print rv
traceback.print_stack(limit=3)
print
return rv
real_dup2 = os.dup2
def dup2(*args):
with l:
real_dup2(*args)
if mypid == os.getpid():
print
print '--', args
traceback.print_stack(limit=3)
print
real_dup = os.dup
def dup(*args):
with l:
rc = real_dup(*args)
if mypid == os.getpid():
print
print '--', args, '->', rv
traceback.print_stack(limit=3)
print
return rv
os.pipe = pipe
os.dup = dup
os.dup2 = dup2
class CaptureStreamHandler(logging.StreamHandler): class CaptureStreamHandler(logging.StreamHandler):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
logging.StreamHandler.__init__(self, *args, **kwargs) logging.StreamHandler.__init__(self, *args, **kwargs)
@ -232,8 +274,9 @@ class TestCase(unittest2.TestCase):
def tearDownClass(cls): def tearDownClass(cls):
super(TestCase, cls).tearDownClass() super(TestCase, cls).tearDownClass()
mitogen.fork.on_fork() mitogen.fork.on_fork()
assert get_fd_count() == cls._fd_count_before, \ if get_fd_count() != cls._fd_count_before:
"%s leaked FDs. Count before: %s, after: %s" % ( import os; os.system('lsof -p %s' % (os.getpid(),))
assert 0, "%s leaked FDs. Count before: %s, after: %s" % (
cls, cls._fd_count_before, get_fd_count(), cls, cls._fd_count_before, get_fd_count(),
) )

Loading…
Cancel
Save