microsoft / microsoft/terminal

Conhost hangs when a lot of content is output in a single write

Open
#11,794 24 comments 0 reactions 1 assignee Claimed by @lhecker View on GitHub
Area-Performance Issue-Bug Product-Conhost
Dominant language
C++
Stars
105k
Forks
9.6k
Avg merge
3d 17h
Merged PRs (30d)
29

Description

### Windows Terminal version

n/a

### Windows build number

10.0.19041.1348

### Other Software

_No response_

### Steps to reproduce

1. Open a cmd shell in conhost.
2. Run the C++ example further down in the comments: [link](#issuecomment-974874048).

The Python example below does not reproduce the issue reliably on all systems.

1. Open a WSL shell in conhost.
2. Execute the Python script below.

```
import math
import os
import sys

size = os.popen('stty size', 'r').read().split()
h,w = tuple(int(n) for n in size)
mx = w//2
my = h//2

def frame(i):
s = '\033[H'
for y in range(h):
for x in range(w):
dy,dx = y-my,x-mx
a = math.atan2(dy*2,dx) + math.pi
c = (int(a/math.pi*127)+i)%256
s += '\033[38;2;%d;%d;%dm*' % (c,c,c)
return s

sys.stdout.write('Generating content...\n')
s = '\033[?25l'
for i in range(512):
s += frame(i)
s += '\033[?25h\033[m\033[H'

sys.stdout.write('Starting animation...\n')
sys.stdout.write(s)
```
This constructs a little VT animation which it outputs with a single `write` call.

### Expected Behavior

You should see a rotating pattern, a bit like a radar scan. This is what it looks like in MinTTY (using wsltty):

https://user-images.githubusercontent.com/4181424/142741894-fb9637da-6046-4c7b-b4cb-6e0e2efc583f.mp4

You'll see the same sort of thing in XTerm, VTE, Alacritty, Kitty, etc.

### Actual Behavior

Conhost displays the first couple of lines of the first frame (if anything), then hangs for a couple of seconds, and eventually just shows the final frame.

Windows Terminal is a little better, in that it doesn't hang, but you still don't see the animation - just the final frame.

I believe the problem is that the buffer is locked for the entire time that the VT parser is processing a write operation, and the render thread can't render anything when it can't obtain the lock. If the write buffer is particularly large (as is the case here), then the renderer may be blocked for quite a long time.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.