Skip to content
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

src: fix crash on OnStreamRead on Windows #45878

Merged
merged 1 commit into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/stream_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,10 @@ void CustomBufferJSListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());

// To deal with the case where POLLHUP is received and UV_EOF is returned, as
// libuv returns an empty buffer (on unices only).
if (nread == UV_EOF && buf.base == nullptr) {
// In the case that there's an error and buf is null, return immediately.
// This can happen on unices when POLLHUP is received and UV_EOF is returned
// or when getting an error while performing a UV_HANDLE_ZERO_READ on Windows.
if (buf.base == nullptr && nread < 0) {
stream->CallJSOnreadMethod(nread, Local<ArrayBuffer>());
return;
}
Expand Down
47 changes: 47 additions & 0 deletions test/parallel/test-net-child-process-connect-reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const { spawn } = require('child_process');
const net = require('net');

if (process.argv[2] === 'child') {
const server = net.createServer(common.mustCall());
server.listen(0, common.mustCall(() => {
process.send({ type: 'ready', data: { port: server.address().port } });
}));
} else {
const cp = spawn(process.execPath,
[__filename, 'child'],
{
stdio: ['ipc', 'inherit', 'inherit']
});

cp.on('exit', common.mustCall((code, signal) => {
assert.strictEqual(code, null);
assert.strictEqual(signal, 'SIGKILL');
}));

cp.on('message', common.mustCall((msg) => {
const { type, data } = msg;
assert.strictEqual(type, 'ready');
const port = data.port;

const conn = net.createConnection({
port,
onread: {
buffer: Buffer.alloc(65536),
callback: () => {},
}
});

conn.on('error', (err) => {
// Error emitted on Windows.
assert.strictEqual(err.code, 'ECONNRESET');
});

conn.on('connect', common.mustCall(() => {
cp.kill('SIGKILL');
}));
}));
}