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

Fix duplicate watchers from being added #19

Closed
wants to merge 1 commit into from

Conversation

sudowork
Copy link

Based on the existing code for WatcherManager.js, it seems like adding a duplicate watcher should not be allowed. However, the current check for watchers[path].listeners('notification').indexOf(watcher) === -1 will never be false. This is because EventEmitter ends up wrapping the listener before adding it if once() was used.

Here's a contrived test based on the examples/get.js example:

var zookeeper = require('./index.js');
var client = zookeeper.createClient(process.argv[2], { retries : 2 });
var path = process.argv[3];

function watcher(event) {
    console.log('Fire me once: %s', event);
    getData(client, path);
}

function getData(client, path) {
    client.getData(
        path,
        watcher,
        function (error, data, stat) {
            if (error) {
                console.log('Error occurred when getting data: %s.', error);
                return;
            }

            console.log(
                'Node: %s has data: %s, version: %d',
                path,
                data ? data.toString() : undefined,
                stat.version
            );
        }
    );
}

client.once('connected', function () {
    console.log('Connected to ZooKeeper.');
    getData(client, path);
    // @NOTE: this should not register a second watcher
    getData(client, path);
});

client.connect();

Output before the patch:

$ node test.js localhost:2181 /test
Connected to ZooKeeper.
Node: /test has data: hi, version: 5
Node: /test has data: hi, version: 5
Fire me once: NODE_DATA_CHANGED[3]@/test
Fire me once: NODE_DATA_CHANGED[3]@/test
Node: /test has data: hi, version: 6
Node: /test has data: hi, version: 6

Output after the patch:

$ node test.js localhost:2181 /test
Connected to ZooKeeper.
Node: /test has data: hi, version: 6
Node: /test has data: hi, version: 6
Fire me once: NODE_DATA_CHANGED[3]@/test
Node: /test has data: hi, version: 7

@alexguan
Copy link
Owner

Thanks for the PR, I will take a look this weekend and merge it in manually since I need to bump the version for the fix.

@alexguan alexguan closed this in 71431c4 Apr 1, 2014
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 this pull request may close these issues.

2 participants