iriscouch / follow

Very stable, very reliable, NodeJS CouchDB _changes follower
Apache License 2.0
393 stars 82 forks source link

Feed that starts at "now" cannot be stopped #46

Open atomatt opened 10 years ago

atomatt commented 10 years ago

I want to stop the follow feed once it has caught up with the database. Unfortunately, if the feed's since is the db's current seq then a _changes request is left hanging and the JS process runs indefinitely.

Here's example code that demonstrates the issue:

var follow = require("follow");

var db = process.argv[2];
var since = process.argv.length > 3 ? parseInt(process.argv[3]) : "now";

var feed = new follow.Feed({db: db, since: since});
feed.on("change", function(change) {
    console.log("change:", change.seq);
});
feed.on("catchup", function() {
    console.log("catchup");
    feed.stop();
});
feed.on("stop", function(err) {
    console.log("stop:", err);
    setTimeout(function() {
        var handles = process._getActiveHandles();
        handles.forEach(function(handle) {
            console.log("******************************");
            console.log(handle);
        });
        console.log(handles.length);
    }, 1000);
});
feed.follow();

The JS process will hang unless a value for since is provided that's older that the db's current seq. When it hangs the request to the _changes feed is visible in the dump of active handles (the timeout just gives things a bit of time to settle down).