Skip to content

Commit

Permalink
Close file descriptors when engine terminates
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Feb 15, 2015
1 parent 21bab13 commit dcdf5c0
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions chess/uci.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ def _execute(self, engine):
assert self.engine == engine
engine._send("quit\n")
engine.terminated.wait()
engine._close_fds()
self._notify_callback()

@property
Expand Down Expand Up @@ -538,6 +539,7 @@ def _stdin_thread_target(self):
command._execute(self)
self.queue.task_done()

self._close_fds()
self._terminated()

def _stdout_thread_target(self):
Expand All @@ -549,8 +551,13 @@ def _stdout_thread_target(self):
line = line.rstrip()
self._received(line)

self._close_fds()
self._terminated()

def _close_fds(self):
self.process.stdin.close()
self.process.stdout.close()

def _terminated(self):
self.returncode = self.process.returncode
self.terminated.set()
Expand Down Expand Up @@ -972,6 +979,7 @@ def terminate(self, async=None):
:return: The return code of the engine process.
"""
self._close_fds()
self.process.terminate()
promise = QuitCommand(self)
if async:
Expand All @@ -987,6 +995,7 @@ def kill(self, async=False):
:return: The return code of the engine process.
"""
self._close_fds()
self.process.kill()
promise = QuitCommand(self)
if async:
Expand Down Expand Up @@ -1023,6 +1032,10 @@ def __init__(self, shell, command):
def _send(self, buf):
self.process.stdin_write(buf.encode("utf-8"))

def _close_fds(self):
# TODO: Check if this is required.
pass

def _terminated(self):
self.returncode = self.process.wait_for_result().return_code
self.terminated.set()
Expand Down

0 comments on commit dcdf5c0

Please sign in to comment.