The error was resulting from my function, that was not a callback, so no possibility to check for events there, but the location that the function was being called in other callbacks was out of place as you speculated. Fixing that, it seems to work.
It is curious that only some of the library functions in my function resulted in exceptions.