Hangup before calls are bridged during executing subroutine

Hello,

I have the following problem with Dial(). I need to dial a particular phone number and before the calls are bridged I want to execute a routine in the new call. I do something like this:
Dial “PJSIP/NUMBER@OUT_ENDPOINT,m(default)U(routine-extension)f(CALLER_NUMBER)”
I’m having problem with this edge case:

  1. The new call is answered
  2. the routine is getting executed
  3. the original call is hung up before the routine finishes

Afterwards, the original call is cancelled but the new call still keeps going because of the subroutine. The subroutine is playing the same message in a loop forever until the user on the other side does not press “1”. This means that if the user never presses 1 the call keeps going forever until the user won’t hang up.

The behaviour that I would like to achieve is that when the original call is hung up, the new call is also hung up. It’s kind of weird that Dial() does not do that but I think it is because of the subroutine. It tried to achieve this behaviour using AMI by sending “Hangup” to the new channel. Unfortunately, this is just going to stop the routine so the message won’t be played but it won’t hangup the call. I can see that asterisk stops playing the audio but it won’t send SIP BYE to end the call. Do you have an idea how to fix this?

Thanks.

What just don’t add a variable as increment counter to hang-up the call on the subroutine when the variable value is > X, you send call to the hangup() app

I’d prefer if I did not have to rely on an arbitrary constant. Any other idea?