Skip to content

Instantly share code, notes, and snippets.

@vxgmichel
Created February 25, 2019 18:49
Show Gist options
  • Save vxgmichel/1c20ee27c62f4f17fa0ede4c05ca2571 to your computer and use it in GitHub Desktop.
Save vxgmichel/1c20ee27c62f4f17fa0ede4c05ca2571 to your computer and use it in GitHub Desktop.
A pytest fixture for detecting leaking file descriptors
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