-
Notifications
You must be signed in to change notification settings - Fork 8.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conhost hangs when a lot of content is output in a single write #11794
Comments
This may seem like a contrived example, and it is, but it's based on a real issue I had when testing my sixel branch with the sixel-bench benchmark. Much like the above animation, it caused the console to hang for a couple of seconds, and eventually just displayed the final frame of the video. I suppose sixel-bench is really a contrived example too, so not an urgent problem (especially since we don't have sixel yet), but this definitely does feel like a bug - I don't think it should be possible to hang the console like that. |
@j4james this issue could be Windows version specific. You're using Windows 10. On version 10.0.2200 (Windows 11) after setting conhost_rotate_test.mp4WT version 1.11.2921.0. Same result when WT is the default terminal application. This post is for reference only! |
Huh. That's weird. I wouldn't expect the Windows version to have any effect, but I wonder if it has something to do with the version of WSL you're using (I'm on v1). This test relies on Python passing the I'll try and rewrite the test case in C, with a direct call to Thanks for attempting to replicate the issue though. |
Here's the equivalent test case rewritten in C++ with a #include <windows.h>
#include <string>
#include <cmath>
void main()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode;
GetConsoleMode(handle, &mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(handle, mode);
CONSOLE_SCREEN_BUFFER_INFOEX csbie;
csbie.cbSize = sizeof(csbie);
GetConsoleScreenBufferInfoEx(handle, &csbie);
const auto w = csbie.dwSize.X;
const auto h = csbie.srWindow.Bottom - csbie.srWindow.Top + 1;
const auto mx = w/2;
const auto my = h/2;
const auto pi = 3.14159265358979323846;
printf("Generating content...\n");
std::string s = "\033[?25l";
for (auto i = 0; i < 512; i++) {
s += "\033[H";
for (auto y = 0; y < h; y++) {
for (auto x = 0; x < w; x++) {
auto dy = y-my;
auto dx = x-mx;
auto a = std::atan2(dy*2,dx) + pi;
auto c = std::to_string((int(a/pi*127)+i)%256);
s += "\033[38;2;"+c+";"+c+";"+c+"m*";
}
}
}
s += "\033[?25h\033[m\033[H";
printf("Starting animation...\n");
DWORD written = 0;
WriteConsoleA(handle, s.c_str(), s.length(), &written, NULL);
} |
@j4james this is the output with the exe version of rotate in 1st tab, and python/wsl in 2nd tab: rotate_test.mp4Changing |
@j4james when running the python script in pwsh, it works but rendering seems slower compared to wsl test: wt_rotate_pwsh.mp4The same |
I should have mentioned I didn't expect to see the problem on the Windows version of Python, because I know it handles I/O differently to Linux, and the output gets split into chunks. But judging from your results, it looks like the same thing can apply to Linux. At least the C++ version confirms the problem, although your system is clearly a lot faster than mine. so you're not seeing much of a delay before it renders the final frame. |
@j4james what is the difference between
and this is the output: wt_printf_rotate.mp4 |
You know, I think @lhecker actually fixed this recently, but I'd assume the fix isn't in 10.0.19041.1348 |
Yeah I had regular freezes in WT of up to 30 seconds, when running the application at high frame rates. Since it was a heisenbug and only occurred when you weren't observing it (with a debugger) I could never actually proof the reason for the issue. But I believe I could determine it regardless: Locks like SRWLOCK are unfair locks and under certain circumstances it can prevent threads from progressing for a long time. Based on this theory I changed the buffer lock in WT into a |
You need to test with the C++ example - the Python version doesn't seem to work for everyone (I'm guessing because you're all on a later version of WSL to me). I can reproduce this with a recent version of conhost built from source and a recent version of Windows Terminal built from source. |
@elsaco |
Btw, this is what it looks like for me in OpenConsole and Windows Terminal, both built from commit a68b0d4. Rotate2.mp4 |
@j4james if the same wt_wsl_rotate.mp4 |
After moving 98aQbqsgwKpEUvcN.mp4After additionally replacing the static thread_local ULONG recursionCount = 0;
static til::ticket_lock lock; ...and increasing the chunk size to a more reasonable 128kB (since the ticket lock is now being "fair"): aMg2dnhJAHLmWdAj.mp4Unfortunately however, chunking the input is likely not a viable option, as some applications rely on the implicit promise that a single write() with VT is processed synchronously. For instance: GPABiTKwPytBxTNM.mp4 |
That's a good point. So maybe this is just the caller's responsibility then, and the issue could be closed as "won't-fix". My main concern was the failure of sixel-bench, but it looks like I'm the only own that can reproduce the issue in Python (which is what sixel-bench is using), so it's likely that could just be resolved by an update of Windows or WSL. |
@j4james But at least one improvement will result out of this: As you can see in my video above, even 2kB chunks of data can cause the output to lag in conhost (and ConPTY) if an application is writing VT sufficiently fast. By switching to a fair mutex ( |
Just be aware that there might be a few places in conhost where the lock is acquired recursively (I'm fairly certain I've come across some in the past). I think I remember you saying that the I don't think it would be that difficult to refactor the code to avoid recursive locks, but it might be a bit of work trying to track down all the cases that need to be dealt with. |
I don't believe it's all too difficult to make a mutex save for reentrancy. My approach is basically: static thread_local ULONG recursionCount = 0;
static til::ticket_lock lock;
void LockConsole() {
const auto rc = ++recursionCount;
if (rc == 1) {
lock.lock();
}
}
void UnlockConsole()
{
const auto rc = --recursionCount;
if (rc == 0) {
lock.unlock();
}
} It's missing safety-checks for over- and underflow, but otherwise it works fine. |
This commit replaces the console lock was replaced with a fair ticket lock implementation, as only fair locks guarantee us that the renderer (or other parts) can acquire the lock, once the VT parser has finally released it. This is related to #11794. ## Validation Steps Performed * No obvious crashes ✅ * No text/VT performance regression ✅ * Cursor blinker starts/stops on focus/defocus of the window ✅
I just randomly came across this issue while searching for sixels of all things. |
@lhecker This still hangs for me in conhost, which it what the issue was originally about. And while it's improved in Windows Terminal, it still has a significant delay before you see anything (assumedly because conhost has to process the buffer first). This is most obvious when running the sixel-bench test. |
Windows Terminal version
n/a
Windows build number
10.0.19041.1348
Other Software
No response
Steps to reproduce
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.
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):
Rotate.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.
The text was updated successfully, but these errors were encountered: