Skip to content

Commit

Permalink
Handle case where output buffer is closed during shutdown (#365)
Browse files Browse the repository at this point in the history
* Handle case where output buffer is closed during shutdown

  - Prevent crash during launch shutdown when a process IO event happens
    after the buffers have been closed
  - Use unbuffered output in that case so IO still has a chance of being seen

Signed-off-by: Pete Baughman <[email protected]>

* Address MR feedback

Signed-off-by: Pete Baughman <[email protected]>
  • Loading branch information
Peter Baughman authored and ivanpauno committed Jan 17, 2020
1 parent 2f32b9f commit 7f3da8c
Showing 1 changed file with 46 additions and 30 deletions.
76 changes: 46 additions & 30 deletions launch/launch/actions/execute_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,40 +426,56 @@ def __on_process_stdin(
def __on_process_stdout(
self, event: ProcessIO
) -> Optional[SomeActionsType]:
self.__stdout_buffer.write(event.text.decode(errors='replace'))
self.__stdout_buffer.seek(0)
last_line = None
for line in self.__stdout_buffer:
if line.endswith(os.linesep):
self.__stdout_logger.info(
self.__output_format.format(line=line[:-len(os.linesep)], this=self)
)
else:
last_line = line
break
self.__stdout_buffer.seek(0)
self.__stdout_buffer.truncate(0)
if last_line is not None:
self.__stdout_buffer.write(last_line)
to_write = event.text.decode(errors='replace')
if self.__stdout_buffer.closed:
# __stdout_buffer was probably closed by __flush_buffers on shutdown. Output without
# buffering.
self.__stdout_logger.info(
self.__output_format.format(line=to_write, this=self)
)
else:
self.__stdout_buffer.write(to_write)
self.__stdout_buffer.seek(0)
last_line = None
for line in self.__stdout_buffer:
if line.endswith(os.linesep):
self.__stdout_logger.info(
self.__output_format.format(line=line[:-len(os.linesep)], this=self)
)
else:
last_line = line
break
self.__stdout_buffer.seek(0)
self.__stdout_buffer.truncate(0)
if last_line is not None:
self.__stdout_buffer.write(last_line)

def __on_process_stderr(
self, event: ProcessIO
) -> Optional[SomeActionsType]:
self.__stderr_buffer.write(event.text.decode(errors='replace'))
self.__stderr_buffer.seek(0)
last_line = None
for line in self.__stderr_buffer:
if line.endswith(os.linesep):
self.__stderr_logger.info(
self.__output_format.format(line=line[:-len(os.linesep)], this=self)
)
else:
last_line = line
break
self.__stderr_buffer.seek(0)
self.__stderr_buffer.truncate(0)
if last_line is not None:
self.__stderr_buffer.write(last_line)
to_write = event.text.decode(errors='replace')
if self.__stderr_buffer.closed:
# __stderr buffer was probably closed by __flush_buffers on shutdown. Output without
# buffering.
self.__stderr_logger.info(
self.__output_format.format(line=to_write, this=self)
)
else:
self.__stderr_buffer.write(to_write)
self.__stderr_buffer.seek(0)
last_line = None
for line in self.__stderr_buffer:
if line.endswith(os.linesep):
self.__stderr_logger.info(
self.__output_format.format(line=line[:-len(os.linesep)], this=self)
)
else:
last_line = line
break
self.__stderr_buffer.seek(0)
self.__stderr_buffer.truncate(0)
if last_line is not None:
self.__stderr_buffer.write(last_line)

def __flush_buffers(self, event, context):
with self.__stdout_buffer as buf:
Expand Down

0 comments on commit 7f3da8c

Please sign in to comment.