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])