diff --git a/lib/session/call-session.js b/lib/session/call-session.js index ac98340f..dba3d1c2 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -632,7 +632,7 @@ class CallSession extends Emitter { try { if (!this.ms) this.ms = this.getMS(); const ep = await this.ms.createEndpoint({remoteSdp: this.req.body}); - ep.cs = this; + //ep.cs = this; this.ep = ep; ep.set({ hangup_after_bridge: false, @@ -716,14 +716,15 @@ class CallSession extends Emitter { /** * Hang up the call and free the media endpoint */ - async _clearResources() { + _clearResources() { for (const resource of [this.dlg, this.ep]) { - try { - if (resource && resource.connected) await resource.destroy(); - } catch (err) { - this.logger.error(err, 'CallSession:_clearResources error'); + if (resource && resource.connected) { + resource.destroy() + .catch((err) => this.logger.error(err, 'CallSession:_clearResources error')); } } + this.dlg = null; + this.ep = null; // remove any temporary tts files that were created (audio is still cached in redis) for (const path of this.tmpFiles) { diff --git a/lib/session/inbound-call-session.js b/lib/session/inbound-call-session.js index 3581d2c1..e72ece47 100644 --- a/lib/session/inbound-call-session.js +++ b/lib/session/inbound-call-session.js @@ -21,15 +21,17 @@ class InboundCallSession extends CallSession { this.req = req; this.res = res; - req.on('cancel', () => { - this._notifyCallStatusChange({callStatus: CallStatus.NoAnswer, sipStatus: 487}); - this._callReleased(); - }); + req.once('cancel', this._onCancel.bind(this)); this.on('callStatusChange', this._notifyCallStatusChange.bind(this)); this._notifyCallStatusChange({callStatus: CallStatus.Trying, sipStatus: 100}); } + _onCancel() { + this._notifyCallStatusChange({callStatus: CallStatus.NoAnswer, sipStatus: 487}); + this._callReleased(); + } + _onTasksDone() { if (!this.res.finalResponseSent) { if (this._mediaServerFailure) { @@ -45,6 +47,7 @@ class InboundCallSession extends CallSession { this.res.send(603); } } + this.req.removeAllListeners('cancel'); } /** @@ -56,6 +59,7 @@ class InboundCallSession extends CallSession { this.emit('callStatusChange', {callStatus: CallStatus.Completed, duration}); this.logger.debug('InboundCallSession: caller hung up'); this._callReleased(); + this.req.removeAllListeners('cancel'); } } diff --git a/lib/tasks/dial.js b/lib/tasks/dial.js index 67f3ca82..0bef8dcb 100644 --- a/lib/tasks/dial.js +++ b/lib/tasks/dial.js @@ -448,6 +448,12 @@ class TaskDial extends Task { this._killOutdials(); // NB: order is important } + _onMaxCallDuration(cs) { + this.logger.info(`Dial:_onMaxCallDuration tearing down call as it has reached ${this.timeLimit}s`); + this.ep && this.ep.unbridge(); + this.kill(cs); + } + /** * We now have a call leg produced by the Dial action, so * - hangup any simrings in progress @@ -468,11 +474,7 @@ class TaskDial extends Task { await cs.propagateAnswer(); } if (this.timeLimit) { - this.timerMaxCallDuration = setTimeout(() => { - this.logger.info(`Dial:_selectSingleDial tearing down call as it has reached ${this.timeLimit}s`); - this.ep && this.ep.unbridge(); - this.kill(cs); - }, this.timeLimit * 1000); + this.timerMaxCallDuration = setTimeout(this._onMaxCallDuration.bind(this, cs), this.timeLimit * 1000); } sessionTracker.add(this.callSid, cs); this.dlg.on('destroy', () => {