python-chess stockfish analysis died unexpectedly

94 Views Asked by At

I'm trying to get position evaluation using Stockfish on python-chess. From a total of 1 million FEN position, Stockfish will return the score. However, after the 25430th position (oddly specific), the program died.

import chess
import chess.engine

def stockfish_evaluation(board):
  result = engine.analyse(board, chess.engine.Limit(depth=1))
  return result['score'].relative.score(mate_score=10000)

engine = chess.engine.SimpleEngine.popen_uci("/usr/bin/stockfish")
inputf = "lichess-1M.json"
outputf = open("out-1M.data","w")
line_num = sum(1 for _ in open(inputf))
line_count = 0
file = open(inputf)

while True:
  data = ''
  line_buf = 0

  # buffers the json file by 25000 line per block
  # note that one line corresponds to one position, so we are evaluating 25000 position per block (memory constraint)
  for line in file:
    line_count += 1
    line_buf += 1
    print('\r'+f"Processing - {line_count}/{line_num} ({format(line_count/line_num*100,'.2f')}%)  ",end='')
    data += line + '\n'
    if line_buf >= 25000: break

  # then manually extract the FEN position
  n = 0
  t = 0
  s = ''
  isFen = False
  while n < len(data):
    if isFen:
      s += data[n]
      if data[n] == '"':
        isFen = False
        # calls the evaluation function and write it to output file
        outputf.write(f"{s}: {stockfish_evaluation(chess.Board(s[1:-1]))}\n")
    elif data[n:n+6] == '"fen":':
      isFen = True
      n += 6
      s = '"'
    n += 1

Error produced running the above code

<UciProtocol (pid=1014814)>: stderr >> *** stack smashing detected ***: terminated
Traceback (most recent call last):
  File "/home/user/Downloads/chess/test.py", line 33, in <module>
    outputf.write(f"{s}: {stockfish_evaluation(chess.Board(s[1:-1]))}\n")
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/Downloads/chess/test.py", line 5, in stockfish_evaluation
    result = engine.analyse(board, chess.engine.Limit(depth=1))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/chess/engine.py", line 2996, in analyse
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/usr/lib/python3.11/asyncio/tasks.py", line 452, in wait_for
    return await fut
           ^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/chess/engine.py", line 1242, in analyse
    await analysis.wait()
  File "/home/user/.local/lib/python3.11/site-packages/chess/engine.py", line 2771, in wait
    return await self._finished
           ^^^^^^^^^^^^^^^^^^^^
chess.engine.EngineTerminatedError: engine process died unexpectedly (exit code: -6)

checks the output file

$ cat out-1M.data | wc -l
25340

The reason why I dont use the JSON library is because the file is pretty big, so I just manually iterate over the file. I do not suspect the error to come from my part though. As you can see on the error produced the error came from the engine (note that I have tried different engine and it still fails on the 25430th position)

0

There are 0 best solutions below