Skip to content

Commit c3af73d

Browse files
miss-islingtonValeriyaSinevich
authored andcommitted
bpo-30237: Output error when ReadConsole is canceled by CancelSynchronousIo. (GH-7911)
(cherry picked from commit ce75df3) Co-authored-by: ValeriyaSinevich <[email protected]>
1 parent 1e98d87 commit c3af73d

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
@@ -556,14 +556,18 @@ read_console_w(HANDLE handle, DWORD maxlen, DWORD *readlen) {
556556
Py_BEGIN_ALLOW_THREADS
557557
DWORD off = 0;
558558
while (off < maxlen) {
559-
DWORD n, len = min(maxlen - off, BUFSIZ);
559+
DWORD n = (DWORD)-1;
560+
DWORD len = min(maxlen - off, BUFSIZ);
560561
SetLastError(0);
561562
BOOL res = ReadConsoleW(handle, &buf[off], len, &n, NULL);
562563

563564
if (!res) {
564565
err = GetLastError();
565566
break;
566567
}
568+
if (n == (DWORD)-1 && (err = GetLastError()) == ERROR_OPERATION_ABORTED) {
569+
break;
570+
}
567571
if (n == 0) {
568572
err = GetLastError();
569573
if (err != ERROR_OPERATION_ABORTED)

Parser/myreadline.c

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

112-
n_read = 0;
112+
n_read = (DWORD)-1;
113113
total_read = 0;
114114
wbuf = wbuf_local;
115115
wbuflen = sizeof(wbuf_local) / sizeof(wbuf_local[0]) - 1;
@@ -121,6 +121,9 @@ _PyOS_WindowsConsoleReadline(HANDLE hStdIn)
121121
err = GetLastError();
122122
goto exit;
123123
}
124+
if (n_read == (DWORD)-1 && (err = GetLastError()) == ERROR_OPERATION_ABORTED) {
125+
break;
126+
}
124127
if (n_read == 0) {
125128
int s;
126129
err = GetLastError();

0 commit comments

Comments
 (0)