RIS reset doesn't clear the display and scrollback correctly #3223

Closed
opened 2026-01-30 23:16:12 +00:00 by claunia · 6 comments
Owner

Originally created by @j4james on GitHub (Aug 6, 2019).

Originally assigned to: @j4james on GitHub.

Environment

Windows build number: Version 10.0.18362.175

Steps to reproduce

  1. Open a conhost WSL shell.
  2. Execute printf "\ec" (the RIS escape sequence).
  3. Note that the display isn't cleared.
  4. Do a few directory listings or something to fill the scrollback buffer.
  5. Execute printf "\e[41m\ec" (setting the background to red, followed by RIS).
  6. Note that the display has been cleared with a red background, and the scrollback is only partially cleared.

Expected behavior

The RIS escape sequence should clear the display with the default background color, and should erase everything in the scrollback buffer.

Actual behavior

The RIS escape sequence doesn't work at all when there isn't anything in the scrollback. And when it does work, it clears the display with the wrong color, and doesn't completely erase the scrollback buffer.

More details

The initial RIS doesn't work because of the way the Viewport::Subtract method is implemented. When the removeMe viewport completely overlaps the original viewport, it returns a single empty viewport, but it really ought to return nothing. An empty viewport throws an exception when passed to the SCREEN_INFORMATION::WriteRect method, which is what happens when called from the ScrollRegion function here. This in turn causes the AdaptDispatch::_EraseScrollback method to fail, which then aborts the AdaptDispatch::HardReset method.

The reason the color is wrong, and the scrollback isn't fully erased, is because of the order of the steps in the AdaptDispatch::HardReset method. You need to reset the SGR state before clearing the display, otherwise it'll use the current background color for the fill color. And you need to clear the display before clearing the scrollback, otherwise the last page of content will be retained in the scrollback.

I'd be happy to provide a PR with these fixes if nobody disagrees with my assessment.

Originally created by @j4james on GitHub (Aug 6, 2019). Originally assigned to: @j4james on GitHub. # Environment ```none Windows build number: Version 10.0.18362.175 ``` # Steps to reproduce 1. Open a conhost WSL shell. 2. Execute `printf "\ec"` (the [RIS](https://vt100.net/docs/vt510-rm/RIS.html) escape sequence). 3. Note that the display isn't cleared. 4. Do a few directory listings or something to fill the scrollback buffer. 5. Execute `printf "\e[41m\ec"` (setting the background to red, followed by RIS). 6. Note that the display has been cleared with a red background, and the scrollback is only partially cleared. # Expected behavior The RIS escape sequence should clear the display with the default background color, and should erase everything in the scrollback buffer. # Actual behavior The RIS escape sequence doesn't work at all when there isn't anything in the scrollback. And when it does work, it clears the display with the wrong color, and doesn't completely erase the scrollback buffer. # More details The initial RIS doesn't work because of the way the `Viewport::Subtract` method is implemented. When the _removeMe_ viewport completely overlaps the _original_ viewport, it [returns a single empty viewport](https://github.com/microsoft/terminal/blob/9b92986b49bed8cc41fde4d6ef080921c41e6d9e/src/types/viewport.cpp#L995-L998), but it really ought to return nothing. An empty viewport [throws an exception](https://github.com/microsoft/terminal/blob/7abcc35fdf7d98bc98016cda8f4b90398c65692e/src/host/screenInfo.cpp#L2608-L2609) when passed to the `SCREEN_INFORMATION::WriteRect` method, which is what happens when called from the `ScrollRegion` function [here](https://github.com/microsoft/terminal/blob/9b92986b49bed8cc41fde4d6ef080921c41e6d9e/src/host/output.cpp#L469-L476). This in turn causes the `AdaptDispatch::_EraseScrollback` method to fail, which then aborts the `AdaptDispatch::HardReset` method. The reason the color is wrong, and the scrollback isn't fully erased, is because of the [order of the steps](https://github.com/microsoft/terminal/blob/594a7e4501d1108267388197ec187ca77f82756b/src/terminal/adapter/adaptDispatch.cpp#L1604-L1615) in the `AdaptDispatch::HardReset` method. You need to reset the SGR state before clearing the display, otherwise it'll use the current background color for the fill color. And you need to clear the display before clearing the scrollback, otherwise the last page of content will be retained in the scrollback. I'd be happy to provide a PR with these fixes if nobody disagrees with my assessment.
claunia added the Product-ConhostResolution-Fix-AvailableIssue-BugArea-VT labels 2026-01-30 23:16:12 +00:00
Author
Owner

@DHowett-MSFT commented on GitHub (Aug 8, 2019):

I've tagged this up for you, but we can wait for @zadjii-msft if you don't want to start on it just yet. 😄

@DHowett-MSFT commented on GitHub (Aug 8, 2019): I've tagged this up for you, but we can wait for @zadjii-msft if you don't want to start on it just yet. :smile:
Author
Owner

@zadjii-msft commented on GitHub (Aug 8, 2019):

Yea this seems like an accurate assessment of the situation. Great find :)

@zadjii-msft commented on GitHub (Aug 8, 2019): Yea this seems like an accurate assessment of the situation. Great find :)
Author
Owner

@ghost commented on GitHub (Aug 29, 2019):

I tried the CI build but printf "\ec" still doesn't clear all scrollback; reset and Ctrl-L doesn't work either. It seems that only the viewport is cleared when Ctrl-L is pressed.

@ghost commented on GitHub (Aug 29, 2019): I tried the CI build but `printf "\ec"` still doesn't clear all scrollback; `reset` and Ctrl-L doesn't work either. It seems that only the viewport is cleared when Ctrl-L is pressed.
Author
Owner

@j4james commented on GitHub (Sep 10, 2019):

@visualer Note that this is a fix for conhost - Window Terminal may have other problems of its own.

@j4james commented on GitHub (Sep 10, 2019): @visualer Note that this is a fix for conhost - Window Terminal may have other problems of its own.
Author
Owner

@ghost commented on GitHub (Sep 24, 2019):

:tada:This issue was addressed in #2367, which has now been successfully released as Windows Terminal Preview v0.5.2661.0.🎉

Handy links:

@ghost commented on GitHub (Sep 24, 2019): :tada:This issue was addressed in #2367, which has now been successfully released as `Windows Terminal Preview v0.5.2661.0`.:tada: Handy links: * [Release Notes](https://github.com/microsoft/terminal/releases/tag/v0.5.2661.0) * [Store Download](https://www.microsoft.com/store/apps/9n0dx20hk701?cid=storebadge&ocid=badge)
Author
Owner

@DHowett-MSFT commented on GitHub (Sep 25, 2019):

This also went out with Windows Insider Fast 18990! 😄

@DHowett-MSFT commented on GitHub (Sep 25, 2019): This also went out with Windows Insider Fast 18990! :smile:
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#3223