diff --git a/tools/idf_py_actions/tools.py b/tools/idf_py_actions/tools.py index 7bcc8a2f13..317784c447 100644 --- a/tools/idf_py_actions/tools.py +++ b/tools/idf_py_actions/tools.py @@ -10,7 +10,15 @@ import sys from asyncio.subprocess import Process from pkgutil import iter_modules from types import FunctionType -from typing import Any, Dict, Generator, List, Match, Optional, TextIO, Tuple, Union +from typing import Any +from typing import Dict +from typing import Generator +from typing import List +from typing import Match +from typing import Optional +from typing import TextIO +from typing import Tuple +from typing import Union import click import yaml @@ -344,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