uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
8.08k stars 574 forks source link

Detached child processes retains listen port (macos) #840

Closed porsager closed 1 year ago

porsager commented 1 year ago

If starting a child process after listen is called, this child process will somehow retain the port listened to, even after calling us_listen_socket_close or exit of the parent app. This only seems to happen on macos, I've tested on linux, and haven't been able to replicate.

Here is a reproducible sample

import uws from 'uWebSockets.js'
import cp from 'child_process'

const app = uws.App()

app.listen(3000, x => {
  console.log('Listening on', 3000)

  setTimeout(() => {
    uws.us_listen_socket_close(x)
    setTimeout(() => process.exit(), 1000)
    // port 3000 is now bound to the detached process started below (netstat -anvp tcp | grep .3000)
  }, 1000)
})

cp.spawn('node', ['--eval', 'setTimeout(() => {}, 100000)'], { detached: true }).unref()

If spawning the child process before calling listen everything works as expected.

uNetworkingAB commented 1 year ago

If spawning the child process before calling listen everything works as expected.

You typically do it this way. How fork works on different platforms is highly varying so you are always going to have differences if you don't follow the main usage (which is fork before listen)