Fixed deadlock that can happen when running as an XPC service (fixes #2412)