Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit 7827b91

Browse files
authored
Merge pull request share#261 from dcharbonnier/sharedb/memory-leak
fix leak on duplicate subscription - share#260
2 parents 88bb142 + 713a8ff commit 7827b91

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/agent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Agent.prototype._subscribeToStream = function(collection, id, stream) {
105105
stream.on('end', function() {
106106
// The op stream is done sending, so release its reference
107107
var streams = agent.subscribedDocs[collection];
108-
if (!streams) return;
108+
if (!streams || streams[id] !== stream) return;
109109
delete streams[id];
110110
if (util.hasKeys(streams)) return;
111111
delete agent.subscribedDocs[collection];

test/client/connection.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ describe('client connection', function() {
5656
});
5757
});
5858

59+
it('subscribing to same doc closes old stream and adds new stream to agent', function(done) {
60+
var connection = this.backend.connect();
61+
var agent = connection.agent;
62+
var collection = 'test';
63+
var docId = 'abcd-1234';
64+
var doc = connection.get(collection, docId);
65+
doc.subscribe(function(err) {
66+
if (err) return done(err);
67+
var originalStream = agent.subscribedDocs[collection][docId];
68+
doc.subscribe(function() {
69+
if (err) return done(err);
70+
expect(originalStream).to.have.property('open', false);
71+
var newStream = agent.subscribedDocs[collection][docId];
72+
expect(newStream).to.have.property('open', true);
73+
expect(newStream).to.not.be(originalStream);
74+
connection.close();
75+
done();
76+
});
77+
});
78+
});
79+
5980
it('emits socket errors as "connection error" events', function(done) {
6081
var connection = this.backend.connect();
6182
connection.on('connection error', function(err) {

0 commit comments

Comments
 (0)