It's something in your code, try to reduce the existing code until you reach something more like the minimal example you've created. Along the way you should notice what makes the behavior change.
Or start with the minimal example and keep adding your code until you get a similar code base.
Took a look at the posted code, didn't see anything obvious, but the issue is probably in some code piece you don't expect it...