From a22c5ad29788238852a21e1b89ac4cdc982d7e1b Mon Sep 17 00:00:00 2001 From: Nithin Tatikonda Date: Wed, 18 Feb 2026 18:37:20 -0800 Subject: [PATCH] Internal PiperOrigin-RevId: 872137706 --- 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 7ceb2a8b4..b1abcc0a1 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.