It’s hard to tell what’s happening without the full context. If you don’t mind, it would be easier if you could send me the link to your experiment. One explanation could be that the MediaRecorder element is no longer active when the stop command on it tries to run, making the script crash before it can reach SendResults. Also, just to clarify, this early-exit bit of code is inside the Template command, right?
Re. the undefined trial getting stuck, do your sendrecords trials work as expected? That is, do they effectively upload the recordings?