From fd76ff60acb481393fdb84c38007c827ce850f41 Mon Sep 17 00:00:00 2001 From: teknium1 Date: Tue, 24 Feb 2026 04:13:32 -0800 Subject: [PATCH] fix: improve stdout/stderr handling in delegate_task function - Saved and restored stdout/stderr to prevent redirection issues in child threads, ensuring consistent output during task delegation. - Enhanced reliability of output handling in concurrent execution scenarios. --- tools/delegate_tool.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/delegate_tool.py b/tools/delegate_tool.py index 5a6cb0d50..111beb33a 100644 --- a/tools/delegate_tool.py +++ b/tools/delegate_tool.py @@ -21,6 +21,7 @@ import io import json import logging import os +import sys import time from concurrent.futures import ThreadPoolExecutor, as_completed from typing import Any, Dict, List, Optional @@ -253,6 +254,11 @@ def delegate_task( completed_count = 0 spinner_ref = getattr(parent_agent, '_delegate_spinner', None) + # Save stdout/stderr before the executor — redirect_stdout in child + # threads races on sys.stdout and can leave it as devnull permanently. + _saved_stdout = sys.stdout + _saved_stderr = sys.stderr + with ThreadPoolExecutor(max_workers=MAX_CONCURRENT_CHILDREN) as executor: futures = {} for i, t in enumerate(task_list): @@ -300,6 +306,10 @@ def delegate_task( except Exception: pass + # Restore stdout/stderr in case redirect_stdout race left them as devnull + sys.stdout = _saved_stdout + sys.stderr = _saved_stderr + # Sort by task_index so results match input order results.sort(key=lambda r: r["task_index"])