Skip to content

Commit 33a141f

Browse files
Complete pending write operations before canceling io. (#628)
* Complete pending write operations before canceling io. * Removed comment, question has been addressed in mr. * include useful comment from vthiebaut10 --------- Co-authored-by: Christopher Greene <[email protected]>
1 parent 59d9124 commit 33a141f

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

contrib/win32/win32compat/termio.c

+18-2
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,22 @@ int
263263
syncio_close(struct w32_io* pio)
264264
{
265265
debug4("syncio_close - pio:%p", pio);
266+
267+
/*
268+
* Wait for io write operation that is called by worker thread to terminate
269+
* to avoid the write operation being terminated prematurely by CancelIoEx.
270+
* If you see any process waiting here indefinitely - its because no one
271+
* is draining from other end of the pipe. This is an unfortunate
272+
* consequence that should otherwise have very little impact on practical
273+
* scenarios.
274+
*/
275+
if (pio->write_details.pending) {
276+
WaitForSingleObject(pio->write_overlapped.hEvent, INFINITE);
277+
278+
/* drain queued APCs */
279+
SleepEx(0, TRUE);
280+
}
281+
266282
CancelIoEx(WINHANDLE(pio), NULL);
267283

268284
/* If io is pending, let worker threads exit. */
@@ -279,10 +295,10 @@ syncio_close(struct w32_io* pio)
279295

280296
WaitForSingleObject(pio->read_overlapped.hEvent, INFINITE);
281297
}
282-
if (pio->write_details.pending)
283-
WaitForSingleObject(pio->write_overlapped.hEvent, INFINITE);
298+
284299
/* drain queued APCs */
285300
SleepEx(0, TRUE);
301+
286302
/* TODO - fix this, closing Console handles is interfering with TTY/PTY rendering */
287303
if (FILETYPE(pio) != FILE_TYPE_CHAR)
288304
CloseHandle(WINHANDLE(pio));

0 commit comments

Comments
 (0)