Created
February 25, 2019 18:49
-
-
Save vxgmichel/1c20ee27c62f4f17fa0ede4c05ca2571 to your computer and use it in GitHub Desktop.
A pytest fixture for detecting leaking file descriptors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pytest | |
import subprocess | |
def get_open_fds(): | |
cmd = f"lsof -p {os.getpid()}" | |
result = subprocess.run(cmd.split(), capture_output=True) | |
return {line.split()[-1] for line in result.stdout.splitlines()} | |
@pytest.fixture | |
def debug_fd(): | |
old_fds = get_open_fds() | |
yield | |
new_fds = get_open_fds() | |
delta = new_fds - old_fds | |
assert not delta |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment