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

Still receiving lines after calling unwatch #102

Closed
BTOdell opened this issue Jun 3, 2020 · 6 comments
Closed

Still receiving lines after calling unwatch #102

BTOdell opened this issue Jun 3, 2020 · 6 comments

Comments

@BTOdell
Copy link

BTOdell commented Jun 3, 2020

I'm using chokidar to watch for add and unlink events for certain file paths and initializing a new Tail instance when a file is added and unwatching it when the file is unlinked.
However, despite unwatching the Tail in the unlink handler, the Tail line callback is still being invoked once the file is re-added to the file system. I end up having multiple instances of Tail still watching the file.

const watcher = chokidar.watch(watchDirectoryPath, {
    persistent: true,
    depth: 0
});
let tail = undefined;
// Start watching files
watcher.on("add", (filePath: string) => {
    if (tail != null) {
        tail.unwatch();
    }
    const newTail = new Tail(filePath);
    newTail.on("line", (line: string) => {
        if (line.length <= 0) {
            return;
        }
        if (tail !== newTail) {
            console.error("inactive tail is still producing lines!");
        }
        console.log(line);
    });
    newTail.on("error", () => {
        // Suppress error
    });
    tail = newTail;
});
// Stop watching files
watcher.on("unlink", (filePath: string) => {
    if (tail != null) {
        tail.unwatch();
    }
});

Note: This is a very simple version of the code that I'm actually running.
If a file is added, written to, removed, then re-added and written to again, the second time the lines will be duplicated along with error messages saying "inactive tail is still producing lines!" from my explicit check in the line callback.

@lucagrulla
Copy link
Owner

Hi @BTOdell
I can't replicate the issue with your code sample.
I also checked the library for misanaged variable references, but nothing stands out as a bug.

I would encourage you to review your code and check for mismanaged references.

@BTOdell
Copy link
Author

BTOdell commented Jun 8, 2020

I'll try to reproduce the problem in a separate repository and link it here.

@BTOdell
Copy link
Author

BTOdell commented Jun 9, 2020

I've created this repository that I have used to successfully reproduce the error:
https://github.com/BTOdell/tail-unwatch-bug

The producer is in Java and the consumer is in Node.js (TypeScript). This closely matches the environments that I was using when the issue was first observed.

I enabled logging in your library which helped me find the bug. The "follow" feature is enabled by default and the way you have it implemented is causing the library to start watching again (after 1 second) even if unwatch is manually called by the end user:
https://github.com/lucagrulla/node-tail/blob/master/src/tail.coffee#L107
setTimeout returns a handle which should be cancelled using clearTimeout if the end user manually calls unwatch.

@lucagrulla
Copy link
Owner

Yes,
I can see that now.
Great spot!

You were having a rename event then?

@BTOdell
Copy link
Author

BTOdell commented Jun 9, 2020

I actually don't know why a rename event was triggering because I'm not renaming files, only adding and removing them continuously. Although I've read that the built-in fs.watch and fs.watchFile have issues with producing incorrect events. That's the whole reason why chokidar exists: https://github.com/paulmillr/chokidar/blob/master/README.md#why

@lucagrulla
Copy link
Owner

fix available in v2.0.4.

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