Skip to content

Commit d9f3aae

Browse files
sebgodclaude
andcommitted
Handle IOException in VirtualTerminal.Size with retry
Console.WindowWidth can throw IOException when the console handle is temporarily invalid. Retry once after a brief sleep. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent da78b46 commit d9f3aae

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/Console.Lib/VirtualTerminal.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,23 @@ public void EnterAlternateScreen()
112112

113113
public (int Column, int Row) Offset => (0, 0);
114114

115-
public (int Width, int Height) Size => (System.Console.WindowWidth, System.Console.WindowHeight);
115+
public (int Width, int Height) Size
116+
{
117+
get
118+
{
119+
try
120+
{
121+
return (System.Console.WindowWidth, System.Console.WindowHeight);
122+
}
123+
catch (System.IO.IOException)
124+
{
125+
// Console handle can become temporarily invalid during alternate screen transitions;
126+
// retry once after a brief yield
127+
Thread.Sleep(10);
128+
return (System.Console.WindowWidth, System.Console.WindowHeight);
129+
}
130+
}
131+
}
116132

117133
public void Clear() => System.Console.Clear();
118134

0 commit comments

Comments
 (0)