Skip to content

Commit 28bbbda

Browse files
bpo-30237: Output error when ReadConsole is canceled by CancelSynchronousIo. (GH-7911)
Co-authored-by: ValeriyaSinevich <[email protected]>
1 parent 94972d5 commit 28bbbda

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Output error when ReadConsole is canceled by CancelSynchronousIo instead of
2+
crashing.

Modules/_io/winconsoleio.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,18 @@ read_console_w(HANDLE handle, DWORD maxlen, DWORD *readlen) {
573573
Py_BEGIN_ALLOW_THREADS
574574
DWORD off = 0;
575575
while (off < maxlen) {
576-
DWORD n, len = min(maxlen - off, BUFSIZ);
576+
DWORD n = (DWORD)-1;
577+
DWORD len = min(maxlen - off, BUFSIZ);
577578
SetLastError(0);
578579
BOOL res = ReadConsoleW(handle, &buf[off], len, &n, NULL);
579580

580581
if (!res) {
581582
err = GetLastError();
582583
break;
583584
}
585+
if (n == (DWORD)-1 && (err = GetLastError()) == ERROR_OPERATION_ABORTED) {
586+
break;
587+
}
584588
if (n == 0) {
585589
err = GetLastError();
586590
if (err != ERROR_OPERATION_ABORTED)

Parser/myreadline.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ _PyOS_WindowsConsoleReadline(HANDLE hStdIn)
114114
char *buf = NULL;
115115
int err = 0;
116116

117-
n_read = 0;
117+
n_read = (DWORD)-1;
118118
total_read = 0;
119119
wbuf = wbuf_local;
120120
wbuflen = sizeof(wbuf_local) / sizeof(wbuf_local[0]) - 1;
@@ -126,6 +126,9 @@ _PyOS_WindowsConsoleReadline(HANDLE hStdIn)
126126
err = GetLastError();
127127
goto exit;
128128
}
129+
if (n_read == (DWORD)-1 && (err = GetLastError()) == ERROR_OPERATION_ABORTED) {
130+
break;
131+
}
129132
if (n_read == 0) {
130133
int s;
131134
err = GetLastError();

0 commit comments

Comments
 (0)