Skip to content

Commit 0539462

Browse files
ssnlfacebook-github-bot
authored andcommitted
Fix pin_memory_thread not exiting quickly (#23646)
Summary: fixes #23642 Pull Request resolved: #23646 Differential Revision: D16600874 Pulled By: soumith fbshipit-source-id: 50f0828d774a558d6f21e9dd21135906bd5be128
1 parent 3b5daef commit 0539462

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

torch/utils/data/_utils/pin_memory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def _pin_memory_loop(in_queue, out_queue, device_id, done_event):
2222
except queue.Empty:
2323
continue
2424
idx, data = r
25-
if not isinstance(data, ExceptionWrapper):
25+
if not done_event.is_set() and not isinstance(data, ExceptionWrapper):
2626
try:
2727
data = pin_memory(data)
2828
except Exception:

torch/utils/data/dataloader.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,13 @@ def _shutdown_workers(self):
886886
# corrupted data in `worker_result_queue` which `pin_memory_thread`
887887
# reads from.
888888
if hasattr(self, 'pin_memory_thread'):
889-
self.pin_memory_thread_done_event.set()
890889
# Use hasattr in case error happens before we set the attribute.
890+
self.pin_memory_thread_done_event.set()
891+
# Send something to pin_memory_thread in case it is waiting
892+
# so that it can wake up and check `pin_memory_thread_done_event`
893+
self.worker_result_queue.put((None, None))
891894
self.pin_memory_thread.join()
895+
self.worker_result_queue.close()
892896

893897
# Exit workers now.
894898
self.workers_done_event.set()

0 commit comments

Comments
 (0)