Crash when toggling alt buffer in cmd shell #20661

Open
opened 2026-01-31 07:20:27 +00:00 by claunia · 0 comments
Owner

Originally created by @j4james on GitHub (Oct 12, 2023).

Originally assigned to: @lhecker on GitHub.

Windows Terminal version

1.19.2682.0

Windows build number

10.0.19045.3448

Other Software

No response

Steps to reproduce

  1. Open a cmd shell or pwsh tab in Windows Terminal.
  2. Run the python script below, pressing Enter a few times.
import sys
while True:
  sys.stdout.write("\033[?1049h")
  sys.stdout.flush()
  sys.stdin.readline()
  sys.stdout.write("\033[?1049l")

Expected Behavior

The script should be able to run forever without any ill effects.

Actual Behavior

After pressing Enter three or four times, the tab crashes and disappears. The terminal itself is still fine.

I can also reproduce this in OpenConsole, so it's not a conpty thing, but you need to set the HKCU\Console\VirtualTerminalLevel registry entry first to enable the VT support.

Also worth noting that it doesn't seem to be triggered when executed from a WSL bash shell. I could only reproduce it with a cmd shell or pwsh.

And when run from within the debugger, I see an exception in one of two places. Most commonly here:

Exception thrown at 0x00007FF7F77A7FF0 in OpenConsole.exe: 0xC0000005: Access violation reading location 0x0000000000000090.
>	OpenConsole.exe!RenderData::GetViewport() Line 25	C++
 	OpenConsole.exe!Microsoft::Console::Render::Renderer::_CheckViewportAndScroll() Line 399	C++
 	OpenConsole.exe!Microsoft::Console::Render::Renderer::_PaintFrameForEngine(Microsoft::Console::Render::IRenderEngine * const pEngine) Line 122	C++
 	OpenConsole.exe!Microsoft::Console::Render::RenderThread::_ThreadProc() Line 217	C++
 	[External Code]	

But sometimes also here:

Exception thrown at 0x00007FFD7B7DC73E (ntdll.dll) in OpenConsole.exe: 0xC0000005: Access violation reading location 0x00007DF522920000.
 	ntdll.dll!LdrpDispatchUserCallTarget()	Unknown
>	OpenConsole.exe!Microsoft::Console::VirtualTerminal::StateMachine::_EnterEscape() Line 778	C++
 	OpenConsole.exe!Microsoft::Console::VirtualTerminal::StateMachine::ProcessCharacter(const wchar_t wch) Line 1864	C++
 	OpenConsole.exe!Microsoft::Console::VirtualTerminal::StateMachine::ProcessString(const std::basic_string_view<wchar_t,std::char_traits<wchar_t>> string) Line 2123	C++
 	OpenConsole.exe!DoWriteConsole(const wchar_t * pwchBuffer, unsigned __int64 * const pcbBuffer, SCREEN_INFORMATION & screenInfo, bool requiresVtQuirk, std::unique_ptr<WriteData,std::default_delete<WriteData>> & waiter) Line 321	C++
 	OpenConsole.exe!WriteConsoleWImplHelper(SCREEN_INFORMATION & context, const std::basic_string_view<wchar_t,std::char_traits<wchar_t>> buffer, unsigned __int64 & read, bool requiresVtQuirk, std::unique_ptr<WriteData,std::default_delete<WriteData>> & waiter) Line 361	C++
 	OpenConsole.exe!ApiRoutines::WriteConsoleWImpl(SCREEN_INFORMATION & context, const std::basic_string_view<wchar_t,std::char_traits<wchar_t>> buffer, unsigned __int64 & read, bool requiresVtQuirk, std::unique_ptr<IWaitRoutine,std::default_delete<IWaitRoutine>> & waiter) Line 590	C++
 	OpenConsole.exe!ApiDispatchers::ServerWriteConsole(_CONSOLE_API_MSG * const m, int * const pbReplyPending) Line 381	C++
 	OpenConsole.exe!IoSorter::ServiceIoOperation(_CONSOLE_API_MSG * const pMsg, _CONSOLE_API_MSG * * ReplyMsg) Line 33	C++
 	OpenConsole.exe!ConsoleIoThread(void * lpParameter) Line 1035	C++
 	[External Code]	

Git bisect suggests this was caused by commit 821ae3af2d (PR #15783).

Originally created by @j4james on GitHub (Oct 12, 2023). Originally assigned to: @lhecker on GitHub. ### Windows Terminal version 1.19.2682.0 ### Windows build number 10.0.19045.3448 ### Other Software _No response_ ### Steps to reproduce 1. Open a cmd shell or pwsh tab in Windows Terminal. 2. Run the python script below, pressing <kbd>Enter</kbd> a few times. ```py import sys while True: sys.stdout.write("\033[?1049h") sys.stdout.flush() sys.stdin.readline() sys.stdout.write("\033[?1049l") ``` ### Expected Behavior The script should be able to run forever without any ill effects. ### Actual Behavior After pressing <kbd>Enter</kbd> three or four times, the tab crashes and disappears. The terminal itself is still fine. I can also reproduce this in OpenConsole, so it's not a conpty thing, but you need to set the `HKCU\Console\VirtualTerminalLevel` registry entry first to enable the VT support. Also worth noting that it doesn't seem to be triggered when executed from a WSL bash shell. I could only reproduce it with a cmd shell or pwsh. And when run from within the debugger, I see an exception in one of two places. Most commonly here: ``` Exception thrown at 0x00007FF7F77A7FF0 in OpenConsole.exe: 0xC0000005: Access violation reading location 0x0000000000000090. > OpenConsole.exe!RenderData::GetViewport() Line 25 C++ OpenConsole.exe!Microsoft::Console::Render::Renderer::_CheckViewportAndScroll() Line 399 C++ OpenConsole.exe!Microsoft::Console::Render::Renderer::_PaintFrameForEngine(Microsoft::Console::Render::IRenderEngine * const pEngine) Line 122 C++ OpenConsole.exe!Microsoft::Console::Render::RenderThread::_ThreadProc() Line 217 C++ [External Code] ``` But sometimes also here: ``` Exception thrown at 0x00007FFD7B7DC73E (ntdll.dll) in OpenConsole.exe: 0xC0000005: Access violation reading location 0x00007DF522920000. ntdll.dll!LdrpDispatchUserCallTarget() Unknown > OpenConsole.exe!Microsoft::Console::VirtualTerminal::StateMachine::_EnterEscape() Line 778 C++ OpenConsole.exe!Microsoft::Console::VirtualTerminal::StateMachine::ProcessCharacter(const wchar_t wch) Line 1864 C++ OpenConsole.exe!Microsoft::Console::VirtualTerminal::StateMachine::ProcessString(const std::basic_string_view<wchar_t,std::char_traits<wchar_t>> string) Line 2123 C++ OpenConsole.exe!DoWriteConsole(const wchar_t * pwchBuffer, unsigned __int64 * const pcbBuffer, SCREEN_INFORMATION & screenInfo, bool requiresVtQuirk, std::unique_ptr<WriteData,std::default_delete<WriteData>> & waiter) Line 321 C++ OpenConsole.exe!WriteConsoleWImplHelper(SCREEN_INFORMATION & context, const std::basic_string_view<wchar_t,std::char_traits<wchar_t>> buffer, unsigned __int64 & read, bool requiresVtQuirk, std::unique_ptr<WriteData,std::default_delete<WriteData>> & waiter) Line 361 C++ OpenConsole.exe!ApiRoutines::WriteConsoleWImpl(SCREEN_INFORMATION & context, const std::basic_string_view<wchar_t,std::char_traits<wchar_t>> buffer, unsigned __int64 & read, bool requiresVtQuirk, std::unique_ptr<IWaitRoutine,std::default_delete<IWaitRoutine>> & waiter) Line 590 C++ OpenConsole.exe!ApiDispatchers::ServerWriteConsole(_CONSOLE_API_MSG * const m, int * const pbReplyPending) Line 381 C++ OpenConsole.exe!IoSorter::ServiceIoOperation(_CONSOLE_API_MSG * const pMsg, _CONSOLE_API_MSG * * ReplyMsg) Line 33 C++ OpenConsole.exe!ConsoleIoThread(void * lpParameter) Line 1035 C++ [External Code] ``` Git bisect suggests this was caused by commit 821ae3af2d311350fbfaa4c77af09858488681e9 (PR #15783).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20661