Merge branch 'fix/exit_asyncio_gracefully' into 'master'

fix: exit gracefully when process started via asyncio is terminated

Closes IDFGH-12390

See merge request espressif/esp-idf!30036
pull/13651/head
Roland Dobai 2024-04-12 14:12:18 +08:00
commit 34f1076711
1 zmienionych plików z 13 dodań i 3 usunięć

Wyświetl plik

@ -352,9 +352,19 @@ class RunTool:
stderr_output_file = os.path.join(self.build_dir, log_dir_name, f'idf_py_stderr_output_{p.pid}')
stdout_output_file = os.path.join(self.build_dir, log_dir_name, f'idf_py_stdout_output_{p.pid}')
if p.stderr and p.stdout: # it only to avoid None type in p.std
await asyncio.gather(
self.read_and_write_stream(p.stderr, stderr_output_file, sys.stderr),
self.read_and_write_stream(p.stdout, stdout_output_file, sys.stdout))
try:
await asyncio.gather(
self.read_and_write_stream(p.stderr, stderr_output_file, sys.stderr),
self.read_and_write_stream(p.stdout, stdout_output_file, sys.stdout))
except asyncio.CancelledError:
# The process we are trying to read from was terminated. Print the
# message here and let the asyncio to finish, because
# Runner context in asyncio.run is closing the event loop and
# if exception is raised(unhandled here) the transport is not closed before
# the even loop is closed and we get RuntimeError: Event loop is closed
# in the transport __del__ function because it's trying to use the closed
# even loop.
red_print(f'\n{self.tool_name} process terminated\n')
await p.wait() # added for avoiding None returncode
return p, stderr_output_file, stdout_output_file