diff --git a/torch/_inductor/compile_worker/subproc_pool.py b/torch/_inductor/compile_worker/subproc_pool.py index f3f8e7b3b3ef3e..4260ae80e2abca 100644 --- a/torch/_inductor/compile_worker/subproc_pool.py +++ b/torch/_inductor/compile_worker/subproc_pool.py @@ -90,7 +90,6 @@ def __init__(self, nprocs: int): self.write_lock = threading.Lock() self.read_pipe: Pipe = typing.cast(Pipe, self.process.stdout) self.read_thread = threading.Thread(target=self._read_thread, daemon=True) - self.read_thread.start() self.futures_lock = threading.Lock() self.pending_futures: Dict[int, Future[Any]] = {} @@ -98,6 +97,10 @@ def __init__(self, nprocs: int): self.running = True + # Start thread last to ensure all member variables are initialized + # before any access. + self.read_thread.start() + def submit(self, job_fn: Callable[..., Any], *args): if args: job_fn = functools.partial(job_fn, *args)