From 60378ff941d5c32b09b2056457cd752b07b899d7 Mon Sep 17 00:00:00 2001 From: Ian Shehadeh Date: Fri, 7 Jan 2022 16:32:39 -0500 Subject: [PATCH] windows: fix ev/read hang when called on fs stream handles returned by CreateFileA and FILE_FLAG_OVERLAPPED support reading from arbitrary offsets. The offset is passed to ReadFile in through the OVERLAPPED structure. Since state->overlapped is zeroed ev_machine_read ReadFile would always read from the start of the file and never finish This commit changes ev_machine_read to update the offset to the number of bytes read before calling ReadFile. --- src/core/ev.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/core/ev.c b/src/core/ev.c index d37cb4f6..c47cf359 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -2219,6 +2219,10 @@ JanetAsyncStatus ev_machine_read(JanetListenerState *s, JanetAsyncEvent event) { } else #endif { + // Some handles (not all) read from the offset in lopOverlapped + // if its not set before calling `ReadFile` these streams will always read from offset 0 + state->overlapped.Offset = (DWORD) state->bytes_read; + status = ReadFile(s->stream->handle, state->chunk_buf, chunk_size, NULL, &state->overlapped); if (!status && (ERROR_IO_PENDING != WSAGetLastError())) { if (WSAGetLastError() == ERROR_BROKEN_PIPE) {