# Buggy code
def __init__(
    self,
    protocol,
    transport,
    loop,
    addr: (str, int) or str,
    config: connect_utils._ClientConfiguration,
    params: connect_utils._ConnectionParameters,
):
    self._protocol = protocol
    self._transport = transport
    self._loop = loop
    self._top_xact = None
    self._aborted = False
    # Incremented very time the connection is released back to a pool.
    # Used to catch invalid references to connection-related resources
    # post-release (e.g. explicit prepared statements).
    self._pool_release_ctr = 0

    self._addr = addr
    self._config = config
    self._params = params

    self._stmt_cache = _StatementCache(
        loop=loop,
        max_size=config.statement_cache_size,
        on_remove=self._maybe_gc_stmt,
        max_lifetime=config.max_cached_statement_lifetime,
    )

    self._stmts_to_close = set()

    self._listeners = {}
    self._log_listeners = set()

    settings = self._protocol.get_settings()
    ver_string = settings.server_version
    self._server_version = serverversion.split_server_version_string(ver_string)

    self._server_caps = _detect_server_capabilities(self._server_version, settings)

    self._intro_query = introspection.INTRO_LOOKUP_TYPES

    self._reset_query = None
    self._proxy = None

    # Used to serialize operations that might involve anonymous
    # statements.  Specifically, we want to make the following
    # operation atomic:
    #    ("prepare an anonymous statement", "use the statement")
    #
    # Used for `con.fetchval()`, `con.fetch()`, `con.fetchrow()`,
    # `con.execute()`, and `con.executemany()`.
    self._stmt_exclusive_section = _Atomic()


# 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
def __init__(
    self,
    protocol,
    transport,
    loop,
    addr: (str, int) or str,
    config: connect_utils._ClientConfiguration,
    params: connect_utils._ConnectionParameters,
):
    self._protocol = protocol
    self._transport = transport
    self._loop = loop
    self._top_xact = None
    self._aborted = False
    # Incremented very time the connection is released back to a pool.
    # Used to catch invalid references to connection-related resources
    # post-release (e.g. explicit prepared statements).
    self._pool_release_ctr = 0

    self._addr = addr
    self._config = config
    self._params = params

    self._stmt_cache = _StatementCache(
        loop=loop,
        max_size=config.statement_cache_size,
        on_remove=self._maybe_gc_stmt,
        max_lifetime=config.max_cached_statement_lifetime,
    )

    self._stmts_to_close = set()

    self._listeners = {}
    self._log_listeners = set()
    self._cancellations = set()

    settings = self._protocol.get_settings()
    ver_string = settings.server_version
    self._server_version = serverversion.split_server_version_string(ver_string)

    self._server_caps = _detect_server_capabilities(self._server_version, settings)

    self._intro_query = introspection.INTRO_LOOKUP_TYPES

    self._reset_query = None
    self._proxy = None

    # Used to serialize operations that might involve anonymous
    # statements.  Specifically, we want to make the following
    # operation atomic:
    #    ("prepare an anonymous statement", "use the statement")
    #
    # Used for `con.fetchval()`, `con.fetch()`, `con.fetchrow()`,
    # `con.execute()`, and `con.executemany()`.
    self._stmt_exclusive_section = _Atomic()