-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[bugfix] add optional MEDIA_DECODE_TIMEOUT to prevent silent media-decode hang #9541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HaozheZhang6
wants to merge
3
commits into
modelscope:main
Choose a base branch
from
HaozheZhang6:fix/media-decode-timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
4dd200b
[bugfix] add optional MEDIA_DECODE_TIMEOUT to prevent silent media-de…
HaozheZhang6 acf7068
[bugfix] use Pipe+poll for media decode timeout to avoid large-payloa…
HaozheZhang6 00e9092
test: rewrite media-decode-timeout test as unittest.TestCase
HaozheZhang6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import pytest | ||
| import time | ||
|
|
||
| from swift.template.vision_utils import _decode_with_timeout | ||
|
|
||
|
|
||
| def _sleep_forever(*args, **kwargs): | ||
| time.sleep(3600) | ||
|
|
||
|
|
||
| def _echo(value): | ||
| return value | ||
|
|
||
|
|
||
| def _raise_value_error(*args, **kwargs): | ||
| raise ValueError('boom') | ||
|
|
||
|
|
||
| def test_decode_with_timeout_kills_hung_decode(monkeypatch): | ||
| # A decode that never returns must be killed and surface a TimeoutError rather than hang. | ||
| monkeypatch.setenv('MEDIA_DECODE_TIMEOUT', '2') | ||
| start = time.time() | ||
| with pytest.raises(TimeoutError): | ||
| _decode_with_timeout(_sleep_forever) | ||
| assert time.time() - start < 30 | ||
|
|
||
|
|
||
| def test_decode_with_timeout_returns_result_when_fast(monkeypatch): | ||
| monkeypatch.setenv('MEDIA_DECODE_TIMEOUT', '10') | ||
| assert _decode_with_timeout(_echo, 'ok') == 'ok' | ||
|
|
||
|
|
||
| def test_decode_with_timeout_propagates_decode_error(monkeypatch): | ||
| monkeypatch.setenv('MEDIA_DECODE_TIMEOUT', '10') | ||
| with pytest.raises(ValueError, match='boom'): | ||
| _decode_with_timeout(_raise_value_error) | ||
|
|
||
|
|
||
| def test_decode_with_timeout_disabled_calls_directly(monkeypatch): | ||
| # Default (unset / 0): no subprocess, original behavior and zero overhead. | ||
| monkeypatch.delenv('MEDIA_DECODE_TIMEOUT', raising=False) | ||
| assert _decode_with_timeout(_echo, 'direct') == 'direct' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
multiprocessing.SimpleQueuecan lead to a silent deadlock when the decoded payload is large.The Issue
SimpleQueueis backed by an OS pipe. If the serialized payload (e.g., decoded audio arrays or video frames) exceeds the OS pipe buffer limit (typically 64KB on Linux), the child process'squeue.put()call will block indefinitely until the parent process reads from the queue. However, the parent process callsprocess.join(timeout)before reading from the queue. This creates a classic deadlock: the child is blocked waiting for the parent to read, and the parent is blocked waiting for the child to finish. This will cause a falseTimeoutErrorand terminate the child process for any large media file.The Solution
Use
multiprocessing.Pipeandpoll(timeout)instead. This allows the parent process to detect if data is available to read (or if the child has exited/errored) and read from the pipe, which unblocks the child process if the buffer fills up.