From 12cfda1f584576a0e54b82e710a54b5ef66e6dbe Mon Sep 17 00:00:00 2001 From: Andrew Owen Date: Sun, 11 Jul 2021 03:21:55 -0600 Subject: [PATCH 1/2] Add TerminateProcess to janet_proc_gc and os_proc_kill on Windows --- src/core/os.c | 2 ++ test/suite0009.janet | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/src/core/os.c b/src/core/os.c index 4531bec9..1f519202 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -434,6 +434,7 @@ static int janet_proc_gc(void *p, size_t s) { JanetProc *proc = (JanetProc *) p; #ifdef JANET_WINDOWS if (!(proc->flags & JANET_PROC_CLOSED)) { + TerminateProcess(proc->pHandle, 1); CloseHandle(proc->pHandle); CloseHandle(proc->tHandle); } @@ -519,6 +520,7 @@ static Janet os_proc_kill(int32_t argc, Janet *argv) { janet_panicf("cannot close process handle that is already closed"); } proc->flags |= JANET_PROC_CLOSED; + TerminateProcess(proc->pHandle, 1); CloseHandle(proc->pHandle); CloseHandle(proc->tHandle); #else diff --git a/test/suite0009.janet b/test/suite0009.janet index 2b744a73..965a71a7 100644 --- a/test/suite0009.janet +++ b/test/suite0009.janet @@ -47,6 +47,11 @@ (assert-no-error "pipe stdin to process 2" (os/proc-wait p)) (assert (= "hello!" (string/trim x)) "round trip pipeline in process")) +(let [p (os/spawn [janet "-e" `(do (ev/sleep 30) (os/exit 24)`] :p)] + (os/proc-kill p) + (def retval (os/proc-wait p)) + (assert (not= retval 24) "Process was *not* terminated by parent")) + # Parallel subprocesses (defn calc-1 From e8a86013dab4334e2f572fdaee8c4ab781db9bdd Mon Sep 17 00:00:00 2001 From: Andrew Owen Date: Sat, 24 Jul 2021 02:30:00 -0600 Subject: [PATCH 2/2] Add fixes for :write on filestreams that come from os/open --- src/core/ev.c | 10 ++++++++++ test/suite0009.janet | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/core/ev.c b/src/core/ev.c index ccd65010..298aad8a 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -1838,6 +1838,16 @@ JanetAsyncStatus ev_machine_write(JanetListenerState *s, JanetAsyncEvent event) } else #endif { + // File handles in IOCP need to specify this if they are writing to the + // ends of files, like how this is used here. + // If the underlying resource doesn't support seeking + // byte offsets, they will be ignored + // but this otherwise writes to the end of the file in question + // Right now, os/open streams aren't seekable, so this works. + // for more details see the lpOverlapped parameter in + // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile + state->overlapped.Offset = (DWORD) 0xFFFFFFFF; + state->overlapped.OffsetHigh = (DWORD) 0xFFFFFFFF; status = WriteFile(s->stream->handle, bytes, len, NULL, &state->overlapped); if (!status && (ERROR_IO_PENDING != WSAGetLastError())) { janet_cancel(s->fiber, janet_ev_lasterr()); diff --git a/test/suite0009.janet b/test/suite0009.janet index 965a71a7..fdbe1b81 100644 --- a/test/suite0009.janet +++ b/test/suite0009.janet @@ -105,6 +105,18 @@ (file/close outfile) (os/rm "unique.txt")) +# Ensure that the stream created by os/open works + +(assert-no-error "File writing 4.1" + (def outstream (os/open "unique.txt" :wct)) + (defer (:close outstream) + (:write outstream "123\n") + (:write outstream "456\n")) + # Cast to string to enable comparison + (assert (= "123\n456\n" (string (slurp "unique.txt"))) "File writing 4.2") + (os/rm "unique.txt")) + + # ev/gather (assert (deep= @[1 2 3] (ev/gather 1 2 3)) "ev/gather 1")