From bc17ead6b208be70ba40977b4a8d61ab3e5a8e12 Mon Sep 17 00:00:00 2001 From: Nithin Tatikonda Date: Tue, 24 Feb 2026 09:20:05 -0800 Subject: [PATCH] Internal PiperOrigin-RevId: 874646637 --- grain/_src/python/dataset/transformations/prefetch.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/grain/_src/python/dataset/transformations/prefetch.py b/grain/_src/python/dataset/transformations/prefetch.py index d2041de6d..46a756eab 100644 --- a/grain/_src/python/dataset/transformations/prefetch.py +++ b/grain/_src/python/dataset/transformations/prefetch.py @@ -21,6 +21,7 @@ import functools from multiprocessing import queues import queue +import sys import threading import typing from typing import Any, Optional, Protocol, TypeVar @@ -584,7 +585,12 @@ def _stop_prefetch(self): # Remove entries from the buffer to unblock the producer, so that it checks # producer_running.is_set() and exits. self._clear_buffer() - self._prefetch_thread.join() + if not sys.is_finalizing(): + # Joining the worker thread is not necessary when the Python interpreter + # is shutting down. Attempting to join can lead to hanging in Python + # 3.13 as daemon threads can hang during interpreter shutdown. See + # https://github.com/python/cpython/issues/123940#issuecomment-2976446309 + self._prefetch_thread.join() self._prefetch_thread = None # Clear the buffer again in case the prefetch loop added more elements on # exit.