pytest-grpc icon indicating copy to clipboard operation
pytest-grpc copied to clipboard

Resource not closed warning in pytest (and possible lockups of tests)

Open matejsp opened this issue 3 years ago • 0 comments

Problematic code:

@pytest.fixture(scope='module')
def grpc_addr():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 0))
    return 'localhost:{}'.format(sock.getsockname()[1])

Correct code (use with and yield):

@pytest.fixture(scope='module')
def grpc_addr():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind(('localhost', 0))
        yield 'localhost:{}'.format(sock.getsockname()[1])

matejsp avatar Nov 03 '22 12:11 matejsp