Skip to content

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

@matejsp

Description

@matejsp

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions