Skip to content

[bugfix] Close video stream in detect() via context manager - #616

Open
M-Colley wants to merge 1 commit into
Breakthrough:mainfrom
M-Colley:fix/detect-close-video-stream
Open

M-Colley wants to merge 1 commit into
Breakthrough:mainfrom
M-Colley:fix/detect-close-video-stream

Conversation

@M-Colley

Copy link
Copy Markdown
Contributor

Summary

scenedetect.detect() in scenedetect/__init__.py opens a VideoStream via open_video(video_path, backend=backend) without using VideoStream as a context manager. When detect() returns or raises an exception (such as ValueError on an invalid start_time or end_time), the underlying file descriptor remains open in the stack frame. On Windows, callers attempting to delete or move temporary video files immediately hit PermissionError: [WinError 32].

Reproduction

import pathlib
import tempfile
from scenedetect import ContentDetector, detect

with tempfile.TemporaryDirectory() as tmp:
    vid = pathlib.Path(tmp) / "clip.mp4"
    # Copy any valid mp4 to vid, then trigger an error inside detect():
    try:
        detect(str(vid), ContentDetector(), start_time="bad_timecode")
    except ValueError:
        vid.unlink()  # Raises PermissionError: [WinError 32] on Windows

Fix and Verification

  1. Wrap open_video(video_path, backend=backend) in with open_video(video_path, backend=backend) as video: inside scenedetect.detect().
  2. Add test_api_detect_closes_stream_on_return_and_error in tests/test_api.py verifying immediate Path.unlink() cleanup after both normal execution and ValueError exceptions (12/12 test_api.py tests passing, ruff check and ruff format --check clean).

Wrap open_video() in a with statement inside scenedetect.detect() so
underlying file handles and decoders are deterministically closed on
both normal completion and exceptions (preventing WinError 32 file
locks on Windows).

@MuhammadBilalKhan267 MuhammadBilalKhan267 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. Left one comment.

Comment thread tests/test_api.py
shutil.copyfile(test_video_file, error_copy)
with pytest.raises(ValueError):
detect(error_copy, ContentDetector(), start_time="invalid_timecode")
error_copy.unlink()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we assert that close() is called instead of deleting the video? Linux allows deletion while the stream is still open. The test doesn't cover that.

@Breakthrough Breakthrough left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👍

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants