Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion grain/_src/python/dataset/transformations/prefetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down