Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] it crashed when tar a changing file #238

Closed
ww8912188 opened this issue Nov 20, 2019 · 3 comments
Closed

[BUG] it crashed when tar a changing file #238

ww8912188 opened this issue Nov 20, 2019 · 3 comments

Comments

@ww8912188
Copy link

node-tar will crash when tar a file which is changing rapidly.

Steps to Reproduce

  1. generate a file which is changing rapidly:
for i in {1..1000000} ;
do 
  echo $i >> ./ou 2>&1
done
  1. tar the file:
const tar = require('tar');

let file = './test.tar.gz'
let cwd = './createAccounts'
let fileList = ['./ou']

try {
  tar.create({
    gzip: false,
    file,
    cwd,
  }, fileList).then(_ => console.log('done')).catch(err => console.log(err))
} catch (err) {
  console.log(err)
}

Node will crashed and raise below error:

{ Error: did not encounter expected EOF
    at Object.[onread] (/Users/nan/code/backend/node_modules/tar/lib/write-entry.js:239:18)
    at fs.read (/Users/nan/code/backend/node_modules/tar/lib/write-entry.js:220:19)
    at FSReqWrap.wrapper [as oncomplete] (fs.js:658:17)
  path: '/Users/nan/code/backend/createAccounts/ou',
  syscall: 'read',
  code: 'EOF' }

Expected Behavior

Anyway it should not crash node.

version info:

node v8.11.1
node-tar 4.4.8

@isaacs
Copy link
Owner

isaacs commented Nov 20, 2019

Well, it should raise an error and fail the archive process, but that error should be caught by the catch method you're providing. Not sure why the error event isn't being handled properly, that's a bug.

@isaacs
Copy link
Owner

isaacs commented Nov 20, 2019

I think the bug here was already fixed.

Using this script:

const {spawn} = require('child_process')

const c = spawn('bash', ['-c', `
echo 'starting' > ./ou
for i in {1..10000} ; do
  echo $i >> ./ou
  echo 'going'
done
`])

c.stdout.once('data', () => {
  const tar = require('./');

  const file = './test.tar.gz'
  const fileList = ['./ou']

  try {
    tar.create({
      gzip: false,
      file,
    }, fileList)
      .then(_ => console.log('done'))
      .catch(err => console.log('CAUGHT', err))
  } catch (err) {
    console.log('THROWN', err)
  }
})

I get the following output on 4.4.8

fs.js:143
    throw new ERR_INVALID_CALLBACK(cb);
    ^

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
    at makeCallback (fs.js:143:11)
    at Object.close (fs.js:393:20)
    at Object.[close] (/Users/isaacs/dev/js/tar/lib/write-entry.js:225:8)
    at Object.[onread] (/Users/isaacs/dev/js/tar/lib/write-entry.js:243:18)
    at /Users/isaacs/dev/js/tar/lib/write-entry.js:220:19
    at FSReqCallback.wrapper [as oncomplete] (fs.js:470:5) {
  code: 'ERR_INVALID_CALLBACK'
}

But on 4.4.13, I get this:

$ node archive-changing-file.js
CAUGHT Error: did not encounter expected EOF
    at Object.[onread] (/Users/isaacs/dev/js/tar/lib/write-entry.js:239:18)
    at /Users/isaacs/dev/js/tar/lib/write-entry.js:220:19
    at FSReqCallback.wrapper [as oncomplete] (fs.js:470:5) {
  path: '/Users/isaacs/dev/js/tar/ou',
  syscall: 'read',
  code: 'EOF'
}

and on the latest master (v5.0.5) I get this:

$ node archive-changing-file.js
CAUGHT Error: did not encounter expected EOF
    at Object.[onread] (/Users/isaacs/dev/js/tar/lib/write-entry.js:248:18)
    at /Users/isaacs/dev/js/tar/lib/write-entry.js:230:19
    at FSReqCallback.wrapper [as oncomplete] (fs.js:470:5) {
  path: '/Users/isaacs/dev/js/tar/ou',
  syscall: 'read',
  code: 'EOF'
}

Bisect says that the changed behavior started at this commit:

commit d04c3ffb41a0d2bbae926a38d3456ebda0249565
Author: isaacs <i@izs.me>
Date:   Tue Sep 24 21:55:39 2019 -0700

    Always provide a callback to fs.close()

    Fix #231

 lib/write-entry.js | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Moral of the story, upgrade to at least 4.4.12 (if not all the way to latest, v5.0.5), and always put a callback on your fs.close calls.

@isaacs isaacs closed this as completed Nov 20, 2019
@ww8912188
Copy link
Author

It works smoothly after I upgrade tar version to v5.0.5. Thanks for your quick response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants