From 5360f38b14ea89144604b81a7b51c68c5f81cc05 Mon Sep 17 00:00:00 2001 From: xham3 <215794250+xham3@users.noreply.github.com> Date: Tue, 10 Jun 2025 23:12:17 -0700 Subject: [PATCH] Fix process communicate() deadlock when epg match data overfill subprocess.PIPE buffer. --- apps/channels/tasks.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/apps/channels/tasks.py b/apps/channels/tasks.py index 6217a4ca..a551028f 100755 --- a/apps/channels/tasks.py +++ b/apps/channels/tasks.py @@ -1,6 +1,7 @@ # apps/channels/tasks.py import logging import os +import select import re import requests import time @@ -136,18 +137,35 @@ def match_epg_channels(): text=True ) - # Log stderr in real-time - for line in iter(process.stderr.readline, ''): - if line: - logger.info(line.strip()) + stdout = '' + block_size = 1024 - process.stderr.close() - stdout, stderr = process.communicate() + while True: + # Monitor stdout and stderr for readability + readable, _, _ = select.select([process.stdout, process.stderr], [], [], 1) # timeout of 1 second + if not readable: # timeout expired + if process.poll() is not None: # check if process finished + break + else: # process still running, continue + continue + + for stream in readable: + if stream == process.stdout: + stdout += stream.read(block_size) + elif stream == process.stderr: + error = stream.readline() + if error: + logger.info(error.strip()) + + if process.poll() is not None: + break + + process.wait() os.remove(temp_file_path) if process.returncode != 0: - return f"Failed to process EPG matching: {stderr}" + return f"Failed to process EPG matching" result = json.loads(stdout) # This returns lists of dicts, not model objects