Fixed a random crash in layer shutdowns.