isaacs / node-graceful-fs

fs with incremental backoff on EMFILE
ISC License
1.27k stars 147 forks source link

copyFile can fail with EMFILE #208

Closed mbargiel closed 3 years ago

mbargiel commented 3 years ago

The copyFile wrapper implemented in #199 has a small flaw. Unlike the other wrappers, the enqueue function of copyFile https://github.com/isaacs/node-graceful-fs/pull/199/files#diff-05bab169a94002226e0f82bf51c510b4fd2c6cd53f5d558c1ea4136c24a0d7c8R177-R179 enqueues the wrapped function (fs$copyFile) rather than the wrapper which implements the enqueue + retry logic (go$copyFile).

I hit the following issue on v4.2.6. An EMFILE error occurred on a copyFile; then on the retry attempt, another EMFILE error occurred, but that one bubbled up.

The copyFile function needs to be slightly modified to follow the same pattern as the other functions.

I can try to open a PR soon, but basically the following snippet (https://github.com/isaacs/node-graceful-fs/pull/199/files#diff-05bab169a94002226e0f82bf51c510b4fd2c6cd53f5d558c1ea4136c24a0d7c8R177-R179) needs to be changed:

    return fs$copyFile(src, dest, function (err) {
      if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
        enqueue([fs$copyFile, [src, dest, cb]])

should be

    return go$copyFile(src, dest, cb)

    function go$copyFile(src, dest, cb) {
      return fs$copyFile(src, dest, function (err) {
        if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
          enqueue([go$copyFile, [src, dest, cb]])