# Buggy code
def start(self, wait=60, *, server_settings={}, **opts):
    """Start the cluster."""
    status = self.get_status()
    if status == "running":
        return
    elif status == "not-initialized":
        raise ClusterError(
            "cluster in {!r} has not been initialized".format(self._data_dir)
        )

    port = opts.pop("port", None)
    if port == "dynamic":
        port = find_available_port()

    extra_args = ["--{}={}".format(k, v) for k, v in opts.items()]
    extra_args.append("--port={}".format(port))

    sockdir = server_settings.get("unix_socket_directories")
    if sockdir is None:
        sockdir = server_settings.get("unix_socket_directory")
    if sockdir is None:
        sockdir = "/tmp"

    ssl_key = server_settings.get("ssl_key_file")
    if ssl_key:
        # Make sure server certificate key file has correct permissions.
        keyfile = os.path.join(self._data_dir, "srvkey.pem")
        shutil.copy(ssl_key, keyfile)
        os.chmod(keyfile, 0o400)
        server_settings = server_settings.copy()
        server_settings["ssl_key_file"] = keyfile

    if self._pg_version < (9, 3):
        sockdir_opt = "unix_socket_directory"
    else:
        sockdir_opt = "unix_socket_directories"

    server_settings[sockdir_opt] = sockdir

    for k, v in server_settings.items():
        extra_args.extend(["-c", "{}={}".format(k, v)])

    if _system == "Windows":
        # On Windows we have to use pg_ctl as direct execution
        # of postgres daemon under an Administrative account
        # is not permitted and there is no easy way to drop
        # privileges.
        process = subprocess.run(
            [self._pg_ctl, "start", "-D", self._data_dir, "-o", " ".join(extra_args)],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )
        stderr = process.stderr

        if process.returncode != 0:
            raise ClusterError(
                "pg_ctl start exited with status {:d}: {}".format(
                    process.returncode, stderr.decode()
                )
            )
    else:
        if os.getenv("ASYNCPG_DEBUG_SERVER"):
            stdout = sys.stdout
        else:
            stdout = subprocess.DEVNULL

        self._daemon_process = subprocess.Popen(
            [self._postgres, "-D", self._data_dir, *extra_args],
            stdout=stdout,
            stderr=subprocess.STDOUT,
            preexec_fn=ensure_dead_with_parent,
        )

        self._daemon_pid = self._daemon_process.pid

    self._test_connection(timeout=wait)


# 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 start(self, wait=60, *, server_settings={}, **opts):
    """Start the cluster."""
    status = self.get_status()
    if status == "running":
        return
    elif status == "not-initialized":
        raise ClusterError(
            "cluster in {!r} has not been initialized".format(self._data_dir)
        )

    port = opts.pop("port", None)
    if port == "dynamic":
        port = find_available_port()

    extra_args = ["--{}={}".format(k, v) for k, v in opts.items()]
    extra_args.append("--port={}".format(port))

    sockdir = server_settings.get("unix_socket_directories")
    if sockdir is None:
        sockdir = server_settings.get("unix_socket_directory")
    if sockdir is None:
        sockdir = "/tmp"

    ssl_key = server_settings.get("ssl_key_file")
    if ssl_key:
        # Make sure server certificate key file has correct permissions.
        keyfile = os.path.join(self._data_dir, "srvkey.pem")
        shutil.copy(ssl_key, keyfile)
        os.chmod(keyfile, 0o600)
        server_settings = server_settings.copy()
        server_settings["ssl_key_file"] = keyfile

    if self._pg_version < (9, 3):
        sockdir_opt = "unix_socket_directory"
    else:
        sockdir_opt = "unix_socket_directories"

    server_settings[sockdir_opt] = sockdir

    for k, v in server_settings.items():
        extra_args.extend(["-c", "{}={}".format(k, v)])

    if _system == "Windows":
        # On Windows we have to use pg_ctl as direct execution
        # of postgres daemon under an Administrative account
        # is not permitted and there is no easy way to drop
        # privileges.
        if os.getenv("ASYNCPG_DEBUG_SERVER"):
            stdout = sys.stdout
        else:
            stdout = subprocess.DEVNULL

        process = subprocess.run(
            [self._pg_ctl, "start", "-D", self._data_dir, "-o", " ".join(extra_args)],
            stdout=stdout,
            stderr=subprocess.STDOUT,
        )

        if process.returncode != 0:
            if process.stderr:
                stderr = ":\n{}".format(process.stderr.decode())
            else:
                stderr = ""
            raise ClusterError(
                "pg_ctl start exited with status {:d}{}".format(
                    process.returncode, stderr
                )
            )
    else:
        if os.getenv("ASYNCPG_DEBUG_SERVER"):
            stdout = sys.stdout
        else:
            stdout = subprocess.DEVNULL

        self._daemon_process = subprocess.Popen(
            [self._postgres, "-D", self._data_dir, *extra_args],
            stdout=stdout,
            stderr=subprocess.STDOUT,
            preexec_fn=ensure_dead_with_parent,
        )

        self._daemon_pid = self._daemon_process.pid

    self._test_connection(timeout=wait)