I'm afraid that what you have shared is too specific to your environments and data sources, that I can not simply attempt to reproduce on my setup, so I will leave to someone else to come along. It may be that someone had the same idea as you and had the same error , and would recognise it, so I think there is a chance you might get help.
Otherwise in your shoes, I would add statements to print status of key variables to the log through my app so I can detect the failure points. its not clear what function considers its .x empty, and has no .init for example.