Skip to content

Commit 76e916a

Browse files
authored
Make sure to close file stream after backup upload (home-assistant#5723)
* Make sure to close file stream after backup upload Currently the file stream does not get closed before importing the file stream. It seems the test case didn't catch that, presumably because it is a race condition if the bytes get flushed to disk or not. Properly close the stream before continue handling the file. * Close file stream in executor * Add comment about closing twice is fine
1 parent 582b128 commit 76e916a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

supervisor/api/backups.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,8 @@ def open_backup_file() -> Path:
531531

532532
def close_backup_file() -> None:
533533
if backup_file_stream:
534+
# Make sure it got closed, in case of exception. It is safe to
535+
# close the file stream twice.
534536
backup_file_stream.close()
535537
if temp_dir:
536538
temp_dir.cleanup()
@@ -541,6 +543,7 @@ def close_backup_file() -> None:
541543
tar_file = await self.sys_run_in_executor(open_backup_file)
542544
while chunk := await contents.read_chunk(size=2**16):
543545
await self.sys_run_in_executor(backup_file_stream.write, chunk)
546+
await self.sys_run_in_executor(backup_file_stream.close)
544547

545548
backup = await asyncio.shield(
546549
self.sys_backups.import_backup(
@@ -563,8 +566,7 @@ def close_backup_file() -> None:
563566
return False
564567

565568
finally:
566-
if temp_dir or backup:
567-
await self.sys_run_in_executor(close_backup_file)
569+
await self.sys_run_in_executor(close_backup_file)
568570

569571
if backup:
570572
return {ATTR_SLUG: backup.slug}

0 commit comments

Comments
 (0)