Skip to content

Commit a1c5fe2

Browse files
committed
Processing (Windows): improves reliablity when terminating child process
1 parent 2d4a77b commit a1c5fe2

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

src/common/impl/processing_windows.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ const char* ffProcessSpawn(char* const argv[], bool useStdErr, FFProcessHandle*
140140
return NULL;
141141
}
142142

143+
static void terminateChildProcess(HANDLE hProcess, HANDLE hChildPipeRead, HANDLE hReadEvent, IO_STATUS_BLOCK* piosb)
144+
{
145+
IO_STATUS_BLOCK cancelIosb = {};
146+
if (NT_SUCCESS(NtCancelIoFileEx(hChildPipeRead, piosb, &cancelIosb)))
147+
{
148+
if (hReadEvent)
149+
NtWaitForSingleObject(hReadEvent, TRUE, &(LARGE_INTEGER) { .QuadPart = -100000 }); // wait for cancellation to complete
150+
}
151+
NtTerminateProcess(hProcess, 1);
152+
}
153+
143154
const char* ffProcessReadOutput(FFProcessHandle* handle, FFstrbuf* buffer)
144155
{
145156
assert(handle->pipeRead != INVALID_HANDLE_VALUE);
@@ -180,13 +191,13 @@ const char* ffProcessReadOutput(FFProcessHandle* handle, FFstrbuf* buffer)
180191
break;
181192

182193
case STATUS_TIMEOUT:
183-
CancelIo(hChildPipeRead);
184-
TerminateProcess(hProcess, 1);
194+
{
195+
terminateChildProcess(hProcess, hChildPipeRead, hReadEvent, &iosb);
185196
return "NtReadFile(hChildPipeRead) timed out";
197+
}
186198

187199
default:
188-
CancelIo(hChildPipeRead);
189-
TerminateProcess(hProcess, 1);
200+
terminateChildProcess(hProcess, hChildPipeRead, hReadEvent, &iosb);
190201
return "NtWaitForSingleObject(hReadEvent) failed";
191202
}
192203
}
@@ -196,8 +207,7 @@ const char* ffProcessReadOutput(FFProcessHandle* handle, FFstrbuf* buffer)
196207

197208
if (!NT_SUCCESS(status))
198209
{
199-
CancelIo(hChildPipeRead);
200-
TerminateProcess(hProcess, 1);
210+
terminateChildProcess(hProcess, hChildPipeRead, NULL, &iosb);
201211
return "NtReadFile(hChildPipeRead) failed";
202212
}
203213

src/common/windows/nt.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,3 +1373,14 @@ NTSYSAPI LOGICAL NTAPI RtlQueryPerformanceCounter(
13731373
NTSYSAPI LOGICAL NTAPI RtlQueryPerformanceFrequency(
13741374
_Out_ PLARGE_INTEGER PerformanceFrequency
13751375
);
1376+
1377+
NTSYSAPI NTSTATUS NTAPI NtCancelIoFileEx(
1378+
_In_ HANDLE FileHandle,
1379+
_In_opt_ PIO_STATUS_BLOCK IoRequestToCancel,
1380+
_Out_ PIO_STATUS_BLOCK IoStatusBlock
1381+
);
1382+
1383+
NTSYSAPI NTSTATUS NTAPI NtTerminateProcess(
1384+
_In_opt_ HANDLE ProcessHandle,
1385+
_In_ NTSTATUS ExitStatus
1386+
);

0 commit comments

Comments
 (0)