Skip to content

Commit

Permalink
fix(session): force detach links in all possible active states
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Jul 14, 2016
1 parent 517b3b8 commit 3a7272d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
7 changes: 6 additions & 1 deletion lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ Link.prototype.detach = function() {
/// Important bit is that this should not trigger auto-reattach behavior as that'll happen with reconnect.
///
Link.prototype.forceDetach = function() {
debug('Force-detach for ' + this.name + '. Current state: ' + this.state());
var state = this.state();
if (state !== 'attached' && state !== 'attaching' && state !== 'reattaching') {
return;
}

debug('force detach for ' + this.name + '. current state: ' + state);
if (!!this._reattachTimer) clearTimeout(this._reattachTimer);
this.linkSM.forceDetach();
};
Expand Down
13 changes: 2 additions & 11 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,7 @@ Session.prototype._unmap = function() {

debug('Session unmapped - force-detaching all links.');
// force-detach all links (they've already been detached due to unmap, just need to let them know about it)
var detachHandler = function(l) {
if (l.state() === 'attached') l.forceDetach();
};
var detachHandler = function(l) { l.forceDetach(); };
u.values(this._senderLinks).forEach(detachHandler);
u.values(this._receiverLinks).forEach(detachHandler);
this._linksByRemoteHandle = {};
Expand Down Expand Up @@ -475,14 +473,7 @@ Session.prototype._processDispositionFrame = function(frame) {
* Resets the state of all known links for this session.
*/
Session.prototype._resetLinkState = function() {
var forceDetachLink = function(l) {
var state = l.state();
if (state === 'attached' || state === 'attaching' ||
state === 'reattaching') {
l.forceDetach();
}
};

var forceDetachLink = function(l) { l.forceDetach(); };
u.values(this._senderLinks).forEach(forceDetachLink);
u.values(this._receiverLinks).forEach(forceDetachLink);
};
Expand Down
38 changes: 38 additions & 0 deletions test/unit/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,5 +459,43 @@ describe('Client', function() {
return test.client.connect(test.server.address())
.then(function() { return test.client.createReceiver('testing'); });
});


it('should not reattach after session unmapped and connection closed (issue #237)', function() {
test.server.setResponseSequence([
constants.amqpVersion,
new frames.OpenFrame(test.client.policy.connect.options),
new frames.BeginFrame({
remoteChannel: 1, nextOutgoingId: 0,
incomingWindow: 2147483647, outgoingWindow: 2147483647,
handleMax: 4294967295
}),
function (prev) {
var rxAttach = frames.readFrame(prev[prev.length-1]);
return new frames.AttachFrame({
name: rxAttach.name, handle: 1,
role: constants.linkRole.sender,
source: {}, target: {},
initialDeliveryCount: 0
});
},
[ // force detach from remote server, and force close of the connection
new frames.DetachFrame({ handle: 1, closed: true }),
new frames.EndFrame(),
new frames.CloseFrame()
]
]);

return test.client.connect(test.server.address())
.then(function() {
var $terminate = test.client._connection._terminate.bind(test.client._connection);
test.client._connection._terminate = function() {
setTimeout(function() { $terminate(); }, 500);
};
})
.then(function() { return test.client.createReceiver('testing'); })
.delay(500);
});

});
});

0 comments on commit 3a7272d

Please sign in to comment.