Skip to content

OSError: [Errno 22] Invalid argument on Windows when flushing stderr #2458

Description

@Jorisslagter

Description

ComfyUI crashes on Windows (Desktop app) with OSError: [Errno 22] Invalid argument when the tqdm progress bar tries to flush stderr during sampling.

Environment

  • OS: Windows 11
  • ComfyUI Version: 0.6.0
  • ComfyUI-Manager: 4.0.4
  • Python: 3.12.11
  • PyTorch: 2.8.0+cu129
  • GPU: NVIDIA GeForce RTX 5080
  • Using ComfyUI Desktop app

Error Stack Trace

File "comfyui_manager/prestartup_script.py", line 346, in flush
    original_stderr.flush()
File "C:\Users\...\ComfyUI\app\logger.py", line 35, in flush
    super().flush()
OSError: [Errno 22] Invalid argument

Full traceback

The error occurs in the sampling loop when tqdm tries to display progress:

File "comfy/k_diffusion/sampling.py", line 188, in sample_euler
    for i in trange(len(sigmas) - 1, disable=disable):
File "tqdm/std.py", line 448, in status_printer
    getattr(sys.stderr, 'flush', lambda: None)()
File "comfyui_manager/prestartup_script.py", line 346, in flush
    original_stderr.flush()
OSError: [Errno 22] Invalid argument

Cause

The flush() method in prestartup_script.py line 342-346 doesn't handle the case where original_stderr is a special Windows handle (from ComfyUI Desktop app) that doesn't support the flush operation.

Suggested Fix

Add try-except around the flush call in prestartup_script.py:

with std_log_lock:
    try:
        if self.is_stdout:
            original_stdout.flush()
        else:
            original_stderr.flush()
    except OSError:
        pass  # Ignore flush errors on Windows

Workaround

Users can manually apply this fix to prestartup_script.py to prevent the crash.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions