Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

cluster 'listening' handler's port number when worker calls listen(0) #4104

Closed
wants to merge 2 commits into from
Closed

Conversation

aadityabhatia
Copy link

http://nodejs.org/api/cluster.html#cluster_event_listening

According to the documentation, the event handler receives the address object with port property, which is set to the port that the worker passed to listen(). This covers the case where user manually specifies a port number, but if use specifies 0, a random port is assigned.

In such a situation, the event handler should receive the randomly assigned port number, not 0.

cluster = require 'cluster'
if not cluster.isMaster
    app = require('express').createServer()
    app.get '/*', (req, res) -> res.send "Hello Cluster"
    app.listen 0, ->
        console.log "I am listening on port " + app.address().port
        // output: I am listening on port 56789
else
    cluster.on 'listening', (worker, address) ->
        console.log "W#{worker.id} listening on port " + address.port
        // output: W1 listening on port 0
    cluster.fork()

@bnoordhuis
Copy link
Member

I'm okay with the change. Can you add a test case and sign the CLA?

@aadityabhatia
Copy link
Author

added the test case and signed the CLA

@langpavel
Copy link

@aadityabhatia Does this work with UNIX domain sockets?

@aadityabhatia
Copy link
Author

@langpavel Yes, in case of UNIX domain socket the server.address().port is set to -1, and therefore the functionality is unaffected.

@langpavel
Copy link

@aadityabhatia Thanks for answer
+1 for this patch

@bnoordhuis
Copy link
Member

Thanks, landed in c668185.

@bnoordhuis bnoordhuis closed this Oct 9, 2012
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants