Files
node/test/parallel/test-net-transfer-guards.js
T
Guy Bedford f955c20dd1 net: support sending net.BoundSocket to threads and child processes
This adds support for transferring net.BoundSocket instances to other
threads via the worker_threads postMessage() transfer list, and for
sending them to child processes as the sendHandle argument of
subprocess.send(), following on from the BoundSocket introduction.

A BoundSocket reserves a port synchronously at construction time.
Making it transferable means a port can be reserved on one thread or
process and the bound (but not yet listening or connected) TCP handle
handed off to another to listen or connect on, without racing on the
bind.

For threads, BoundSocket implements kTransfer/kTransferList/
kDeserialize, moving the underlying TCP handle with the same mechanism
used for net.Socket and net.Server transfer. For child processes, the
handleConversion entry reuses the same transfer protocol on the
sending side and the same _TransferredBoundSocket deserialization path
on the receiving side; the underlying transport is that of cluster's
shared-handle scheduling: SCM_RIGHTS on Unix and WSADuplicateSocket on
Windows, both of which carry bind state.

In both cases the source instance is left in the adopted state:
address(), fd() and close() throw ERR_SOCKET_HANDLE_ADOPTED. Transfer
requires an un-adopted, open TCP handle, otherwise
ERR_WORKER_HANDLE_NOT_TRANSFERABLE is thrown; pipe (path) binds are
not transferable and throw ERR_INVALID_HANDLE_TYPE when sent over IPC.
On the receiving side the local address is re-derived from the handle
rather than trusted from serialized state.

Signed-off-by: Guy Bedford <guybedford@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64725
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-09-08 16:05:22 +02:00

81 lines
2.5 KiB
JavaScript

'use strict';
// This test verifies the guards that reject transferring a net.Socket,
// net.Server or net.BoundSocket that is not in a clean, movable state.
// Serialization is triggered with a MessageChannel, so no worker thread is
// required.
const common = require('../common');
const assert = require('assert');
const net = require('net');
const { MessageChannel } = require('worker_threads');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const { port1 } = new MessageChannel();
// A pipe BoundSocket is not transferable; only TCP binds can move between
// event loops.
{
const bound = new net.BoundSocket({ path: common.PIPE });
assert.throws(() => port1.postMessage({ bound }, [bound]), {
code: 'ERR_WORKER_HANDLE_NOT_TRANSFERABLE',
});
bound.close();
}
// A Server that is not listening (no handle) cannot be transferred.
{
const server = net.createServer();
assert.throws(() => port1.postMessage({ server }, [server]), {
code: 'ERR_WORKER_HANDLE_NOT_TRANSFERABLE',
});
}
// A closed BoundSocket no longer owns a handle and cannot be transferred.
{
const bound = new net.BoundSocket({ host: '127.0.0.1', port: 0 });
bound.close();
assert.throws(() => port1.postMessage({ bound }, [bound]), {
code: 'ERR_WORKER_HANDLE_NOT_TRANSFERABLE',
});
}
// An adopted BoundSocket no longer owns a handle and cannot be transferred.
{
const bound = new net.BoundSocket({ host: '127.0.0.1', port: 0 });
const socket = new net.Socket({ handle: bound });
assert.throws(() => port1.postMessage({ bound }, [bound]), {
code: 'ERR_WORKER_HANDLE_NOT_TRANSFERABLE',
});
socket.destroy();
}
// A Socket that has already consumed data cannot be transferred, because that
// buffered data would be lost on the sending side.
{
const server = net.createServer(common.mustCall((socket) => {
socket.on('data', common.mustCall(() => {
assert.throws(() => port1.postMessage({ socket }, [socket]), {
code: 'ERR_WORKER_HANDLE_NOT_TRANSFERABLE',
});
// The socket is still usable after a rejected transfer.
assert.ok(socket._handle != null);
socket.destroy();
server.close();
port1.close();
}));
}));
server.listen(0, common.mustCall(() => {
const client = net.connect(server.address().port, common.mustCall(() => {
client.end('data');
}));
// The server destroys the socket after the rejected transfer, which may
// surface as a reset on the client; swallow it either way.
client.on('error', () => {});
}));
}