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

Error on rotated file if new file takes time to appear #18

Closed
martinj opened this issue May 30, 2021 · 1 comment · Fixed by #22
Closed

Error on rotated file if new file takes time to appear #18

martinj opened this issue May 30, 2021 · 1 comment · Fixed by #22

Comments

@martinj
Copy link

martinj commented May 30, 2021

If the file is rotated and it takes a while for the new file to appear it will fail.

The first error ENOENT will be caught and _readRemainderFromFileHandle will be executed, this will set this[kFileHandle] = null and on the next succeding poll it will detect a file rename and call _readRemainderFromFileHandle again but this[kFileHandle] is now null.

Example

'use strict'

const TailFile = require('@logdna/tail-file');
const fs = require('fs');

async function go() {
	const tail = new TailFile('./foo.log');
	tail.on('retry', () => console.log('retry'));
	tail.on('error', console.log)

	await tail.start();

	setTimeout(() => {
		fs.renameSync('./foo.log', './foo.log.1');
	}, 1000);

	setTimeout(() => {
		fs.copyFileSync('./data.txt', './foo.log');
	}, 2100);

}

go().catch(console.log);

Output

retry
TypeError: Cannot read property 'stat' of null
    at TailFile._readRemainderFromFileHandle (/node_modules/@logdna/tail-file/lib/tail-file.js:123:43)
    at TailFile._pollFileForChanges (/node_modules/@logdna/tail-file/lib/tail-file.js:171:20)
@darinspivey
Copy link
Contributor

Ah, right you are! Thank you for reporting this, and for providing a working example. Looks like this edge case was just missed. There are 2 places that will read 'the remainder' from the FH; one for when the inode changes and one for if the file disappears. The former condition can happen if the file changes in between polls, thus never throwing an ENOENT error. This edge case is sort of a combination of the two and should definitely be handled. After all, the code is designed to be configurable for wait time and number of retries after a file disappears.

I've completed the coding fix, but need to update the tests. This can be merged asap after the US is back from the holiday weekend in a few days. Thanks again.

darinspivey added a commit that referenced this issue Jun 1, 2021
When a file's inode changes, or the `stat` call throws
causes tail-file to read the remainder of the contents
via a FileHandle. If the file disappears, but takes a bit
to re-appear, `_readRemainderFromFileHandle()` is
erroneously called twice; once for the `ENOENT` from the
disappearing file, and once when the new file causes the
inode to change. This commit fixes a bug where the FH is
nullified after the first call, and thus throws on the
subsequent call.

Semver: patch
Fixes: #18
darinspivey added a commit that referenced this issue Jun 1, 2021
When a file's inode changes, or the `stat` call throws
causes tail-file to read the remainder of the contents
via a FileHandle. If the file disappears, but takes a bit
to re-appear, `_readRemainderFromFileHandle()` is
erroneously called twice; once for the `ENOENT` from the
disappearing file, and once when the new file causes the
inode to change. This commit fixes a bug where the FH is
nullified after the first call, and thus throws on the
subsequent call.

Semver: patch
Fixes: #18
darinspivey added a commit that referenced this issue Jun 1, 2021
When a file's inode changes, or the `stat` call throws
causes tail-file to read the remainder of the contents
via a FileHandle. If the file disappears, but takes a bit
to re-appear, `_readRemainderFromFileHandle()` is
erroneously called twice; once for the `ENOENT` from the
disappearing file, and once when the new file causes the
inode to change. This commit fixes a bug where the FH is
nullified after the first call, and thus throws on the
subsequent call.

Semver: patch
Fixes: #18
darinspivey added a commit that referenced this issue Jun 1, 2021
When a file's inode changes, or the `stat` call throws
causes tail-file to read the remainder of the contents
via a FileHandle. If the file disappears, but takes a bit
to re-appear, `_readRemainderFromFileHandle()` is
erroneously called twice; once for the `ENOENT` from the
disappearing file, and once when the new file causes the
inode to change. This commit fixes a bug where the FH is
nullified after the first call, and thus throws on the
subsequent call.

Semver: patch
Fixes: #18
darinspivey added a commit that referenced this issue Jun 1, 2021
When a file's inode changes, or the `stat` call throws
causes tail-file to read the remainder of the contents
via a FileHandle. If the file disappears, but takes a bit
to re-appear, `_readRemainderFromFileHandle()` is
erroneously called twice; once for the `ENOENT` from the
disappearing file, and once when the new file causes the
inode to change. This commit fixes a bug where the FH is
nullified after the first call, and thus throws on the
subsequent call.

Semver: patch
Fixes: #18
darinspivey added a commit that referenced this issue Jun 1, 2021
When a file's inode changes, or the `stat` call throws
causes tail-file to read the remainder of the contents
via a FileHandle. If the file disappears, but takes a bit
to re-appear, `_readRemainderFromFileHandle()` is
erroneously called twice; once for the `ENOENT` from the
disappearing file, and once when the new file causes the
inode to change. This commit fixes a bug where the FH is
nullified after the first call, and thus throws on the
subsequent call.

Semver: patch
Fixes: #18
darinspivey added a commit that referenced this issue Jun 2, 2021
When a file's inode changes, or the `stat` call throws
causes tail-file to read the remainder of the contents
via a FileHandle. If the file disappears, but takes a bit
to re-appear, `_readRemainderFromFileHandle()` is
erroneously called twice; once for the `ENOENT` from the
disappearing file, and once when the new file causes the
inode to change. This commit fixes a bug where the FH is
nullified after the first call, and thus throws on the
subsequent call.

Semver: patch
Fixes: #18
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

Successfully merging a pull request may close this issue.

2 participants