Skip to content

Commit 4bf6dd1

Browse files
guillochonawaelchli
authored andcommitted
Close SummaryWriter in TensorBoardLogger on finalize (#5696)
Not entirely sure this is the "right" solution to this problem, but currently when model fitting is finished the `TensorBoardLogger` attribute `_experiment` (a `SummaryWriter`) is left with an open file handle. This causes issues in particular on Windows systems (and probably others), and also makes the files un-syncable on cloud-synced devices like OneDrive. This PR adds a `close()` to `finalize` to make sure this handle is closed upon fit completion. Co-authored-by: Adrian Wälchli <[email protected]>
1 parent 8732475 commit 4bf6dd1

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
188188

189189
### Fixed
190190

191+
- Fixed `TensorBoardLogger` not closing `SummaryWriter` on `finalize` ([#5696](https://github.com/PyTorchLightning/pytorch-lightning/pull/5696))
192+
193+
191194
- Fixed filtering of pytorch "unsqueeze" warning when using DP ([#5622](https://github.com/PyTorchLightning/pytorch-lightning/pull/5622))
192195

193196

pytorch_lightning/loggers/tensorboard.py

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ def save(self) -> None:
237237
@rank_zero_only
238238
def finalize(self, status: str) -> None:
239239
self.experiment.flush()
240+
self.experiment.close()
240241
self.save()
241242

242243
@property

tests/loggers/test_tensorboard.py

+9
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,12 @@ def configure_optimizers(self):
265265

266266
mock_count_steps = [m[2]["step"] for m in mock_log_metrics.mock_calls if "count_step" in m[2]["metrics"]]
267267
assert model._indexes == mock_count_steps
268+
269+
270+
@mock.patch('pytorch_lightning.loggers.tensorboard.SummaryWriter')
271+
def test_tensorboard_finalize(summary_writer, tmpdir):
272+
""" Test that the SummaryWriter closes in finalize. """
273+
logger = TensorBoardLogger(save_dir=tmpdir)
274+
logger.finalize("any")
275+
summary_writer().flush.assert_called()
276+
summary_writer().close.assert_called()

0 commit comments

Comments
 (0)