question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Still receiving lines after calling unwatch

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lucagrullacommented, Jun 20, 2020

fix available in v2.0.4.

0reactions
BTOdellcommented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

ruby on rails - Redis gem watch and unwatch confusion
So there is indeed a reason to use UNWATCH. If the multi block is not called then EXEC is not called either. Then...
Read more >
Everything You Need to Know About Receiving Lines - Brides
Receiving lines are an incredibly useful way to make sure you have a chance to greet all of your wedding guests, especially at...
Read more >
In certain circumstances, the Stop Watching functionality ... - Jira
In certain circumstances, the Stop Watching functionality does not work correctly when unwatching issues reported by you while Autowatch is enabled.
Read more >
Ability to disable/not trigger watch handler on data? #1829
$watch return a handler to unwatch function, which I can call it when I want to stop watch, after that I watch the...
Read more >
Etiquette Q&A: "Do We Need to Have a Receiving Line?"
Consider having a smaller receiving line after the ceremony: The traditional line, which includes parents and attendants, can be shortened to just the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found