Logging calls in NodeJS via ARI

I’m trying to keep track of ongoing calls in my NodeJS app using ARI (specifically, the node-ari-client module). So, I’m using Stasis to log the call (in Node) when it is answered and then using Stasis again in the hangup handler to unlog the call (in Node) when it hangs up.

The relevant pieces of my dialplan look like:

same => n,Dial(${OUTBOUND_ENDPOINT},${DIAL_TIMEOUT},U(logOutboundCall^${CALLERID(number)}^${ARG1}^${OUTBOUND_HOST}))

And

[logOutboundCall]
exten => s,1,NoOp(logOutboundCall)
same  =>   n,Set(CHANNEL(hangup_handler_push)=onHangupCall,s,1)
same  =>   n,Stasis(ari-connect,log_call,OUTBOUND,${ARG1},${ARG2},${ARG3})
same  =>   n,Return()

[onHangupCall]
exten => s,1,NoOp(onHangupCall)
same  =>   n,Set(CHANNEL(hangup_handler_wipe)=)
same  =>   n,Stasis(ari-connect,end_call,Normal)
same  =>   n,Return()

So, I’m using the U option in Dial to call the logOutboundCall subroutine when the called party answers. This actually works quite well, but I keep getting a NOTICE from Asterisk about an abnormal Gosub exit. Here’s a snippet from the verbose log:

[2021-07-09 12:58:40] VERBOSE[500][C-00111923] app_stack.c: PJSIP/Outbound-A-0031d326 Internal Gosub(logOutboundCall,s,1(2095666491,2095574847,Outbound-A)) start
[2021-07-09 12:58:40] VERBOSE[500][C-00111923] pbx.c: Executing [s@logOutboundCall:1] NoOp("PJSIP/Outbound-A-0031d326", "logOutboundCall") in new stack
[2021-07-09 12:58:40] VERBOSE[500][C-00111923] pbx.c: Executing [s@logOutboundCall:2] Set("PJSIP/Outbound-A-0031d326", "CHANNEL(hangup_handler_push)=onHangupCall,s,1") in new stack
[2021-07-09 12:58:40] VERBOSE[500][C-00111923] pbx.c: Executing [s@logOutboundCall:3] Stasis("PJSIP/Outbound-A-0031d326", "ari-connect,log_call,OUTBOUND,2095666491,2095574847,Outbound-A") in new stack
[2021-07-09 12:58:40] VERBOSE[500][C-00111923] pbx.c: Executing [s@logOutboundCall:3] Stasis("PJSIP/Outbound-A-0031d326", "ari-connect,log_call,OUTBOUND,2095666491,2095574847,Outbound-A") in new stack
[2021-07-09 12:58:40] VERBOSE[500][C-00111923] pbx.c: Executing [s@logOutboundCall:4] Return("PJSIP/Outbound-A-0031d326", "") in new stack
[2021-07-09 12:58:40] VERBOSE[500][C-00111923] pbx.c: Spawn extension (all-utilities, , 1) exited non-zero on 'PJSIP/Outbound-A-0031d326'
[2021-07-09 12:58:40] NOTICE[500][C-00111923] app_stack.c: PJSIP/Outbound-A-0031d326 Abnormal 'Gosub(logOutboundCall,s,1(2095666491,2095574847,Outbound-A))' exit.  Popping routine return locations.

Apparently, something is getting confused when I call channel.continueInDialplan() from my node app. The verbose log shows the Stasis line is executed twice (but it isn’t). Then I get the notice about the abnormal exit.

And then I think there is another related issue because we recently got an error message that said:
CK!, Failed assertion Excessive refcount 100000 reached on ao2 object 0x1857ca8 (0)

I’m guessing that whatever is messing up in the Gosub return is causing some reference counter to not be decremented.

Everything seems to be working fine, and we aren’t leaking memory, but I’m nervous about what is going to happen when that refcount gets too high and wraps around.

Any recommendations? Is there a better way I should be logging calls in my Node app?