| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c23ece7 commit d49d990
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,15 +39,23 @@ if (process.argv[2] !== 'child') { | |||
| 39 | 39 | process.send('handle', socket); | |
| 40 | 40 | } | |
| 41 | 41 | ||
| 42 | + // As a side-effect, listening for the message event will ref the IPC channel, | ||
| 43 | + // so the child process will stay alive as long as it has a parent process/IPC | ||
| 44 | + // channel. Once this is done, we can unref our client and server sockets, and | ||
| 45 | + // the only thing keeping this worker alive will be IPC. This is important, | ||
| 46 | + // because it means a worker with no parent will have no referenced handles, | ||
| 47 | + // thus no work to do, and will exit immediately, preventing process leaks. | ||
| 48 | + process.on('message', function() {}); | ||
| 49 | + | ||
| 42 | 50 | const server = net.createServer((c) => { | |
| 43 | 51 | process.once('message', function(msg) { | |
| 44 | 52 | assert.strictEqual(msg, 'got'); | |
| 45 | 53 | c.end('hello'); | |
| 46 | 54 | }); | |
| 47 | 55 | socketConnected(); | |
| 48 | - }); | ||
| 56 | + }).unref(); | ||
| 49 | 57 | server.listen(0, common.localhostIPv4, () => { | |
| 50 | 58 | const port = server.address().port; | |
| 51 | - socket = net.connect(port, common.localhostIPv4, socketConnected); | ||
| 59 | + socket = net.connect(port, common.localhostIPv4, socketConnected).unref(); | ||
| 52 | 60 | }); | |
| 53 | 61 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments