Cluster doesn't terminate if workers are listening to a port
Hi, everyone!
I think I found a bug(?) in cluster. It came up when a colleague of mine was debugging an HTTP service on a port. He uses supervisor to track changes in local FS and restart his app with those changes and recluster to launch several app instances on one listen handle. But the changes he made didn't appear in the running application and he had to restart the app manually. I started inspecting the issue and I was able to roll out the external dependencies and extract a minimal code sample for reproducing the issue.
- Steps to reproduce:
- Use any node/iojs >= 2.3.0 to run the minimal code example I've extracted called echo-cluster
- Open in a browser localhost:8080/ping or some other path. You should recieve 200 and in the body the path you requested.
- Copy the cluster master process PID from the output of echo-cluster. It should be in square brackets next to the text "Cluster started". Use the PID to kill the cluster
kill -s SIGINT <MASTER_PID>
- Result: A worker still responds on that port and responds like nothing has happened.
- Expected behavior: The worker should be terminated together with master.
- Version: v2.3.0+
- Platform: Linux debian 4.4.0-1-amd64 #1 SMP Debian 4.4.6-1 (2016-03-17) x86_64 GNU/Linux
- Subsystem: cluster
- Notes:
- I had an old project on 0.10.x that used the same dev scaffolding and I didn't experience this issue, this made me think it was a regression. So after some switching between versions and running my test code I found that from v2.3.0 forward this behavior is reproducible. v2.2.1 works fine
- I tried reproducing the issue with
curlbut failed. It is probably of the way the browser interprets the keep-alive header. I would be happy if someone explained why curl doesn't work, I wasn't able to find an answer. - Another strange thing is that the issue can't be reproduced when the HTTP server listens to a socket file and the client reaches the node app through a nginx proxy
Sorry, this was a long one, but I that's as brief as I can get.
Thanks for taking the time to bisect. I'll take a guess and say it's commit 9c0a1b8 - cluster: wait on servers closing before disconnect.
The reason you see it with a browser but not with curl is probably because the former has keep-alive enabled. That would also explain why listening on a UNIX socket doesn't behave that way: no keep-alive.
I've played around with firefox'es keep-alive timeout config and I can confirm that the worker dies when the browser closes the connection after the timeout has ran out. So I understand that behavior is correct in terms of supporting persistent HTTP connections, but initially to me it seemed like a bug. The question is should the handling of such a situation be on app side or in node?
The app side. It's consistent with how a single-process HTTP server works; calling server.close() won't actually exit until the last connection closes.
Closing as not an issue with core.