Note that there are some explanatory texts on larger screens.

plurals
  1. POChild process in node.js mysteriously exiting between lines of synchronous code
    text
    copied!<p>I'm having a weird problem with child process in node.js I have some code running in a child process like this, but the child process exits with code 0 <strong>in between lines of synchronous code.</strong> </p> <pre><code>var fs = require('fs'); var http = require('http'); var mkdirp = require('mkdirp'); var urlPath = __dirname + "\\urls.txt"; var savePath = __dirname + "\\"; var objects = []; var globalI = 0; var file; if (!fs.existsSync(urlPath)) { console.log("File at " + urlPath + " does not exist!"); } else { if(!fs.existsSync(savePath)){ mkdirp.sync(savePath); } console.log("File found! Reading..."); console.log("still running"); try{ var data = fs.readFileSync(urlPath, {"encoding":"utf8"}); } catch (err) { console.log("Error reading url file..."); throw err; } finally { console.log("File read!"); var array = data.split("\n"); console.log("Found " + array.length + " urls"); } </code></pre> <p>The line</p> <pre><code>console.log("File found! Reading..."); </code></pre> <p>Runs and appears in the console. However the next line</p> <pre><code>console.log("still running"); </code></pre> <p>does not run. The child process exits before that line of code. I have absolutely no idea why. Any insight would be tremendously appreciated!</p> <p>Also, if I change the order of the two statements, it still only executes the first before exiting.</p> <p><strong>EDIT</strong></p> <p>So maybe it does have to do with flushing and that other bug. If I remove both of those consecutive log statements, it runs the next log statement in the <code>finally</code> block and then quits.</p> <p><strong>EDIT2</strong></p> <p>There is also something else curious about this problem. You can see in the code snippet I have a variable called <code>globalI</code> Later on in the code, that variable gets incremented simply like <code>globalI++;</code> <strong>If I uncomment the incrementation, the child process stops exiting unexpectedly</strong> But it makes no sense, because it never even comes close to the line where the incrementation happens when it does exit unexpectedly.</p> <p>That is actually how I started having this problem. I am completely flabbergasted</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload