Skip to content

Commit

Permalink
BUG: close standard streams after webcam access (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisvang committed Dec 22, 2021
1 parent 6caf2fa commit 9c434e0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
24 changes: 15 additions & 9 deletions imageio/plugins/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,24 @@ def _get_cam_inputname(self, index):
"-i",
"dummy",
]
# Set `shell=True` in sp.Popen to prevent popup of a command line
# window in frozen applications. Note: this would be a security
# vulnerability if user-input goes into the cmd.
proc = sp.Popen(
cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=True
# Set `shell=True` in sp.run to prevent popup of a command
# line window in frozen applications. Note: this would be a
# security vulnerability if user-input goes into the cmd.
# Note that the ffmpeg process returns with exit code 1 when
# using `-list_devices` (or `-list_options`), even if the
# command is successful, so we set `check=False` explicitly.
completed_process = sp.run(
cmd,
stdout=sp.PIPE,
stderr=sp.PIPE,
encoding="utf-8",
shell=True,
check=False,
)
proc.stdout.readline()
proc.terminate()
infos = proc.stderr.read().decode("utf-8", errors="ignore")

# Return device name at index
try:
name = parse_device_names(infos)[index]
name = parse_device_names(completed_process.stderr)[index]
except IndexError:
raise IndexError("No ffdshow camera at index %i." % index)
return "video=%s" % name
Expand Down
30 changes: 29 additions & 1 deletion tests/test_ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import numpy as np

from pytest import raises, skip
from pytest import raises, skip, warns
from imageio.testing import run_tests_if_main, get_test_dir

import imageio
Expand Down Expand Up @@ -561,6 +561,34 @@ def test_webcam_process_termination():
skip("no webcam")


def test_webcam_resource_warnings():
"""
Test for issue #697. Ensures that ffmpeg Reader standard streams are
properly closed by checking for ResourceWarning "unclosed file".
todo: use pytest.does_not_warn() as soon as it becomes available
(see https://github.com/pytest-dev/pytest/issues/9404)
"""
try:
with warns(None) as warnings:
with imageio.get_reader("<video0>"):
pass
except IndexError:
skip("no webcam")

import imageio_ffmpeg

if imageio_ffmpeg.__version__ == "0.4.5":
# We still expect imagio_ffmpeg 0.4.5 to generate (at most) one warning.
# todo: remove this assertion when a fix for imageio_ffmpeg issue #61
# has been released
assert len(warnings) < 2
return

# There should not be any warnings, but show warning messages if there are.
assert not [w.message for w in warnings]


def show_in_console():
reader = imageio.read("imageio:cockatoo.mp4", "ffmpeg")
# reader = imageio.read('<video0>')
Expand Down

0 comments on commit 9c434e0

Please sign in to comment.