Skip to content

contextmanager doesn't close connection on exit #125

@ErwinJunge

Description

@ErwinJunge

When using Connection as a contextmanager, it doesn't close the connection when the context manager exits (https://github.com/chtd/psycopg2cffi/blob/master/psycopg2cffi/_impl/connection.py#L191). It only happens when it gets garbage collected (https://github.com/chtd/psycopg2cffi/blob/master/psycopg2cffi/_impl/connection.py#L184).

This lead to a bunch of dangling connections for me when running a test suite in pypy where I initialized some stuff in a fixture at the start of each test. Apparently pypy doesn't garbage collect quite as aggresively and I managed to run out of available connections in postgres.

In my opinion it would make a lot of sense to end __exit__ with self._close(), since you're not supposed to use the object after the contextmanager exits anyways.

I'd be happy to submit a PR if you agree.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions