willprice / python-omxplayer-wrapper

:tv: Control OMXPlayer, the Raspberry Pi media player, from Python
http://python-omxplayer-wrapper.readthedocs.io
GNU Lesser General Public License v3.0
253 stars 71 forks source link

Race condition with exitEvent #192

Open matthijskooijman opened 4 years ago

matthijskooijman commented 4 years ago

Issue Report

Description

There is a race condition when a player is started and very quickly exits, before the code has a chance to register an exitEvent handler.

Problem reproduction

The following program wants to print "Exit", but does not. The sleep in there is to artificially enlarge the window for the race condition to occur. Without the sleep, this will usually print "Exit", but there might be rare cases where the process quits before the exitEvent is registered.

from omxplayer.player import OMXPlayer
from pathlib import Path
from time import sleep

player = OMXPlayer(Path("/dev/null"))
sleep(1)
player.exitEvent += lambda p, e: print("Exit")

A structure like this is important for a program that wants to keep playing things. That needs some way to guarantee that the exitEvent handler is always run, so it can start playing something else even when the player exits immediately due to whatever reason.

AFAICS, there is currently no way to fix this race condition. To do so, you would need to register the exitEvent before the player is started, which is currently impossible.

Maybe a solution could be combined with #161, which suggests adding a no-argument constructor to start a player and a load() method to switch to a different source. If that constructor would not start the player, just construct the object and there would be either a separate start() method, or the first call to load() would start the player, then there would be a safe window to register the exitEvent handler.

Environment details

Software Version
python-omxplayer-wrapper 0.3.2
python-dbus (dpkg -s python-dbus) 1.2.12
python (python --version) 3.7.3
omxplayer (omxplayer --version) f543a0d