Skip to content

Commit

Permalink
fix: check if the socket exists in the worker
Browse files Browse the repository at this point in the history
Reference: https://nodejs.org/api/child_process.html#child_process_example_sending_a_socket_object

> Any 'message' handlers in the subprocess should verify that socket
exists, as the connection may have been closed during the time it takes
to send the connection to the child.

Without this check, the following exception could be thrown:

internal/per_context/primordials.js:23
  return (thisArg, ...args) => ReflectApply(func, thisArg, args);
                               ^

TypeError: Cannot convert undefined or null to object
    at hasOwnProperty (<anonymous>)
    at internal/per_context/primordials.js:23:32
    at getOrSetAsyncId (internal/async_hooks.js:396:7)
    at Server.connectionListener (_http_server.js:414:5)
    at Server.emit (events.js:315:20)

Related: #1
  • Loading branch information
darrachequesne committed Jul 1, 2021
1 parent ad38b06 commit 7069fbc
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ const setupWorker = (io) => {
process.on("message", ({ type, data }, socket) => {
switch (type) {
case "sticky:connection":
if (!socket) {
// might happen if the socket is closed during the transfer to the worker
// see https://nodejs.org/api/child_process.html#child_process_example_sending_a_socket_object
return;
}
io.httpServer.emit("connection", socket); // inject connection
// republish first chunk
if (socket._handle.onread.length === 1) {
Expand Down

0 comments on commit 7069fbc

Please sign in to comment.