Skip to content

[Bug]: dstack CLI crashes with OSError when run in subprocess or non-TTY environment #2598

@vuyelwadr

Description

@vuyelwadr

Steps to reproduce

run this script

import subprocess
import os

cmd = ["dstack", "ps"]
env = os.environ.copy()
env["DSTACK_NO_TTY"] = "1"
env["NO_COLOR"] = "1"

proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env, universal_newlines=True)
for line in proc.stdout:
    print(repr(line))
proc.wait()

Actual behaviour

The CLI crashes with the following error:
Traceback (most recent call last):
File "/Users/x/.local/bin/dstack", line 10, in
sys.exit(main())
File "/Users/x/.local/share/uv/tools/dstack/lib/python3.10/site-packages/dstack/_internal/cli/main.py", line 87, in main
args.func(args)
File "/Users/x/.local/share/uv/tools/dstack/lib/python3.10/site-packages/dstack/_internal/cli/commands/ps.py", line 44, in _command
console.print(run_utils.get_runs_table(runs, verbose=args.verbose))
File "/Users/x/.local/share/uv/tools/dstack/lib/python3.10/site-packages/dstack/_internal/cli/utils/run.py", line 152, in get_runs_table
table = Table(box=None, expand=os.get_terminal_size()[0] <= 110)
OSError: [Errno 25] Inappropriate ioctl for device

Expected behaviour

The CLI should not crash.
If [os.get_terminal_size()] fails, it should use a default width for table rendering, so that dstack can be used in scripts, automation, and CI environments.

dstack version

0.19.7

Server logs

Additional information

This bug prevents any automation or monitoring of dstack runs from Python or other scripting languages.
The fix is to wrap all calls to [os.get_terminal_size()] in a try/except block and use a default width (e.g., 120) if the call fails.
See also: related PR with fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions