-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
child_process: add 'overlapped' stdio flag
The 'overlapped' value sets the UV_OVERLAPPED_PIPE libuv flag in the child process stdio. Fixes: #29238 PR-URL: #29412 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
8 changed files
with
255 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
#include <errno.h> | ||
#include <unistd.h> | ||
|
||
static size_t r(char* buf, size_t buf_size) { | ||
ssize_t read_count; | ||
do | ||
read_count = read(0, buf, buf_size); | ||
while (read_count < 0 && errno == EINTR); | ||
if (read_count <= 0) | ||
abort(); | ||
return (size_t)read_count; | ||
} | ||
|
||
static void w(const char* buf, size_t count) { | ||
const char* end = buf + count; | ||
|
||
while (buf < end) { | ||
ssize_t write_count; | ||
do | ||
write_count = write(1, buf, count); | ||
while (write_count < 0 && errno == EINTR); | ||
if (write_count <= 0) | ||
abort(); | ||
buf += write_count; | ||
} | ||
|
||
fprintf(stderr, "%zu", count); | ||
fflush(stderr); | ||
} | ||
|
||
int main(void) { | ||
w("0", 1); | ||
|
||
while (1) { | ||
char buf[256]; | ||
size_t read_count = r(buf, sizeof(buf)); | ||
// The JS part (test-child-process-stdio-overlapped.js) only writes the | ||
// "exit" string when the buffer is empty, so the read is guaranteed to be | ||
// atomic due to it being less than PIPE_BUF. | ||
if (!strncmp(buf, "exit", read_count)) { | ||
break; | ||
} | ||
w(buf, read_count); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <windows.h> | ||
|
||
static char buf[256]; | ||
static DWORD read_count; | ||
static DWORD write_count; | ||
static HANDLE stdin_h; | ||
static OVERLAPPED stdin_o; | ||
|
||
static void die(const char* buf) { | ||
fprintf(stderr, "%s\n", buf); | ||
fflush(stderr); | ||
exit(100); | ||
} | ||
|
||
static void overlapped_read(void) { | ||
if (ReadFile(stdin_h, buf, sizeof(buf), NULL, &stdin_o)) { | ||
// Since we start the read operation immediately before requesting a write, | ||
// it should never complete synchronously since no data would be available | ||
die("read completed synchronously"); | ||
} | ||
if (GetLastError() != ERROR_IO_PENDING) { | ||
die("overlapped read failed"); | ||
} | ||
} | ||
|
||
static void write(const char* buf, size_t buf_size) { | ||
overlapped_read(); | ||
DWORD write_count; | ||
HANDLE stdout_h = GetStdHandle(STD_OUTPUT_HANDLE); | ||
if (!WriteFile(stdout_h, buf, buf_size, &write_count, NULL)) { | ||
die("overlapped write failed"); | ||
} | ||
fprintf(stderr, "%d", write_count); | ||
fflush(stderr); | ||
} | ||
|
||
int main(void) { | ||
HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
if (event == NULL) { | ||
die("failed to create event handle"); | ||
} | ||
|
||
stdin_h = GetStdHandle(STD_INPUT_HANDLE); | ||
stdin_o.hEvent = event; | ||
|
||
write("0", 1); | ||
|
||
while (1) { | ||
DWORD result = WaitForSingleObject(event, INFINITE); | ||
if (result == WAIT_OBJECT_0) { | ||
if (!GetOverlappedResult(stdin_h, &stdin_o, &read_count, FALSE)) { | ||
die("failed to get overlapped read result"); | ||
} | ||
if (strncmp(buf, "exit", read_count) == 0) { | ||
break; | ||
} | ||
write(buf, read_count); | ||
} else { | ||
char emsg[0xfff]; | ||
int ecode = GetLastError(); | ||
DWORD rv = FormatMessage( | ||
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | ||
NULL, | ||
ecode, | ||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | ||
(LPSTR)emsg, | ||
sizeof(emsg), | ||
NULL); | ||
if (rv > 0) { | ||
snprintf(emsg, sizeof(emsg), | ||
"WaitForSingleObject failed. Error %d (%s)", ecode, emsg); | ||
} else { | ||
snprintf(emsg, sizeof(emsg), | ||
"WaitForSingleObject failed. Error %d", ecode); | ||
} | ||
die(emsg); | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Test for "overlapped" stdio option. This test uses the "overlapped-checker" | ||
// helper program which basically a specialized echo program. | ||
// | ||
// The test has two goals: | ||
// | ||
// - Verify that overlapped I/O works on windows. The test program will deadlock | ||
// if stdin doesn't have the FILE_FLAG_OVERLAPPED flag set on startup (see | ||
// test/overlapped-checker/main_win.c for more details). | ||
// - Verify that "overlapped" stdio option works transparently as a pipe (on | ||
// unix/windows) | ||
// | ||
// This is how the test works: | ||
// | ||
// - This script assumes only numeric strings are written to the test program | ||
// stdout. | ||
// - The test program will be spawned with "overlapped" set on stdin and "pipe" | ||
// set on stdout/stderr and at startup writes a number to its stdout | ||
// - When this script receives some data, it will parse the number, add 50 and | ||
// write to the test program's stdin. | ||
// - The test program will then echo the number back to us which will repeat the | ||
// cycle until the number reaches 200, at which point we send the "exit" | ||
// string, which causes the test program to exit. | ||
// - Extra assertion: Every time the test program writes a string to its stdout, | ||
// it will write the number of bytes written to stderr. | ||
// - If overlapped I/O is not setup correctly, this test is going to hang. | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const child_process = require('child_process'); | ||
|
||
const exeExtension = process.platform === 'win32' ? '.exe' : ''; | ||
const exe = 'overlapped-checker' + exeExtension; | ||
const exePath = path.join(path.dirname(process.execPath), exe); | ||
|
||
const child = child_process.spawn(exePath, [], { | ||
stdio: ['overlapped', 'pipe', 'pipe'] | ||
}); | ||
|
||
child.stdin.setEncoding('utf8'); | ||
child.stdout.setEncoding('utf8'); | ||
child.stderr.setEncoding('utf8'); | ||
|
||
function writeNext(n) { | ||
child.stdin.write((n + 50).toString()); | ||
} | ||
|
||
child.stdout.on('data', (s) => { | ||
const n = Number(s); | ||
if (n >= 200) { | ||
child.stdin.write('exit'); | ||
return; | ||
} | ||
writeNext(n); | ||
}); | ||
|
||
let stderr = ''; | ||
child.stderr.on('data', (s) => { | ||
stderr += s; | ||
}); | ||
|
||
child.stderr.on('end', common.mustCall(() => { | ||
// This is the sequence of numbers sent to us: | ||
// - 0 (1 byte written) | ||
// - 50 (2 bytes written) | ||
// - 100 (3 bytes written) | ||
// - 150 (3 bytes written) | ||
// - 200 (3 bytes written) | ||
assert.strictEqual(stderr, '12333'); | ||
})); | ||
|
||
child.on('exit', common.mustCall((status) => { | ||
// The test program will return the number of writes as status code. | ||
assert.strictEqual(status, 0); | ||
})); |