scroll-out in secondary console buffer pollutes main scrollback buffer #22229

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

Originally created by @avih on GitHub (Sep 7, 2024).

Windows Terminal version

1.22.240823002-preview

Windows build number

10.0.19045.4780

Other Software

No response

Steps to reproduce

  • Ensure there's some content at the scrollback of the (main) terminal buffer.
  • Run an application which creates a new buffer using CreateConsoleScreenBuffer and then activates it using SetConsoleActiveScreenBuffer.
  • The application writes some lines to the new buffer until it starts scrolling (more lines than the window height can keep visible).
  • The application switches back to the main buffer and exits.

Test program which: prints 100 lines to the main buffer, then creates and activates a new screen buffer, then writes 40 lines to the new buffer, then activates the original buffer and exits:

scrollbuf.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>


#define writecon(h, s) WriteConsole(h, (s), strlen(s), 0, 0)
#define ERR_EXIT(...) (fprintf(stderr, __VA_ARGS__), exit(1), 0)

void write_n_lines(HANDLE h, const char* prefix, int n)
{
	char buf[64];

	for (int i = 1; i <= n; ++i) {
		writecon(h, prefix);
		sprintf(buf, "%d\n", i);
		writecon(h, buf);
	}
}

int main(int argc, char **argv)
{
	int mode = argc > 1 ? atoi(argv[1]) : 0;

	HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hcon == INVALID_HANDLE_VALUE)
		ERR_EXIT("can't get stdout handle\n");

	if (!writecon(hcon, "orig screen buffer\n"))
		ERR_EXIT("can't write to stdout console\n");

	write_n_lines(hcon, "origbuf ", 100);


	HANDLE hnewbuf = CreateConsoleScreenBuffer(
		GENERIC_WRITE, FILE_SHARE_WRITE,
		0, CONSOLE_TEXTMODE_BUFFER, 0);

	if (hnewbuf == INVALID_HANDLE_VALUE)
		ERR_EXIT("can't create new screen buf\n");

	if (!SetConsoleActiveScreenBuffer(hnewbuf))
		ERR_EXIT("can't activete new screen buf\n");

	if (!writecon(hnewbuf, "new screen buffer\n"))
		ERR_EXIT("can't write to new screen buf\n");

	write_n_lines(hnewbuf, "newbuf ", 40);

	Sleep(1000);

	if (!SetConsoleActiveScreenBuffer(hcon))
		ERR_EXIT("can't restore orig screen buf\n");
	CloseHandle(hnewbuf);

	return 0;
}

Expected Behavior

The original scrollback buffer is restored without traces of content which was printed to the new/secondary console buffer.

Actual Behavior

Lines which were printed to the new (secondary) buffer buf were scrolled out of view become part of the scrollback of the main buffer.

With the test program above (which prints 40 lines to the secondary buffer), if the terminal height is less than 40, for instance 30 lines, then the first 10 lines printed to the secondary buffer will be added to the scrollback of the main buffer.

After the program exits, one needs to scroll up (e.g. using the mouse wheel) to ovserve the lines from the secondary buffer.

Additional notes:

  • This does not seem to be a recent regression. I reproduced it also with Windows Terminal 1.15 (the oldest version I have locally), 1.22-preview, and git master artifacts build from a day or two ago (with #17817 already fixed).
  • There is no issue in OpenConsole.exe and also no issue in the native win10 conhost (i.e. the main scrollback buffer is unaffected by overflow at the secondary buffer).

EDIT:
The issue manifests when using less.exe for windows in Windows terminal, e.g. less some-file.c and then scrolling down using the down-arrow, then after less exits then the scrollback buffer has content which was scrolled out-of-view in less (but no issue in OpenConsole or conhost).

Originally created by @avih on GitHub (Sep 7, 2024). ### Windows Terminal version 1.22.240823002-preview ### Windows build number 10.0.19045.4780 ### Other Software _No response_ ### Steps to reproduce - Ensure there's some content at the scrollback of the (main) terminal buffer. - Run an application which creates a new buffer using `CreateConsoleScreenBuffer` and then activates it using `SetConsoleActiveScreenBuffer`. - The application writes some lines to the new buffer until it starts scrolling (more lines than the window height can keep visible). - The application switches back to the main buffer and exits. Test program which: prints 100 lines to the main buffer, then creates and activates a new screen buffer, then writes 40 lines to the new buffer, then activates the original buffer and exits: <details> <summary>scrollbuf.c</summary> ```c #include <stdio.h> #include <string.h> #include <stdlib.h> #include <windows.h> #define writecon(h, s) WriteConsole(h, (s), strlen(s), 0, 0) #define ERR_EXIT(...) (fprintf(stderr, __VA_ARGS__), exit(1), 0) void write_n_lines(HANDLE h, const char* prefix, int n) { char buf[64]; for (int i = 1; i <= n; ++i) { writecon(h, prefix); sprintf(buf, "%d\n", i); writecon(h, buf); } } int main(int argc, char **argv) { int mode = argc > 1 ? atoi(argv[1]) : 0; HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE); if (hcon == INVALID_HANDLE_VALUE) ERR_EXIT("can't get stdout handle\n"); if (!writecon(hcon, "orig screen buffer\n")) ERR_EXIT("can't write to stdout console\n"); write_n_lines(hcon, "origbuf ", 100); HANDLE hnewbuf = CreateConsoleScreenBuffer( GENERIC_WRITE, FILE_SHARE_WRITE, 0, CONSOLE_TEXTMODE_BUFFER, 0); if (hnewbuf == INVALID_HANDLE_VALUE) ERR_EXIT("can't create new screen buf\n"); if (!SetConsoleActiveScreenBuffer(hnewbuf)) ERR_EXIT("can't activete new screen buf\n"); if (!writecon(hnewbuf, "new screen buffer\n")) ERR_EXIT("can't write to new screen buf\n"); write_n_lines(hnewbuf, "newbuf ", 40); Sleep(1000); if (!SetConsoleActiveScreenBuffer(hcon)) ERR_EXIT("can't restore orig screen buf\n"); CloseHandle(hnewbuf); return 0; } ``` </details> ### Expected Behavior The original scrollback buffer is restored without traces of content which was printed to the new/secondary console buffer. ### Actual Behavior Lines which were printed to the new (secondary) buffer buf were scrolled out of view become part of the scrollback of the main buffer. With the test program above (which prints 40 lines to the secondary buffer), if the terminal height is less than 40, for instance 30 lines, then the first 10 lines printed to the secondary buffer will be added to the scrollback of the main buffer. After the program exits, one needs to scroll up (e.g. using the mouse wheel) to ovserve the lines from the secondary buffer. Additional notes: - This does not seem to be a recent regression. I reproduced it also with Windows Terminal 1.15 (the oldest version I have locally), 1.22-preview, and git master artifacts build from a day or two ago (with #17817 already fixed). - There is no issue in `OpenConsole.exe` and also no issue in the native win10 conhost (i.e. the main scrollback buffer is unaffected by overflow at the secondary buffer). --------- EDIT: The issue manifests when using `less.exe` for windows in Windows terminal, e.g. `less some-file.c` and then scrolling down using the down-arrow, then after `less` exits then the scrollback buffer has content which was scrolled out-of-view in less (but no issue in OpenConsole or conhost).
claunia added the Needs-TriageIssue-Bug labels 2026-01-31 08:07:13 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#22229