Archive for April, 2011

3 Node.js gotchas that I’ve found in the past 3 days

I’ve been writing a NoSQL database server in Node.js. Now before anybody lynch me for mentioning 2 buzzwords or for the fact that I’m writing a database server, please know that I have perfectly valid and informed reasons for doing this.

Ignoring the oddity that is Javascript, one of the earliest issues that I faced was the fact that there seemed to be no way to be notified of when a fs.createWriteStream(...).write(...) completes. WriteStream has a ‘drain’ event but I’m not interested in when the entire write queue is drained, I’m only interested in when a specific write has been drained. After reading the Node.js code it turned out that WriteStream.write() does accept a callback for completion notification, it just isn’t documented.

Another issue was that EPIPE errors on the socket would cause Node.js to throw an exception. I was initially confused because I thought such errors would be emitted through the ‘error’ event, which I haven’t bound on yet. In a mailing list post Ryan Dahl said that errors are thrown as exceptions except when the ‘error’ event is bound, which he admits is a rather dubious behavior. Again this isn’t documented.

I also found out that Node.js would close the connection whenever it encounters a socket error. Any socket error. This is also rather dubious behavior because it would make it impossible to handle certain edge cases involving EPIPE. Suppose that the server is sending some data to the client. The client hasn’t acknowledged that yet, but sends a bunch of data to the server and closes the connection. The server will now encounter an EPIPE error upon writing, making it impossible to read the remaining data that the client has sent. Granted, such a protocol is rather dubious by itself but I do think Node.js should make it at least possible to handle this situation.

Those who want to develop in Node.js have been warned.

Comments (6)