Fix channel worker stalling when stopping ffmpeg with unread stdout
This commit is contained in:
@@ -74,6 +74,10 @@ class FfmpegStreamProcess:
|
||||
if self._process is None or self._process.returncode is not None:
|
||||
return
|
||||
|
||||
# wait() only returns once every pipe hits EOF, so unread stdout left over
|
||||
# from the consumer breaking out of iter_chunks() must be drained, or it hangs forever.
|
||||
stdout_drain_task = asyncio.create_task(self._drain_stdout())
|
||||
|
||||
self._process.terminate()
|
||||
try:
|
||||
await asyncio.wait_for(self._process.wait(), timeout=FFMPEG_TERMINATE_TIMEOUT_SECONDS)
|
||||
@@ -82,9 +86,16 @@ class FfmpegStreamProcess:
|
||||
self._process.kill()
|
||||
await self._process.wait()
|
||||
|
||||
await stdout_drain_task
|
||||
if self._stderr_task is not None:
|
||||
await self._stderr_task
|
||||
|
||||
async def _drain_stdout(self) -> None:
|
||||
if self._process is None or self._process.stdout is None:
|
||||
return
|
||||
while await self._process.stdout.read(FFMPEG_READ_CHUNK_SIZE):
|
||||
pass
|
||||
|
||||
async def _drain_stderr(self) -> None:
|
||||
if self._process is None or self._process.stderr is None:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user