# Buggy code
async def release(self, connection):
    """Release a database connection back to the pool."""

    async def _release_impl(ch: PoolConnectionHolder):
        try:
            await ch.release()
        finally:
            self._queue.put_nowait(ch)

    self._check_init()

    if (
        type(connection) is not PoolConnectionProxy
        or connection._holder._pool is not self
    ):
        raise exceptions.InterfaceError(
            "Pool.release() received invalid connection: "
            "{connection!r} is not a member of this pool".format(connection=connection)
        )

    if connection._con is None:
        # Already released, do nothing.
        return

    connection._detach()

    # Use asyncio.shield() to guarantee that task cancellation
    # does not prevent the connection from being returned to the
    # pool properly.
    return await asyncio.shield(_release_impl(connection._holder), loop=self._loop)


# Traceback (AttributeError)
[2017-11-01 00:09:25,800] (ERROR) asyncio: Task was destroyed but it is pending!
task: <Task pending coro=<Pool.release.<locals>._release_impl() running at /usr/local/lib/python3.6/site-packages/asyncpg/pool.py:465> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f830f109d68>()]> cb=[shield.<locals>._done_callback() at /usr/local/lib/python3.6/asyncio/tasks.py:672]>
[2017-11-01 00:09:25,804] (ERROR) asyncio: Task was destroyed but it is pending!
task: <Task pending coro=<Pool.release.<locals>._release_impl() running at /usr/local/lib/python3.6/site-packages/asyncpg/pool.py:465> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f830f0891f8>()]> cb=[shield.<locals>._done_callback() at /usr/local/lib/python3.6/asyncio/tasks.py:672]>
[2017-11-01 00:09:25,808] (ERROR) asyncio: Fatal write error on socket transport
protocol: <asyncpg.protocol.protocol.Protocol object at 0x7f830f6bb588>
transport: <_SelectorSocketTransport fd=9>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/asyncpg/pool.py", line 192, in release
await self._con.reset()
File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 986, in reset
await self.execute(reset_query)
File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 238, in execute
return await self._protocol.query(query, timeout)
File "asyncpg/protocol/protocol.pyx", line 296, in query
AttributeError: 'weakref' object has no attribute 'cline_in_traceback'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 762, in write
n = self._sock.send(data)
OSError: [Errno 9] Bad file descriptor
Exception ignored in: <coroutine object Pool.release.<locals>._release_impl at 0x7f830f197678>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/asyncpg/pool.py", line 465, in _release_impl
File "/usr/local/lib/python3.6/site-packages/asyncpg/pool.py", line 203, in release
File "/usr/local/lib/python3.6/site-packages/asyncpg/pool.py", line 192, in release
File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 986, in reset
File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 238, in execute
File "asyncpg/protocol/protocol.pyx", line 296, in query
AttributeError: 'weakref' object has no attribute 'cline_in_traceback'
[2017-11-01 00:09:25,813] (ERROR) asyncio: Fatal write error on socket transport
protocol: <asyncpg.protocol.protocol.Protocol object at 0x7f830f6bb6d8>
transport: <_SelectorSocketTransport fd=10>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/asyncpg/pool.py", line 192, in release
await self._con.reset()
File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 986, in reset
await self.execute(reset_query)
File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 238, in execute
return await self._protocol.query(query, timeout)
File "asyncpg/protocol/protocol.pyx", line 296, in query
AttributeError: 'weakref' object has no attribute 'cline_in_traceback'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 762, in write
n = self._sock.send(data)
OSError: [Errno 9] Bad file descriptor
Exception ignored in: <coroutine object Pool.release.<locals>._release_impl at 0x7f830f197990>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/asyncpg/pool.py", line 465, in _release_impl
File "/usr/local/lib/python3.6/site-packages/asyncpg/pool.py", line 203, in release
File "/usr/local/lib/python3.6/site-packages/asyncpg/pool.py", line 192, in release
File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 986, in reset
File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 238, in execute
File "asyncpg/protocol/protocol.pyx", line 296, in query
AttributeError: 'weakref' object has no attribute 'cline_in_traceback'
[2017-11-01 00:09:25,817] (ERROR) asyncio: Task was destroyed but it is pending!
task: <Task pending coro=<DefaultModule.run() running at ./src/models/module.py:52> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f830f1093a8>()]>>
[2017-11-01 00:09:25,821] (ERROR) asyncio: Task was destroyed but it is pending!
task: <Task pending coro=<DefaultModule.run() running at ./src/models/module.py:52> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f830f089198>()]>>
[2017-11-01 00:09:25,825] (ERROR) DatabaseController: Traceback (most recent call last):
File "./src/controllers/database.py", line 102, in fetch
_logger.info("Conflict with recovery, retrying")
GeneratorExit

# Fixed code
async def release(self, connection, *, timeout=None):
    """Release a database connection back to the pool.

    :param Connection connection:
        A :class:`~asyncpg.connection.Connection` object to release.
    :param float timeout:
        A timeout for releasing the connection.  If not specified, defaults
        to the timeout provided in the corresponding call to the
        :meth:`Pool.acquire() <asyncpg.pool.Pool.acquire>` method.

    .. versionchanged:: 0.14.0
        Added the *timeout* parameter.
    """

    async def _release_impl(ch: PoolConnectionHolder, timeout: float):
        try:
            await ch.release(timeout)
        finally:
            self._queue.put_nowait(ch)

    self._check_init()

    if (
        type(connection) is not PoolConnectionProxy
        or connection._holder._pool is not self
    ):
        raise exceptions.InterfaceError(
            "Pool.release() received invalid connection: "
            "{connection!r} is not a member of this pool".format(connection=connection)
        )

    if connection._con is None:
        # Already released, do nothing.
        return

    connection._detach()

    if timeout is None:
        timeout = connection._holder._timeout

    # Use asyncio.shield() to guarantee that task cancellation
    # does not prevent the connection from being returned to the
    # pool properly.
    return await asyncio.shield(
        _release_impl(connection._holder, timeout), loop=self._loop
    )