Temporary GIF file may not be deleted on Windows in `make_video()`

Author: KIN-Kazuhiro-kyoto-u-2024Created Sep 26, 2026Updated Sep 26, 2026

Describe the bug In tensorboardX/summary.py, make_video() creates a temporary file with delete=False and attempts to remove it while the NamedTemporaryFile context is still active:

python
with tempfile.NamedTemporaryFile(suffix='.gif', delete=False) as fp:
    filename = fp.name

    ...

    with open(filename, 'rb') as f:
        tensor_string = f.read()

    try:
        os.remove(filename)
    except OSError:
        logger.warning('The temporary file used by moviepy cannot be deleted.')

On Windows, this can cause os.remove() to fail because the temporary file is still open.

Minimal runnable code to reproduce the behavior

python
import os
import tempfile

with tempfile.NamedTemporaryFile(delete=False) as f:
    filename = f.name
    f.write(b"test")

    try:
        os.remove(filename)
        print("remove succeeded")
    except OSError as e:
        print(f"remove failed: {e}")

print(f"file exists after with: {os.path.exists(filename)}")

This produced:

remove failed: [WinError 32] ...
file exists after with: True

In contrast, moving the os.remove() call outside the NamedTemporaryFile context succeeds:

python
import os
import tempfile

with tempfile.NamedTemporaryFile(delete=False) as f:
    filename = f.name
    f.write(b"test")

try:
    os.remove(filename)
    print("remove succeeded")
except OSError as e:
    print(f"remove failed: {e}")

print(f"file exists: {os.path.exists(filename)}")

This results in the temporary file being successfully removed.

Expected behavior The temporary GIF file should be successfully removed after make_video() finishes, including on Windows, without leaving the temporary file behind.

Environment The result of pip list|grep -E "torch|proto|tensor" pip list | findstr "torch proto tensor":

protobuf                7.36.2
tensorboard             2.21.0
tensorboard-data-server 0.7.2
tensorboardX            2.6.5

Python environment Python: 3.13.15 (Virtual environment created by venv) OS: Windows 11 Home

Additional context This appears to be related to PR #164, which initially introduced a cleanup of temporary GIF files in order to resolve issue #160 ("Gif temporary files never deleted").

One possible fix would be to move the existing try/except block outside the NamedTemporaryFile context:

python
with tempfile.NamedTemporaryFile(suffix='.gif', delete=False) as fp:
    filename = fp.name

    ...

    with open(filename, 'rb') as f:
        tensor_string = f.read()

try:
    os.remove(filename)
except OSError:
    logger.warning('The temporary file used by moviepy cannot be deleted.')

Another possibility, for Python 3.12 and later, would be to use:

python
with tempfile.NamedTemporaryFile(
    suffix='.gif',
    delete=True,
    delete_on_close=False,
) as fp:
    ...

This would allow the file to be opened by name while ensuring that it is deleted when the context manager exits.

However, since delete_on_close was introduced in Python 3.12, this approach would not be compatible with Python versions prior to 3.12. Therefore, moving os.remove() outside the context may be preferable if support for older Python versions needs to be maintained.