StaticLayer.GetConvertor().addShutdownHook() is what you need here.