Repository navigation
fix(test): keep private PulseAudio off the session bus #7831
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
75b3490
chore(quest): claim quest/m0/test-flakes-2/pulse-dbus.md
kixelated f00d57e
fix(test): keep private PulseAudio off the session bus
kixelated 42b4384
test: isolate and exercise concurrent private audio servers
kixelated 1ef8eb0
test: include private audio logs when the concurrency check hangs
kixelated aed9b44
Merge remote-tracking branch 'origin/main' into HEAD
kixelated 273a4e9
test: keep the private audio test's paths short and escaped
kixelated 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
This file was deleted.
Oops, something went wrong.
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
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,132 @@ | ||
| """Private audio servers start together and play on their own sinks under one session bus.""" | ||
|
|
||
| import array | ||
| import json | ||
| import math | ||
| import os | ||
| import signal | ||
| import string | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
| import wave | ||
| from contextlib import ExitStack | ||
| from pathlib import Path | ||
|
|
||
| HERE = Path(__file__).resolve().parent | ||
| # Bytes a D-Bus address value may carry unescaped; the rest are %-encoded, including XML's `&` and `<`. | ||
| DBUS_SAFE = frozenset((string.ascii_letters + string.digits + "-_/.\\*").encode()) | ||
|
|
||
|
|
||
| def dbus_unix_path(path): | ||
| return "unix:path=" + "".join(chr(b) if b in DBUS_SAFE else f"%{b:02x}" for b in os.fsencode(path)) | ||
|
|
||
|
|
||
| def play(tone): | ||
| assert os.environ["DBUS_SESSION_BUS_ADDRESS"] | ||
| info = json.loads(subprocess.check_output(["pactl", "--format=json", "info"])) | ||
| assert info["server_string"] == os.environ["PULSE_SERVER"] | ||
| assert info["default_sink_name"] == "moq-null" | ||
| # Hold both daemons alive before either playback can finish and release its D-Bus name. | ||
| print("ready", flush=True) | ||
| assert sys.stdin.readline() == "play\n" | ||
| subprocess.run(["paplay", "--device=moq-null", str(tone)], check=True) | ||
|
|
||
|
|
||
| def parallel(root): | ||
| with ExitStack() as children: | ||
| runs = [ | ||
| children.enter_context( | ||
| subprocess.Popen( | ||
| [ | ||
| sys.executable, | ||
| str(HERE / "pulse.py"), | ||
| "--dir", | ||
| str(root / name), | ||
| "--", | ||
| sys.executable, | ||
| str(Path(__file__).resolve()), | ||
| "--play", | ||
| str(root / "tone.wav"), | ||
| ], | ||
| stdin=subprocess.PIPE, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.STDOUT, | ||
| ) | ||
| ) | ||
| for name in ("one", "two") | ||
| ] | ||
| for child in runs: | ||
| line = child.stdout.readline() | ||
| if line != b"ready\n": | ||
| # Surface why, such as a startup rejection or a failed pactl check, not only that it failed. | ||
| output, _ = child.communicate() | ||
| raise AssertionError("private server exited before playback:\n" + (line + output).decode()) | ||
| for child in runs: | ||
| child.stdin.write(b"play\n") | ||
| child.stdin.flush() | ||
| for child in runs: | ||
| output, _ = child.communicate() | ||
| assert child.returncode == 0, output.decode() | ||
|
|
||
|
|
||
| @unittest.skipUnless(sys.platform == "linux", "private PulseAudio is Linux-only") | ||
| class Pulse(unittest.TestCase): | ||
| def test_parallel_playback(self): | ||
| # The system temp dir keeps both Unix sockets well inside sockaddr_un's 108 bytes, however deep the checkout. | ||
| with tempfile.TemporaryDirectory(prefix="pulse-") as directory: | ||
| root = Path(directory) | ||
| bus_config = root / "bus.conf" | ||
| bus_config.write_text( | ||
| f"<busconfig><type>session</type><listen>{dbus_unix_path(root / 'b')}</listen><auth>EXTERNAL</auth>" | ||
| '<policy context="default"><allow own="*"/><allow send_destination="*"/>' | ||
| '<allow receive_sender="*"/></policy></busconfig>' | ||
| ) | ||
| with wave.open(str(root / "tone.wav"), "wb") as tone: | ||
| tone.setparams((1, 2, 48000, 0, "NONE", "not compressed")) | ||
| samples = array.array("h", (int(8000 * math.sin(2 * math.pi * 440 * i / 48000)) for i in range(12000))) | ||
| if sys.byteorder != "little": | ||
| samples.byteswap() | ||
| tone.writeframes(samples.tobytes()) | ||
| # The shared bus is private to this test, never the desktop's session bus. | ||
| with subprocess.Popen( | ||
| [ | ||
| "dbus-run-session", | ||
| f"--config-file={bus_config}", | ||
| "--", | ||
| sys.executable, | ||
| str(Path(__file__).resolve()), | ||
| "--parallel", | ||
| str(root), | ||
| ], | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.STDOUT, | ||
| start_new_session=True, | ||
| ) as session: | ||
| timed_out = False | ||
| try: | ||
| # Each pulse.py may spend 30 s on startup alone; leave room for playback after it. | ||
| session.communicate(timeout=60) | ||
| except subprocess.TimeoutExpired: | ||
| timed_out = True | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| finally: | ||
| # Reap the whole test tree even if startup or a playback client hangs. | ||
| try: | ||
| os.killpg(session.pid, signal.SIGKILL) | ||
| except ProcessLookupError: | ||
| pass | ||
| output, _ = session.communicate() | ||
| logs = "\n".join(f"{log}:\n{log.read_text()}" for log in root.glob("*/pulse.log")) | ||
| diagnostics = output.decode() + logs | ||
| self.assertFalse(timed_out, "private audio test timed out\n" + diagnostics) | ||
| self.assertEqual(session.returncode, 0, diagnostics) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| if sys.argv[1:2] == ["--play"]: | ||
| play(Path(sys.argv[2])) | ||
| elif sys.argv[1:2] == ["--parallel"]: | ||
| parallel(Path(sys.argv[2])) | ||
| else: | ||
| unittest.main() | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.