ts-node-dev: Infinite loop, 100% CPU

In ts-node-dev-hooks, the compile function runs this:

waitForFile(compiledPath + '.done')

which ends up in the function waitForFile. This runs an infinite loop as the file is never created. This is the block:

var waitForFile = function(fileName) {
  var start = new Date().getTime()
  while (true) {
    const exists = execCheck
      ? execSync(['node', checkFileScript, '"' + fileName + '"'].join(' '), {
          stdio: 'inherit'
        }) || true
      : fs.existsSync(fileName)

    if (exists) {
      return
    }
    var passed = new Date().getTime() - start
    if (timeThreshold && passed > timeThreshold) {
      throw new Error('Could not require ' + fileName)
    }
  }
}

exists is always false, execCheck is false. timeThreshold is 0

Unless I misunderstand the code, it also seems terribly “spinny”. Why don’t we use file watchers, or something like 100ms polling? We should also bail out after a timeout - e.g. no produced file within say 10 seconds or so. Why is timeThreshold 0?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 8
  • Comments: 19 (6 by maintainers)

Most upvoted comments

This is definitely still an issue, seeing it daily on our project using ts-node-dev and it’s always a git checkout that seems to trigger it.

@wclr I did some digging on this and the issue seems to be related to fork. I built a minimal repro: https://gist.github.com/LucasPickering/f1568762a2a774923f15a8f6b5781c87

If you clone that repo and do npm install, you should see these results:

npm run start

This runs with just ts-node, and the output is:

> ts-node-dev-74-repro@1.0.0 start /Users/lucas.pickering/MarkForged/ts-node-dev-74-repro
> ts-node index.ts

Starting process: /Users/lucas.pickering/MarkForged/ts-node-dev-74-repro/process.ts
Starting child process...

and it quickly exits, which is what we’d expect. With ts-node-dev though…

npm run start-dev

output looks like:

> ts-node-dev-74-repro@1.0.0 start-dev /Users/lucas.pickering/MarkForged/ts-node-dev-74-repro
> ts-node-dev --debug index.ts

Ignore watch: []
[INFO] 16:31:23 ts-node-dev ver. 1.1.1 (using ts-node ver. 9.1.1, typescript ver. 4.1.2)
[DEBUG] 16:31:23 Starting child process -r /var/folders/vg/tmnjzzr93pb3t8xq53v7fd5c0000gn/T/ts-node-dev-hook-6849730767871609.js /Users/lucas.pickering/MarkForged/ts-node-dev-74-repro/node_modules/ts-node-dev/lib/wrap.js index.ts
[DEBUG] 16:31:23 /Users/lucas.pickering/MarkForged/ts-node-dev-74-repro/index.ts added to watcher
[DEBUG] 16:31:24 /Users/lucas.pickering/MarkForged/ts-node-dev-74-repro/index.ts compiled in 811 ms
Starting process: /Users/lucas.pickering/MarkForged/ts-node-dev-74-repro/process.ts
[DEBUG] 16:31:24 /Users/lucas.pickering/MarkForged/ts-node-dev-74-repro/process.ts added to watcher

and then it just hangs. I tried downgrading TypeScript to 4.1.2 (and 4.0.0 for good measure) and still saw the issue, so I’m not sure how long this has been around. Would you mind taking another look at this? Please let me know if there’s any other info I can give that would be helpful.

I ran into this as well. For me it seemed to be related to node 10.16.0. Downgrading to 10.15.x resolved it.