MIDI export fails on unknown Lua error

I’m trying to export my song to .mid, but it fails like this:

When I try an older song it works. Any idea how to fix this?

EDIT: This probably has something to do with MIDI control messages (M0 & M2) that I use before the song starts, but there’s nothing special. Just patch changes and channel volumes etc. But if I delete this “setup pattern” the export works. However, this same approach has worked before without problems so I’m not sure what exactly causes the crash.

Seems to crash on these lines:

Still doesn’t make any sense as I have similar settings on multiple other channels. If I remove those control messages on this specific instrument/channel the export will succeed. I don’t understand what’s so special on exactly those lines. I guess they trigger some indexing bug in the export script.

EDIT: Seems that if I set any control messages at all for this specific track the export crashes. I tried to make it not the first track, changed the instrument number, changed the message id’s etc, but no luck. Wtf.