Avoid race condition between domain unload and threadpool. (#3491)