sgentle/phantomjs-node — pull request #59 by catshirt (2013-01-04 03:04:36+00:00)
Title: wait for listening event before trying to access a server address()

issue: phantom.coffee tries to access server.address() to pass to startPhantomProcess. the node docs you should ["[not] call server.address() until the 'listening' event has been emitted."](http://nodejs.org/api/net.html#net_server_address).

manifestation: for some reason, accessing it synchronously actually works fine in many cases (though it doesn't seem it should). however, in some cases address() returns null. i think i've narrowed it down in this [test case](https://gist.github.com/2c2b104a26f97ba863bb): server.address() will return null when accessed asynchronously in a child process.

implications: this means you can't successfully use phantom.js inside a child process. arguably i'd think you should probably only ever run phantom.js in a forked process. that aside, this is also a problem on distributed systems who host your application as a child process, as suggested in Issue #56.

solution: the solution is to ensure the server is listening before trying to create the phantom process so you ensure you have a port to read.