Skip to content

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

Merged
Breakthrough merged 3 commits into
Breakthrough:mainfrom
M-Colley:fix/detect-close-video-stream
Nov 29, 2026
Merged

Breakthrough merged 3 commits 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).

@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 Resolved

@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 👍

@Breakthrough
Breakthrough force-pushed the main branch 2 times, most recently from 9f4c933 to a696070 Compare November 24, 2026 03:31
@M-Colley

Copy link
Copy Markdown
Contributor Author

Updated tests/test_api.py to wrap scenedetect.open_video and assert that close() is called once on both normal return and error.

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).
@M-Colley
M-Colley force-pushed the fix/detect-close-video-stream branch from 1fb7f73 to 46619b1 Compare November 27, 2026 21:46
Comment thread tests/test_api.py
import shutil
from unittest.mock import MagicMock

import scenedetect

@Breakthrough Breakthrough Nov 28, 2026 •

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.

Can you use monkeypatch.setattr("scenedetect.open_video", tracking_open_video) and add open_video to the from scenedetect import ... line, so the import scenedetect can be removed (fixing this warning)?

@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.

I want to cut 0.7.4, will address any unresolved issues on main. Thanks for the PR!

@Breakthrough
Breakthrough dismissed MuhammadBilalKhan267’s stale review November 29, 2026 23:14

Addressed in 46619b1: the test now asserts close() is called on return and on error.

@Breakthrough
Breakthrough merged commit 9133889 into Breakthrough:main Nov 29, 2026
25 checks passed
@M-Colley
M-Colley deleted the fix/detect-close-video-stream branch December 3, 2026 10:47
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.

4 participants