From ca808161ccaba25a25b006f10772a1521ffe935b Mon Sep 17 00:00:00 2001 From: Roderik Ploszek Date: Sat, 24 Feb 2018 22:40:47 +0100 Subject: [PATCH] Fix solver communication deadlocks Calling wait() on process object that communicates through pipes causes deadlocks when the pipes fill up. To fix this, communicate() method was used to send data to the solver. Returned bytes objects were converted to BytesIO objects to keep compatibility with existing processing. --- pysollib/hint.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/pysollib/hint.py b/pysollib/hint.py index ae0bef80..eaaa3a40 100644 --- a/pysollib/hint.py +++ b/pysollib/hint.py @@ -28,6 +28,7 @@ import time import subprocess import re import sys +from io import BytesIO # PySol imports from pysollib.settings import DEBUG, FCS_COMMAND @@ -801,18 +802,14 @@ class Base_Solver_Hint: if os.name != 'nt': kw['close_fds'] = True p = subprocess.Popen(command, **kw) - pin, pout, perr = p.stdin, p.stdout, p.stderr bytes_board = board if sys.version_info > (3,): bytes_board = bytes(board, 'utf-8') - pin.write(bytes_board) - pin.close() - if os.name == 'posix': - p.wait() + pout, perr = p.communicate(bytes_board) if p.returncode in (127, 1): # Linux and Windows return codes for "command not found" error raise RuntimeError('Solver exited with {}'.format(p.returncode)) - return pout, perr + return BytesIO(pout), BytesIO(perr) class FreeCellSolver_Hint(Base_Solver_Hint): @@ -1091,8 +1088,6 @@ class FreeCellSolver_Hint(Base_Solver_Hint): pout.close() perr.close() - if os.name == 'posix': - os.wait() class BlackHoleSolver_Hint(Base_Solver_Hint):