Fix: handle OSError from os.get_terminal_size() in CLI table rendering for non-TTY environments#2599
Merged
peterschmidt85 merged 2 commits intodstackai:masterfrom May 4, 2025
Conversation
peterschmidt85
requested changes
May 4, 2025
|
|
||
| offers = Table(box=None, expand=os.get_terminal_size()[0] <= 110) | ||
| try: | ||
| width = os.get_terminal_size()[0] |
Contributor
There was a problem hiding this comment.
IMO, it's better to use something like this:
import shutil
table = Table(box=None, expand=shutil.get_terminal_size(fallback=(120, 40)).columns <= 110)
…terminal width detection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 psin 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 psin a Python subprocess or PTY (not a real terminal).Why this is needed
Related Issue
Thank you for reviewing!