Compare commits

..

1 Commits

Author SHA1 Message Date
pfg
5d90ee679f test: add tls async cb after socket end 2025-05-29 17:52:35 -07:00
5 changed files with 98 additions and 78 deletions

View File

@@ -796,6 +796,7 @@ const ServerPrototype = {
if (typeof optionalCallback === "function") process.nextTick(optionalCallback, $ERR_SERVER_NOT_RUNNING());
return;
}
this[serverSymbol] = undefined;
const connectionsCheckingInterval = this[kConnectionsCheckingInterval];
if (connectionsCheckingInterval) {
connectionsCheckingInterval._destroyed = true;

View File

@@ -88,12 +88,14 @@ function isIP(s): 0 | 4 | 6 {
}
const bunTlsSymbol = Symbol.for("::buntls::");
const bunSocketServerConnections = Symbol.for("::bunnetserverconnections::");
const bunSocketServerOptions = Symbol.for("::bunnetserveroptions::");
const owner_symbol = Symbol("owner_symbol");
const kServerSocket = Symbol("kServerSocket");
const kBytesWritten = Symbol("kBytesWritten");
const bunTLSConnectOptions = Symbol.for("::buntlsconnectoptions::");
const bunTLSServerSession = Symbol.for("::bunTLSServerSession::");
const kReinitializeHandle = Symbol("kReinitializeHandle");
const kRealListen = Symbol("kRealListen");
@@ -338,7 +340,7 @@ const ServerHandlers: SocketHandler = {
const data = this.data;
if (!data) return;
data.server._connections--;
data.server[bunSocketServerConnections]--;
{
if (!data[kclosed]) {
data[kclosed] = true;
@@ -384,7 +386,7 @@ const ServerHandlers: SocketHandler = {
return;
}
}
if (self.maxConnections != null && self._connections >= self.maxConnections) {
if (self.maxConnections && self[bunSocketServerConnections] >= self.maxConnections) {
const data = {
localAddress: _socket.localAddress,
localPort: _socket.localPort || this.localPort,
@@ -403,7 +405,7 @@ const ServerHandlers: SocketHandler = {
const bunTLS = _socket[bunTlsSymbol];
const isTLS = typeof bunTLS === "function";
self._connections++;
self[bunSocketServerConnections]++;
if (pauseOnConnect) {
_socket.pause();
@@ -438,6 +440,16 @@ const ServerHandlers: SocketHandler = {
self.servername = socket.getServername();
const server = self.server;
self.alpnProtocol = socket.alpnProtocol;
if (!server[bunTLSServerSession]) {
server[bunTLSServerSession] = true;
if (server.listenerCount("newSession") > 0) {
const done = () => {};
server.emit("newSession", Buffer.alloc(0), self.getSession(), done);
}
} else if (server.listenerCount("resumeSession") > 0) {
const cb = () => {};
server.emit("resumeSession", Buffer.alloc(0), cb);
}
if (self._requestCert || self._rejectUnauthorized) {
if (verifyError) {
self.authorized = false;
@@ -2074,6 +2086,7 @@ function Server(options?, connectionListener?) {
// https://nodejs.org/api/net.html#netcreateserveroptions-connectionlistener
const {
maxConnections, //
allowHalfOpen = false,
keepAlive = false,
keepAliveInitialDelay = 0,
@@ -2090,13 +2103,16 @@ function Server(options?, connectionListener?) {
this._unref = false;
this.listeningId = 1;
this[bunSocketServerConnections] = 0;
this[bunSocketServerOptions] = undefined;
this[bunTLSServerSession] = false;
this.allowHalfOpen = allowHalfOpen;
this.keepAlive = keepAlive;
this.keepAliveInitialDelay = keepAliveInitialDelay;
this.highWaterMark = highWaterMark;
this.pauseOnConnect = Boolean(pauseOnConnect);
this.noDelay = noDelay;
this.maxConnections = Number.isSafeInteger(maxConnections) && maxConnections > 0 ? maxConnections : 0;
options.connectionListener = connectionListener;
this[bunSocketServerOptions] = options;
@@ -2159,7 +2175,7 @@ Server.prototype[Symbol.asyncDispose] = function () {
};
Server.prototype._emitCloseIfDrained = function _emitCloseIfDrained() {
if (this._handle || this._connections > 0) {
if (this._handle || this[bunSocketServerConnections] > 0) {
return;
}
process.nextTick(() => {
@@ -2188,7 +2204,7 @@ Server.prototype.getConnections = function getConnections(callback) {
//in Bun case we will never error on getConnections
//node only errors if in the middle of the couting the server got disconnected, what never happens in Bun
//if disconnected will only pass null as well and 0 connected
callback(null, this._handle ? this._connections : 0);
callback(null, this._handle ? this[bunSocketServerConnections] : 0);
}
return this;
};

View File

@@ -1,32 +0,0 @@
const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');
['on', 'addListener', 'prependListener'].forEach((testFn) => {
let received = '';
const server = http.createServer(function(req, res) {
res.writeHead(200);
res.end();
req.socket[testFn]('data', function(data) {
received += data;
});
server.close();
}).listen(0, function() {
const socket = net.connect(this.address().port, function() {
socket.write('PUT / HTTP/1.1\r\nHost: example.com\r\n\r\n');
socket.once('data', function() {
socket.end('hello world');
});
socket.on('end', common.mustCall(() => {
assert.strictEqual(received, 'hello world',
`failed for socket.${testFn}`);
}));
});
});
});

View File

@@ -1,41 +0,0 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
let firstSocket;
const dormantServer = net.createServer(common.mustNotCall());
const server = net.createServer(common.mustCall((socket) => {
firstSocket = socket;
}));
dormantServer.maxConnections = 0;
server.maxConnections = 1;
dormantServer.on('drop', common.mustCall((data) => {
assert.strictEqual(!!data.localAddress, true);
assert.strictEqual(!!data.localPort, true);
assert.strictEqual(!!data.remoteAddress, true);
assert.strictEqual(!!data.remotePort, true);
assert.strictEqual(!!data.remoteFamily, true);
dormantServer.close();
}));
server.on('drop', common.mustCall((data) => {
assert.strictEqual(!!data.localAddress, true);
assert.strictEqual(!!data.localPort, true);
assert.strictEqual(!!data.remoteAddress, true);
assert.strictEqual(!!data.remotePort, true);
assert.strictEqual(!!data.remoteFamily, true);
firstSocket.destroy();
server.close();
}));
dormantServer.listen(0, () => {
net.createConnection(dormantServer.address().port);
});
server.listen(0, () => {
net.createConnection(server.address().port);
net.createConnection(server.address().port);
});

View File

@@ -0,0 +1,76 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const fixtures = require('../common/fixtures');
const SSL_OP_NO_TICKET = require('crypto').constants.SSL_OP_NO_TICKET;
const tls = require('tls');
// Check that TLS1.2 session resumption callbacks don't explode when made after
// the tls socket is destroyed. Disable TLS ticket support to force the legacy
// session resumption mechanism to be used.
// TLS1.2 is the last protocol version to support TLS sessions, after that the
// new and resume session events will never be emitted on the server.
const options = {
secureOptions: SSL_OP_NO_TICKET,
key: fixtures.readKey('rsa_private.pem'),
cert: fixtures.readKey('rsa_cert.crt')
};
const server = tls.createServer(options, common.mustCall());
let sessionCb = null;
let client = null;
server.on('newSession', common.mustCall((key, session, done) => {
done();
}));
server.on('resumeSession', common.mustCall((id, cb) => {
sessionCb = cb;
// Destroy the client and then call the session cb, to check that the cb
// doesn't explode when called after the handle has been destroyed.
next();
}));
server.listen(0, common.mustCall(() => {
const clientOpts = {
// Don't send a TLS1.3/1.2 ClientHello, they contain a fake session_id,
// which triggers a 'resumeSession' event for client1. TLS1.2 ClientHello
// won't have a session_id until client2, which will have a valid session.
maxVersion: 'TLSv1.2',
port: server.address().port,
rejectUnauthorized: false,
session: false
};
const s1 = tls.connect(clientOpts, common.mustCall(() => {
clientOpts.session = s1.getSession();
console.log('1st secure');
s1.destroy();
const s2 = tls.connect(clientOpts, (s) => {
console.log('2nd secure');
s2.destroy();
}).on('connect', common.mustCall(() => {
console.log('2nd connected');
client = s2;
next();
}));
}));
}));
function next() {
if (!client || !sessionCb)
return;
client.destroy();
setTimeout(common.mustCall(() => {
sessionCb();
server.close();
}), 100);
}