Why isn't node.js garbage collecting system resources (like file descriptors)?

I've been doing a bunch of async file I / O work in node.js, and it seems to me that one has to be extremely careful to catch all error paths so as not to accidentally skip file descriptors in error conditions.But Javascript is a garbage collector and that's exactly something that you don't need to worry about in a garbage collection environment.

If the variable containing the file descriptor goes out of scope or becomes inaccessible from your code, it seems that the node.js garbage collector should know this and properly clean up the system resource for you. Javascript in the browser does this with DOM objects. If a DOM object has been removed from the DOM and held in a Javascript variable that then goes out of scope or becomes inaccessible, the browser will automatically clear it for you. You don't need to manually delete it. So, of course, this level of integration with non-JS resources is possible.

So I'm wondering why node.js doesn't have this capability, as it seems like it would be very useful and make it a much more robust server development environment?

This is just the case of node.js, which is at a fairly early stage in its development, and it might be a logical improvement over time, but are there more important things to work with at the moment?

Is this a case of node.js left at arm's length with the V8 engine and thus not integrating with the level it would need to handle the garbage collection system?

Is there some conceptual difficulty that makes it impossible to execute GC files or other system resources?

+3


source to share


1 answer


As jfriend00 said, the fact that file descriptors are just numbers in a node creates a problem. The file descriptor may have been copied and there is no way to track this.

In addition, file descriptors from the main process can be shared by children (see module cluster

), which means that even if the process can determine that there are no longer references to a particular file descriptor, another process can have a copy of it.



It is possible that the requirement for file descriptors to be shared in this way caused the file descriptor to make numerical decisions, since it was not possible to pass an object with invisible internals between processes.

+1


source







All Articles