Moving mutex lock to prevent hangs on ctrl-c events when layer is running. The locks should be reveiwed at some point in the future