Skip to content

Complete pending write operations before canceling io. #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions contrib/win32/win32compat/termio.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ int
syncio_close(struct w32_io* pio)
{
debug4("syncio_close - pio:%p", pio);

/*
* Wait for io write operation that is called by worker thread to terminate
* to avoid the write operation being terminated prematurely by CancelIoEx.
* If you see any process waiting here indefinitely - its because no one
* is draining from other end of the pipe. This is an unfortunate
* consequence that should otherwise have very little impact on practical
* scenarios.
*/
if (pio->write_details.pending) {
WaitForSingleObject(pio->write_overlapped.hEvent, INFINITE);

/* drain queued APCs */
SleepEx(0, TRUE);
Copy link
Collaborator

@vthiebaut10 vthiebaut10 Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you added this call to SleepEx to this if statement block?

}

CancelIoEx(WINHANDLE(pio), NULL);

/* If io is pending, let worker threads exit. */
Expand All @@ -276,10 +292,10 @@ syncio_close(struct w32_io* pio)

WaitForSingleObject(pio->read_overlapped.hEvent, INFINITE);
}
if (pio->write_details.pending)
WaitForSingleObject(pio->write_overlapped.hEvent, INFINITE);

/* drain queued APCs */
SleepEx(0, TRUE);

/* TODO - fix this, closing Console handles is interfering with TTY/PTY rendering */
if (FILETYPE(pio) != FILE_TYPE_CHAR)
CloseHandle(WINHANDLE(pio));
Expand Down