-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
child_process: add 'overlapped' stdio flag #29412
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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)) { | ||
tarruda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break; | ||
} | ||
w(buf, read_count); | ||
} | ||
|
||
return 0; | ||
} |
This file contains hidden or 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 hidden or 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. | ||
bnoordhuis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// - 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); | ||
})); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.