Skip to content
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

Deadlock on creating audio segment if another python thread reads from stdin #822

Open
ggolda opened this issue Dec 18, 2024 · 1 comment

Comments

@ggolda
Copy link

ggolda commented Dec 18, 2024

Steps to reproduce

Looks like pydub attempts to read the stdin in the scenario below in audio_segment.py (https://github.com/jiaaro/pydub/blob/master/pydub/audio_segment.py#L766). If another python thread is currently trying to read from stdin, pydub gets stuck forever in a deadlock state.

This is reproduceable only when python is run as a child subprocess of another process (e.g. if you spawn it from another program or run in a jupyter playbook) and tested on windows, probably linux handles simultaneous stdin reads better.

Reading from stdin is a common pattern on windows (sadly) to handle graceful shutdowns because OS doesn't have signals like posix os.

To reproduce, run this code in a jupyter playbook:

import os, sys
import threading
import signal
from pydub import AudioSegment


def windows_graceful_shutdown_listener():
    """
    Windows doesn't have signals, so we need to listen to stdin for quit command
    """
    for line in sys.stdin:
        if line == "quit\n":
            signal.raise_signal(signal.SIGINT)


threading.Thread(target=windows_graceful_shutdown_listener, daemon=True).start()

video_uri = "C:/path/to/file/video.mp4"

audio_file = AudioSegment.from_file(video_uri, start_second=0, duration=10)
audio_file.set_frame_rate(16000)
audio_file.export("test.wav", format="wav")

Expected behavior

pydub shouldn't hang and complete the work, or at least throw a timeout exception if it can not read from stdin.

Actual behavior

pydub hangs in a deadlock state indefinitely.

Your System configuration

  • Python version: 3.12.4
  • Pydub version: 0.25.1
  • ffmpeg or avlib?: ffmpeg
  • ffmpeg/avlib version: 6.0

Is there an audio file you can include to help us reproduce?

Works on any audio or video file

@ggolda
Copy link
Author

ggolda commented Dec 18, 2024

submitted a PR that fixes this issue

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

No branches or pull requests

1 participant