Automation not waiting for script to complete

I have an automation which sends a MQTT message, calls a script, and sends another MQTT message. The script has some delays between its actions (wich amount to a total of 5 seconds).
The problem is that the automation is not waiting for the script to finish, so it sends the second MQTT message while the script is still running.

I have made a custom component which replicates the work of the script, and the automation waits for the completion of the custom component. Unfortunately, I can’t seem to find a way to stop/cancel a custom component :frowning:

Is this expected?