# Buggy code
def close(self):
    log.debug("Closing ffmpeg thread")
    if self.process:
        # kill ffmpeg
        self.process.kill()
        self.process.stdout.close()

        # close the streams
        for stream in self.streams:
            if hasattr(stream, "close"):
                stream.close()

        log.debug("Closed all the substreams")
    if self.close_errorlog:
        self.errorlog.close()
        self.errorlog = None


# Traceback (ImportError)
$ streamlink "https://www.youtube.com/watch?v=VaU6GR8OHNU" -l trace
[16:51:11.763092][cli][debug] OS:         Linux-5.8.0-29-generic-x86_64-with-glibc2.32
[16:51:11.763170][cli][debug] Python:     3.8.6
[16:51:11.763193][cli][debug] Streamlink: 1.7.0+21.g70dc809
[16:51:11.763212][cli][debug] Requests(2.25.0), Socks(1.7.1), Websocket(0.57.0)
[16:51:11.763245][cli][info] Found matching plugin youtube for URL https://www.youtube.com/watch?v=VaU6GR8OHNU
[16:51:11.763275][plugin.youtube][debug] Video ID from URL
[16:51:11.763295][plugin.youtube][debug] Using video ID: VaU6GR8OHNU
[16:51:12.669559][plugin.youtube][debug] get_video_info - 1: Found data
[16:51:12.670741][plugin.youtube][debug] MuxedStream: v 135 a 251 = 480p
[16:51:12.670780][plugin.youtube][debug] MuxedStream: v 133 a 251 = 240p
[16:51:12.670804][plugin.youtube][debug] MuxedStream: v 160 a 251 = 144p
[16:51:12.671317][cli][info] Available streams: audio_mp4a, audio_opus, 144p (worst), 240p, 360p, 480p (best)
[16:51:12.671355][cli][info] Opening stream: 480p (muxed-stream)
[16:51:12.671386][stream.ffmpegmux][debug] Opening http substream
[16:51:12.750200][stream.ffmpegmux][debug] Opening http substream
[16:51:12.813040][stream.ffmpegmux][debug] ffmpeg command: /usr/bin/ffmpeg -nostats -y -i /tmp/ffmpeg-9607-784 -i /tmp/ffmpeg-9607-270 -c:v copy -c:a copy -map 0 -map 1 -f matroska pipe:1
[16:51:12.813226][stream.ffmpegmux][debug] Starting copy to pipe: /tmp/ffmpeg-9607-784
[16:51:12.813344][stream.ffmpegmux][debug] Starting copy to pipe: /tmp/ffmpeg-9607-270
[16:51:12.815867][cli][debug] Pre-buffering 8192 bytes
[16:51:12.875782][cli][info] Starting player: /usr/bin/mpv
[16:51:12.876152][cli.output][debug] Opening subprocess: /usr/bin/mpv "--title=Christmas Music 2020 🎅 Top Christmas Songs Playlist 2020 🎄 Best Christmas Songs Ever" -
[16:51:13.378673][cli][debug] Writing stream to output
[16:51:19.777326][cli][info] Player closed
[16:51:19.777459][stream.ffmpegmux][debug] Closing ffmpeg thread
[16:51:19.779916][stream.ffmpegmux][error] Pipe copy aborted: /tmp/ffmpeg-9607-270
[16:51:19.780000][stream.ffmpegmux][error] Pipe copy aborted: /tmp/ffmpeg-9607-784
[16:51:26.824121][stream.ffmpegmux][debug] Closed all the substreams
[16:51:26.824246][cli][info] Stream ended
[16:51:26.824434][cli][info] Closing currently open stream...
[16:51:26.824477][stream.ffmpegmux][debug] Closing ffmpeg thread
[16:51:26.824539][stream.ffmpegmux][debug] Closed all the substreams
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib/python3.8/logging/__init__.py", line 1081, in emit
msg = self.format(record)
File "/usr/lib/python3.8/logging/__init__.py", line 925, in format
return fmt.format(record)
File "venv3/lib/python3.8/site-packages/streamlink/logger.py", line 54, in format
return super().format(record)
File "/usr/lib/python3.8/logging/__init__.py", line 666, in format
record.asctime = self.formatTime(record, self.datefmt)
File "venv3/lib/python3.8/site-packages/streamlink/logger.py", line 41, in formatTime
return tdt.strftime(datefmt or self.default_time_format)
ImportError: sys.meta_path is None, Python is likely shutting down
Call stack:
File "venv3/lib/python3.8/site-packages/streamlink/stream/ffmpegmux.py", line 158, in close
log.debug("Closing ffmpeg thread")
Message: 'Closing ffmpeg thread'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib/python3.8/logging/__init__.py", line 1081, in emit
msg = self.format(record)
File "/usr/lib/python3.8/logging/__init__.py", line 925, in format
return fmt.format(record)
File "venv3/lib/python3.8/site-packages/streamlink/logger.py", line 54, in format
return super().format(record)
File "/usr/lib/python3.8/logging/__init__.py", line 666, in format
record.asctime = self.formatTime(record, self.datefmt)
File "venv3/lib/python3.8/site-packages/streamlink/logger.py", line 41, in formatTime
return tdt.strftime(datefmt or self.default_time_format)
ImportError: sys.meta_path is None, Python is likely shutting down
Call stack:
File "venv3/lib/python3.8/site-packages/streamlink/stream/ffmpegmux.py", line 169, in close
log.debug("Closed all the substreams")
Message: 'Closed all the substreams'
Arguments: ()

# Fixed code
def close(self):
    if self.closed:
        return

    log.debug("Closing ffmpeg thread")
    if self.process:
        # kill ffmpeg
        self.process.kill()
        self.process.stdout.close()

        # close the streams
        for stream in self.streams:
            if hasattr(stream, "close") and callable(stream.close):
                stream.close()

        log.debug("Closed all the substreams")

    if self.close_errorlog:
        self.errorlog.close()
        self.errorlog = None

    super().close()