Skip to content

Exiting frames loop with torchvision installed hangs foreve #1861

@oliverdain

Description

@oliverdain

I reported the following bug in #1849 which as closed as it was believed it was fixed with an upgrade to a newer version of ffmpeg. However, I just upgraded to 14.3.0, which uses that newer version of ffmpeg (per av.ffmpeg_version_info it is 7.1.1) and the bug described below is still present. I would have just re-opened the original bug, but since it was converted to a conversation that doesn't seem to be possible.

Original bug:

Using torchvision with av with video threading mode of AUTO or FRAME causes a hang. Note, you don't even need to use torchvision, just importing it causes the issue.

Minimal reproducing example:

from pathlib import Path
import sys
import av
# Comment this out and it doesn't hang
import torchvision.transforms.v2.functional

THIS_DIR = Path(__file__).parent

vids = THIS_DIR.glob('*.mp4')
for vid in vids:
    print('VID:', vid)
    sys.stdout.flush()
    with av.open(vid, mode='r') as container:
        assert len(container.streams.video) == 1
        # Comment this out and it doesn't hang
        container.streams.video[0].thread_type = 'AUTO'
        for i, frame in enumerate(container.decode(video=0)):
            print('Frame:', frame)
            sys.stdout.flush()
            # Comment out the early exit and it doesn't hang
            if i == 2:
                break

The keys here are (1) you have to import torchvision - if you don't it works fine (2) you have to specify thread_type = 'AUTO' or thread_type = 'FRAME' or it doesn't hang and (3) you have to stop decoding the video before you reach the end - if you decode all frames it's fine.

What happens is it hits the break line but doesn't actually break - it just hangs. I have also pytorch/vision#9010 as it's not clear whose bug it is.

I think this will happen on just about any video but, if not, I'm happy to provide the mp4 files that are exhibiting the bug for me.

Here is a requirements.txt file that reproduces the issue with Python 3.10.6

--extra-index-url https://download.pytorch.org/whl/cu118
av == 14.2.0
torch == 2.6.0
torchvision == 0.21.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions