Race conditions when loading multiple modules simultaneously

Skip to first unread message

Christofer Dutz

Aug 25, 2020, 11:34:15 AM8/25/20
to Closure Library Discuss
Hi all,

I'm currently trying to solve some issues I'm having with Apache Royale, which uses closure underneath and the issue I tracked down is in the closure part of the library, so I'm asking here. Currently working on the debug output ... release will be the next on my list ;-)

So I have an application which uses some framework types. This application loads multiple modules. These share some types used by the main application, share some with other modules and use some of their own types.

The first als the last are not the problem. It however starts to get tricky if I load two modules at the same time (or at least almost at the same time). From what I see here is that the first module that gets returned starts listing dependencies and their locations. Then the module is initialized by calling goog.require ... the type is not loaded yet, so the library checks the dependency list and adds this to the missing resources list ... same with any of the dependencies of this type. So in the end it's a pretty long list of stuff to get from the server. So the library fetches this and the next module starts doing the same. 

Unfortunately the requested resources haven't been returned from the server yet and therefore they didn't have the chance to register themselves. So the second module potentially requests some resources again. As the url of the resources of the second module differs, the browser also tries to fetch them. 

Now as soon as the first set of resources come back, they register themselves and all is good. But as soon as the second batch comes, they try to re-register, which causes errors. 

My question now is: how can I avoid this problem? I was thinking of also maintaining a list of requested types so if it's not registered yet but it has already been requested, to simply start waiting for the other request to return.

I created an issue in the Royale project which has some more information: 

Would greatly appreciate any suggestions ... queueing the modules to start loading the next one as soon as the first is done doesn't seem to be an ideal solution.


Stephen Hicks

Oct 29, 2020, 7:16:13 PM10/29/20
to Closure Library Discuss
(Sorry for the slow reply).  Ouch, that does sound like something that I'd expect to cause problems.  Unsatisfyingly, my general answer is that goog.require() is not intended to be used as a production module loader (in particular, it's not designed to be externally reentrant over the course of the load), so this is a bit out of scope for its use case as a "debug loader".  If you need a production module loader, goog.module and goog.loader should fit that bill better.  Closure Compiler can be run in a mode where it will split out separate chunks that can be loaded lazily.
Reply all
Reply to author
0 new messages