diff --git a/lib/call-session.js b/lib/call-session.js index 7c2afbf..255aae4 100644 --- a/lib/call-session.js +++ b/lib/call-session.js @@ -665,7 +665,21 @@ class CallSession extends Emitter { } } } catch (err) { - if ('abandonded' !== err.message) this.logger.error({err}, `Error setting up outbound call to: ${uris}`); + if ('abandoned' !== err.message) this.logger.error({err}, `Error setting up outbound call to: ${uris}`); + + /* the call count was incremented on 'init' but we bailed out before connecting (e.g. caller + hung up before the B leg answered), so we must decrement it here - no failure response is + sent on this path, so the res.once('end') safety net in middleware would not fire. + nudgeCallCounts is a no-op if the count was already decremented elsewhere. */ + const {writeCallCount, writeCallCountSP, writeCallCountApp} = this.req.srf.locals; + nudgeCallCounts(this.req, 'failure', { + service_provider_sid: this.service_provider_sid, + account_sid: this.account_sid, + application_sid: this.application_sid, + callId: this.req.locals.callId + }, this.decrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}) + .catch((err) => this.logger.error(err, 'Error decrementing call counts')); + this.emit('failed'); this.srf.endSession(this.req); this.rtpEngineResource.destroy();