1
0
Fork 0
forked from svrjs/svrjs

Fix Bun cluster shim and mitigate Bun segmentation faults

This commit is contained in:
Dorian Niemiec 2023-08-10 04:01:50 +02:00
parent 8ca945cdbd
commit 7820c5aade

61
svr.js
View file

@ -191,6 +191,12 @@ if (!singlethreaded) {
fakeIPCConnection.end(message); fakeIPCConnection.end(message);
}); });
}; };
process.removeFakeIPC = function() {
// Close IPC server
process.send = function() {};
fakeIPCServer.close();
}
} }
} }
@ -258,7 +264,8 @@ if (!singlethreaded) {
socket.on("end", function () { socket.on("end", function () {
newWorker.emit("message", receivedData); newWorker.emit("message", receivedData);
}); });
}).listen(os.platform() === "win32" ? path.join("\\\\?\\pipe", __dirname, "temp/.P" + newWorker.process.pid + ".ipc") : (__dirname + "/temp/.P" + newWorker.process.pid + ".ipc")); });
fakeWorkerIPCServer.listen(os.platform() === "win32" ? path.join("\\\\?\\pipe", __dirname, "temp/.P" + newWorker.process.pid + ".ipc") : (__dirname + "/temp/.P" + newWorker.process.pid + ".ipc"));
// Cleanup when worker process exits // Cleanup when worker process exits
newWorker.on("exit", function () { newWorker.on("exit", function () {
@ -275,10 +282,13 @@ if (!singlethreaded) {
fakeWorkerIPCConnection.end(message); fakeWorkerIPCConnection.end(message);
}); });
} catch (err) { } catch (err) {
if (tries > 25) throw err; if (tries > 25) {
newWorker.emit("error", err);
return;
}
setTimeout(function () { setTimeout(function () {
newWorker.send(message, undefined, undefined, tries + 1); newWorker.send(message, fakeParam2, fakeParam3, fakeParam4, tries + 1);
}, 10); }, 10);
} }
}; };
@ -4803,13 +4813,9 @@ function start(init) {
var commands = { var commands = {
close: function () { close: function () {
try { try {
if(server.listening || server2.listening) { server.close();
server.close(); if (secure && !disableNonEncryptedServer) {
if (secure && !disableNonEncryptedServer) { server2.close();
server2.close();
}
} else {
throw new Error("Server is already closed.");
} }
if (cluster.isPrimary === undefined) serverconsole.climessage("Server closed."); if (cluster.isPrimary === undefined) serverconsole.climessage("Server closed.");
else { else {
@ -4823,15 +4829,11 @@ function start(init) {
}, },
open: function () { open: function () {
try { try {
if(!server.listening) { if (secure) {
if (secure) { server.listen(sport);
server.listen(sport); if (!disableNonEncryptedServer) server2.listen(port);
if (!disableNonEncryptedServer) server2.listen(port);
} else {
server.listen(port); // Reopen Server
}
} else { } else {
throw new Error("Server is already opened."); server.listen(port); // Reopen Server
} }
if (cluster.isPrimary === undefined) serverconsole.climessage("Server opened."); if (cluster.isPrimary === undefined) serverconsole.climessage("Server opened.");
else { else {
@ -4866,25 +4868,31 @@ function start(init) {
if(server2.listening) { if(server2.listening) {
try { try {
server2.close(function() { server2.close(function() {
if(!process.removeFakeIPC) {
if (typeof retcode == "number") {
process.exit(retcode);
} else {
process.exit(0);
}
}
});
} catch(err) {
if(!process.removeFakeIPC) {
if (typeof retcode == "number") { if (typeof retcode == "number") {
process.exit(retcode); process.exit(retcode);
} else { } else {
process.exit(0); process.exit(0);
} }
}); }
} catch(err) { }
} else {
if(!process.removeFakeIPC) {
if (typeof retcode == "number") { if (typeof retcode == "number") {
process.exit(retcode); process.exit(retcode);
} else { } else {
process.exit(0); process.exit(0);
} }
} }
} else {
if (typeof retcode == "number") {
process.exit(retcode);
} else {
process.exit(0);
}
} }
}); });
} catch(err) { } catch(err) {
@ -4894,6 +4902,7 @@ function start(init) {
process.exit(0); process.exit(0);
} }
} }
if(process.removeFakeIPC) process.removeFakeIPC();
} else { } else {
if (typeof retcode == "number") { if (typeof retcode == "number") {
process.exit(retcode); process.exit(retcode);