{"id":2091,"date":"2022-08-30T15:21:58","date_gmt":"2022-08-30T15:21:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/25\/how-to-prevent-node-js-from-crashing-when-opening-too-many-files-at-once-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:21:58","modified_gmt":"2022-08-30T15:21:58","slug":"how-to-prevent-node-js-from-crashing-when-opening-too-many-files-at-once-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-prevent-node-js-from-crashing-when-opening-too-many-files-at-once-collection-of-common-programming-errors\/","title":{"rendered":"How to prevent node.js from crashing when opening too many files at once?-Collection of common programming errors"},"content":{"rendered":"<p>Not sure if this is the best way to solve this issue, but what I&#8217;ve effectively done is wrapped all the asynchronous fs functions with a wrapper that checks whether or not the we have more than the allowed amount of files open (arbitrarily set to a reasonable 100). If there is more than that many files open at the time of the call, the call will idle and check again after a delay to see if there is now an opening. The code for this logic can be found here: https:\/\/github.com\/balupton\/bal-util\/blob\/master\/src\/lib\/paths.coffee#L7-45<\/p>\n<p>In terms of implementing it, you just need to change <code>require('fs').readFile<\/code> or whatever call you are using to <code>require('bal-util').readFile<\/code> and install the bal-util dependency (<code>npm install bal-util<\/code>) which is simple enough.<\/p>\n<p>Hope this helps. If there is a more robust way of doing this, I&#8217;d love to know.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-12-25 10:58:29. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Not sure if this is the best way to solve this issue, but what I&#8217;ve effectively done is wrapped all the asynchronous fs functions with a wrapper that checks whether or not the we have more than the allowed amount of files open (arbitrarily set to a reasonable 100). If there is more than that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2091","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2091"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2091\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}