Skip to content

Commit

Permalink
Merge pull request #261 from dcharbonnier/sharedb/memory-leak
Browse files Browse the repository at this point in the history
fix leak on duplicate subscription - #260
  • Loading branch information
ericyhwang committed Dec 1, 2018
2 parents 88bb142 + 713a8ff commit 7827b91
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Agent.prototype._subscribeToStream = function(collection, id, stream) {
stream.on('end', function() {
// The op stream is done sending, so release its reference
var streams = agent.subscribedDocs[collection];
if (!streams) return;
if (!streams || streams[id] !== stream) return;
delete streams[id];
if (util.hasKeys(streams)) return;
delete agent.subscribedDocs[collection];
Expand Down
21 changes: 21 additions & 0 deletions test/client/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ describe('client connection', function() {
});
});

it('subscribing to same doc closes old stream and adds new stream to agent', function(done) {
var connection = this.backend.connect();
var agent = connection.agent;
var collection = 'test';
var docId = 'abcd-1234';
var doc = connection.get(collection, docId);
doc.subscribe(function(err) {
if (err) return done(err);
var originalStream = agent.subscribedDocs[collection][docId];
doc.subscribe(function() {
if (err) return done(err);
expect(originalStream).to.have.property('open', false);
var newStream = agent.subscribedDocs[collection][docId];
expect(newStream).to.have.property('open', true);
expect(newStream).to.not.be(originalStream);
connection.close();
done();
});
});
});

it('emits socket errors as "connection error" events', function(done) {
var connection = this.backend.connect();
connection.on('connection error', function(err) {
Expand Down

0 comments on commit 7827b91

Please sign in to comment.