Skip to content

Commit

Permalink
Fix pin_memory_thread not exiting quickly (#23646)
Browse files Browse the repository at this point in the history
Summary:
fixes #23642
Pull Request resolved: #23646

Differential Revision: D16600874

Pulled By: soumith

fbshipit-source-id: 50f0828d774a558d6f21e9dd21135906bd5be128
  • Loading branch information
ssnl authored and facebook-github-bot committed Aug 1, 2019
1 parent 3b5daef commit 0539462
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion torch/utils/data/_utils/pin_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _pin_memory_loop(in_queue, out_queue, device_id, done_event):
except queue.Empty:
continue
idx, data = r
if not isinstance(data, ExceptionWrapper):
if not done_event.is_set() and not isinstance(data, ExceptionWrapper):
try:
data = pin_memory(data)
except Exception:
Expand Down
6 changes: 5 additions & 1 deletion torch/utils/data/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,9 +886,13 @@ def _shutdown_workers(self):
# corrupted data in `worker_result_queue` which `pin_memory_thread`
# reads from.
if hasattr(self, 'pin_memory_thread'):
self.pin_memory_thread_done_event.set()
# Use hasattr in case error happens before we set the attribute.
self.pin_memory_thread_done_event.set()
# Send something to pin_memory_thread in case it is waiting
# so that it can wake up and check `pin_memory_thread_done_event`
self.worker_result_queue.put((None, None))
self.pin_memory_thread.join()
self.worker_result_queue.close()

# Exit workers now.
self.workers_done_event.set()
Expand Down

0 comments on commit 0539462

Please sign in to comment.