# Buggy code
def line_received(self, engine, line):
    if line.startswith("move "):
        self._move(engine, line.split(" ", 1)[1])
    elif line == self.final_pong:
        if not self.result.done():
            self.result.set_exception(
                EngineError("xboard engine answered final pong before sending move")
            )
        self.end(engine)
    elif line == "offer draw":
        self.draw_offered = True
    elif line == "resign":
        self.result.set_result(
            PlayResult(
                None, None, self.info, draw_offered=self.draw_offered, resigned=True
            )
        )
        self.end(engine)
    elif line.startswith("1-0") or line.startswith("0-1") or line.startswith("1/2-1/2"):
        if not self.result.done():
            self.result.set_result(
                PlayResult(None, None, self.info, draw_offered=self.draw_offered)
            )
        self.end(engine)
    elif line.startswith("#") or line.startswith("Hint:"):
        pass
    elif len(line.split()) >= 4 and line.lstrip()[0].isdigit():
        self._post(engine, line)
    else:
        LOGGER.warning("%s: Unexpected engine output: %s", engine, line)


# Traceback (asyncio.futures.InvalidStateError)
Exception in callback EngineProtocol.pipe_data_received(1, b'you play bo...Drawn game}\n')
handle: <Handle EngineProtocol.pipe_data_received(1, b'you play bo...Drawn game}\n')>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/events.py", line 126, in _run
self._callback(*self._args)
File "/home/pascal/.local/lib/python3.5/site-packages/chess/engine.py", line 602, in pipe_data_received
self._line_received(line)
File "/home/pascal/.local/lib/python3.5/site-packages/chess/engine.py", line 615, in _line_received
self.command._line_received(self, line)
File "/home/pascal/.local/lib/python3.5/site-packages/chess/engine.py", line 869, in _line_received
self.line_received(engine, line)
File "/home/pascal/.local/lib/python3.5/site-packages/chess/engine.py", line 1716, in line_received
self._move(engine, line.split(" ", 1)[1])
File "/home/pascal/.local/lib/python3.5/site-packages/chess/engine.py", line 1748, in _move
self.result.set_result(PlayResult(move, None, self.info, self.draw_offered))
File "/usr/lib/python3.5/asyncio/futures.py", line 348, in set_result
raise InvalidStateError('{}: {!r}'.format(self._state, self))
asyncio.futures.InvalidStateError: FINISHED: <Future finished result=<PlayResult a...ffered=False)>>
Traceback (most recent call last):
File "./tournament.py", line 96, in <module>
board.push(result.move)
File "/home/pascal/.local/lib/python3.5/site-packages/chess/__init__.py", line 1942, in push
move = self._to_chess960(move)
File "/home/pascal/.local/lib/python3.5/site-packages/chess/__init__.py", line 3318, in _to_chess960
if move.from_square == E1 and self.kings &amp; BB_E1:
AttributeError: 'NoneType' object has no attribute 'from_square'

# Fixed code
def line_received(self, engine, line):
    if line.startswith("move "):
        self._move(engine, line.split(" ", 1)[1])
    elif line.startswith("Hint: "):
        self._hint(engine, line.split(" ", 1)[1])
    elif line == self.pong_after_move:
        if not self.result.done():
            self.result.set_result(self.play_result)
        if not ponder:
            self.set_finished()
    elif line == self.pong_after_ponder:
        if not self.result.done():
            self.result.set_result(self.play_result)
        self.set_finished()
    elif line == "offer draw":
        if not self.result.done():
            self.play_result.draw_offered = True
        self._ping_after_move(engine)
    elif line == "resign":
        if not self.result.done():
            self.play_result.resigned = True
        self._ping_after_move(engine)
    elif line.startswith("1-0") or line.startswith("0-1") or line.startswith("1/2-1/2"):
        self._ping_after_move(engine)
    elif line.startswith("#"):
        pass
    elif len(line.split()) >= 4 and line.lstrip()[0].isdigit():
        self._post(engine, line)
    else:
        LOGGER.warning("%s: Unexpected engine output: %s", engine, line)