Skip to content

Commit

Permalink
Ensure finalize and finalize_iteration are called during shutdown/exc…
Browse files Browse the repository at this point in the history
…eption (pytorch#846)

Summary:
Pull Request resolved: pytorch#846

Fixes pytorch#821

1. Ensure `finalize_iteration` is called during `DataLoader2.shutdown()`
2. If an uncaught exception is raised by `DataLoader2Iterator.next()`, ensure finalization calls are made

Test Plan: Imported from OSS

Reviewed By: ejguan

Differential Revision: D40569566

Pulled By: NivekT

fbshipit-source-id: f0768a62fcc6dfba1aef3a70c8c55563a45304aa
  • Loading branch information
NivekT authored and ejguan committed Oct 21, 2022
1 parent b8eb777 commit 5ced8dc
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions torchdata/dataloader2/dataloader2.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def __next__(self) -> T_co:
if self.dataloader.reading_service is not None:
self.dataloader.reading_service.finalize_iteration()
raise
except Exception:
if self.dataloader:
self.dataloader.shutdown()
raise
else:
if self.dataloader.reading_service is not None:
self.dataloader.reading_service.finalize_iteration()
Expand Down Expand Up @@ -198,6 +202,7 @@ def shutdown(self) -> None:
self._datapipe_iter = None
if not self._terminated:
if self.reading_service is not None:
self.reading_service.finalize_iteration()
self.reading_service.finalize()
self._terminated = True

Expand Down

0 comments on commit 5ced8dc

Please sign in to comment.