Fix OSError (BrokenPipeError) on stdout flush #6142
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes repeated errors being written to stderr whenever a new log message fails to be written to stdout due to the stream pipe being closed by the other process, e.g. when executing
streamlink -l debug | head -n1
. This issue is caused when trying to flush the stdout stream buffer after writing a log message. Python'slogging
module explicitly doesn't handle this case (for some reason).The fix is to simply ignore
OSError
(BrokenPipeError
) inlogging.StreamHandler.flush()
. On Windows,BrokenPipeError
is not raised and instead a simpleOSError
witherrno=EINVAL
.As an optimization, follow-up log calls could all be ignored by overriding
emit()
when this exception is caught for the first time, but it's not really important.Alternatively, the
StreamHandler
'shandleError()
method could be overridden, which is where the"--- Logging error ---"
stuff is coming from, but this would remove potentially useful error messages when logging fails in a different way.The second commit fixes the broken stdout pipe raising
OSError
(BrokenPipeError
) when the Python process exits, as Python always flushes stdout on exit.The fix is to destruct the
sys.stdout
object, as opposed to redirecting stdout to devnull, as mentioned in theSIGPIPE
notes in the stdlib docs.There's no interference with
--stdout
, as there's only a simple flush at the end before callingsys.exit(...)
.Only tested on my local machine. Will check later today and then merge if there are no issues.
master
PR