Fixed #19510 -- Race condition in template loading.

Thanks Kronuz and regebro.