Fix: handle OSError from os.get_terminal_size() in CLI table rendering for non-TTY environments #2599
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a bug in the dstack CLI where running commands like
dstack ps
in a non-interactive environment (subprocess, PTY, or script) would crash with: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
The error was caused by unguarded calls to
os.get_terminal_size()
in the CLI’s table rendering code. This patch wraps those calls in a try/except block and uses a default width (120) if the call fails, ensuring the CLI works in both TTY and non-TTY environments.Details
os.get_terminal_size()
are now wrapped in try/except.Steps to Reproduce
dstack ps
in a Python subprocess or PTY (not a real terminal).Why this is needed
Related Issue
Thank you for reviewing!