Skip to content

Make Pool.close() wait until all checked out connections are released #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions asyncpg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ def is_closed(self):
:return bool: ``True`` if the connection is closed, ``False``
otherwise.
"""
return not self._protocol.is_connected() or self._aborted
return self._aborted or not self._protocol.is_connected()

async def close(self, *, timeout=None):
"""Close the connection gracefully.
Expand All @@ -995,30 +995,21 @@ async def close(self, *, timeout=None):
.. versionchanged:: 0.14.0
Added the *timeout* parameter.
"""
if self.is_closed():
return
self._mark_stmts_as_closed()
self._listeners.clear()
self._log_listeners.clear()
self._aborted = True
try:
await self._protocol.close(timeout)
if not self.is_closed():
await self._protocol.close(timeout)
except Exception:
# If we fail to close gracefully, abort the connection.
self._aborted = True
self._protocol.abort()
self._abort()
raise
finally:
self._clean_tasks()
self._cleanup()

def terminate(self):
"""Terminate the connection without waiting for pending data."""
self._mark_stmts_as_closed()
self._listeners.clear()
self._log_listeners.clear()
self._aborted = True
self._protocol.abort()
self._clean_tasks()
if not self.is_closed():
self._abort()
self._cleanup()

async def reset(self, *, timeout=None):
self._check_open()
Expand All @@ -1041,6 +1032,26 @@ async def reset(self, *, timeout=None):
if reset_query:
await self.execute(reset_query, timeout=timeout)

def _abort(self):
# Put the connection into the aborted state.
self._aborted = True
self._protocol.abort()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also add self._protocol = None to catch any use of connection after _abort().

self._protocol = None

def _cleanup(self):
# Free the resources associated with this connection.
# This must be called when a connection is terminated.

if self._proxy is not None:
# Connection is a member of a pool, so let the pool
# know that this connection is dead.
self._proxy._holder._release_on_close()

self._mark_stmts_as_closed()
self._listeners.clear()
self._log_listeners.clear()
self._clean_tasks()

def _clean_tasks(self):
# Wrap-up any remaining tasks associated with this connection.
if self._cancellations:
Expand Down
Loading