xpra icon indicating copy to clipboard operation
xpra copied to clipboard

Sound doesn't work when XDG_RUNTIME_DIR is not set

Open patrickSpaceSurfer opened this issue 3 years ago • 22 comments

I am running xpra 4.2 with pulseaudio 14.2. My desktop environment does not use the XDG_RUNTIME_DIR environment variable and hence its not set.

When I start xpra I get

Warning: failed to query pulseaudio using 'pactl info'
 XDG_RUNTIME_DIR (/tmp) is not owned by us (uid 500), but by uid 0! (This could e.g. happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.)
 W: [(null)] caps.c: Normally all extra capabilities would be dropped now, but that's impossible because PulseAudio was built without capabilities support.
 Connection failure: Connection refused
 pa_context_connect() failed: Connection refused

Hence audio doesn't work because xpra cannot find the local pulse audio socket. The reason for this is because xpra sets the XDG_RUNTIME_DIR environment variable to /tmp when none is set. This causes the execution of pactl info to fail.

Running pactl info from my terminal I get

pim@patrick$ > pactl info
Server String: /tmp/pulse-H4z67MNfD3Rr/native
Library Protocol Version: 34
Server Protocol Version: 34
Is Local: yes
Client Index: 12
Tile Size: 65472
User Name: pim
Host Name: patrick.ritherdon
Server Name: pulseaudio
Server Version: 14.2
Default Sample Specification: s16le 2ch 44100Hz
Default Channel Map: front-left,front-right
Default Sink: oss_output.dsp2
Default Source: oss_input.dsp5
Cookie: a17c:460a

Running XDG_RUNTIME_DIR=/tmp pactl info from my terminal I get

pim@patrick$ > XDG_RUNTIME_DIR=/tmp pactl info
XDG_RUNTIME_DIR (/tmp) is not owned by us (uid 500), but by uid 0! (This could e.g. happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.)
W: [(null)] caps.c: Normally all extra capabilities would be dropped now, but that's impossible because PulseAudio was built without capabilities support.
Connection failure: Connection refused
pa_context_connect() failed: Connection refused

If I change xpra/scripts/main.py file such that it doesn't set the XDG_RUNTIME_DIR environment variable, then xpra runs fine and sound works:

root@patrick$ > diff /usr/local/lib/python3.8/site-packages/xpra/scripts/main.py.bak /usr/local/lib/python3.8/site-packages/xpra/scripts/main.py
382c382
<                 os.environ["XDG_RUNTIME_DIR"] = xrd
---
> #                os.environ["XDG_RUNTIME_DIR"] = xrd

patrickSpaceSurfer avatar Jun 08 '21 19:06 patrickSpaceSurfer

What distribution is it? Does it provide /run/user/$UID? 4.3 trunk creates /run/user/$UID/xpra directory if needed on server startup, but only if parent directory exists.

It probably makes sense to create XDG_RUNTIME_DIR not in /tmp but in $HOME/.xpra as last resort.

basilgello avatar Jun 11 '21 19:06 basilgello

I am running FreeBSD 13.0 which does not have /run/user/. I believe desktop environments on FreeBSD which use XDG_RUNTIME_DIR set it to /tmp/$(id -u), but I am not sure.

IMHO, if XDG_RUNTIME_DIR is not defined, then you should leave it undefined. It certainly all works fine for me without XDG_RUNTIME_DIR defined.

If you really need to define XDG_RUNTIME_DIR then you will have to run programs like pactl info with the original environment, because changing their environment is less likely to work and in the case of pactl info it doesn't matter what you set it to, it will not work.

patrickSpaceSurfer avatar Jun 11 '21 21:06 patrickSpaceSurfer

Setting XDG_RUNTIME_DIR to /tmp/$(id -u) on my system:

22:27:42 ~
pim@patrick$ > pactl info
Server String: /tmp/pulse-H4z67MNfD3Rr/native
Library Protocol Version: 34
Server Protocol Version: 34
Is Local: yes
Client Index: 57
Tile Size: 65472
User Name: pim
Host Name: patrick.ritherdon
Server Name: pulseaudio
Server Version: 14.2
Default Sample Specification: s16le 2ch 44100Hz
Default Channel Map: front-left,front-right
Default Sink: oss_output.dsp2
Default Source: oss_input.dsp5
Cookie: a17c:460a
22:27:47 ~
pim@patrick$ > mkdir /tmp/$(id -u)
22:27:53 ~
pim@patrick$ > chown $(id -u) /tmp/$(id -u)
22:28:03 ~
pim@patrick$ > XDG_RUNTIME_DIR=/tmp/$(id -u) pactl info
W: [(null)] caps.c: Normally all extra capabilities would be dropped now, but that's impossible because PulseAudio was built without capabilities support.
Connection failure: Connection refused
pa_context_connect() failed: Connection refused

patrickSpaceSurfer avatar Jun 11 '21 21:06 patrickSpaceSurfer

Then I guess @totaam will define FreeBSD as a separate platform because in Xpra too many code targeting Linux relies on {/var,}/run/user.

basilgello avatar Jun 11 '21 21:06 basilgello

Patrick, can you try running Xpra proxy server by chance and try connecting to shadow desktop, run commands etc? I fixed all this in Linyx but since FreeBSD does not use XDG_RUNTIME_DIR I guess most of it is broken, too.

basilgello avatar Jun 11 '21 21:06 basilgello

This is not a FreeBSD issue, its my desktop environment, which doesn't use XDG_RUNTIME_DIR. I have had linux machines with the same desktop environment and dont set XDG_RUNTIME_DIR.

xpra works fine for me without sound. All the other functionality (starting commands, detaching and attaching sessions) all works. Its just the sound.

I can manually fix the sound if I change xpra/scripts/main.py such that it doesn't set XDG_RUNTIME_DIR when its not set by the original environment. I have been running xpra for a couple of days with my modified xpra/scripts/main.py, running remote sessions (via ssh) on both linux (arch) and FreeBSD machines, all works fine.

The issue is that pactl info needs to be run with the same environment that was used to initially start the pulseaudio daemon. You must not change/add/remove XDG_RUNTIME_DIR for pactl info to work.

patrickSpaceSurfer avatar Jun 11 '21 21:06 patrickSpaceSurfer

I guess then it is best to mark the presence/absence of XDG_RUNTIME_DIR in scripts/main.py and take that info into account in the pactl invocation routine.

basilgello avatar Jun 11 '21 22:06 basilgello

I am pretty sure that would work great. Thanks for your help!

patrickSpaceSurfer avatar Jun 11 '21 22:06 patrickSpaceSurfer

Let me check that in the morning then :) Out of curiosity, which DE is it?

By "running Xpra proxy server and checking if attaching sessions etc works", I wanted you to run the following as your unprivileged user from terminal:

xpra proxy :14500 --bind-tcp=127.0.0.1:14500 --tcp-auth allow --daemon no

then access the HTML5 client via web browser at http://localhost:14500/connect.html and try running new commands, attach existing Xpra sessions etc.

I am afraid none of the functionality of HTML5 client works without XDG_RUNTIME_DIR set, currently.

basilgello avatar Jun 11 '21 22:06 basilgello

The machines I use tend not to run an X Display Manager chooser (in which case I run startx from a terminal) or they run xdm.

Either way my desktop environment is started by my .xinitrc script which starts pulseaudio, gkrellm and fluxbox (or sometimes even older things like mwm). So it really is a very simple (and old) desktop environment!

I could easily change my xinitrc to set XDG_RUNTIME_DIR to something appropriate (in fact I probably should), but I just thought I might let you guys know that people like me were having issues with xpra.

patrickSpaceSurfer avatar Jun 11 '21 22:06 patrickSpaceSurfer

I can run:

xpra proxy :20 --tcp-auth=allow --bind-tcp=127.0.0.1:14501
Warning: XDG_RUNTIME_DIR is not defined
 and '/run/user/500' does not exist
 using '/tmp'
Entering daemon mode; any further errors will be reported to:
  /tmp/:20.log.new

Seems to start the proxy fine.

However, I cant attach to it

xpra -d all attach tcp://localhost:14501/
Warning: XDG_RUNTIME_DIR is not defined
 and '/run/user/500' does not exist
 using '/tmp'
2021-06-11 23:32:40,678 run_mode error
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/xpra/scripts/main.py", line 128, in main
    return run_mode(script_file, err, options, args, mode, defaults)
  File "/usr/local/lib/python3.8/site-packages/xpra/scripts/main.py", line 407, in run_mode
    return do_run_mode(script_file, error_cb, options, args, mode, defaults)
  File "/usr/local/lib/python3.8/site-packages/xpra/scripts/main.py", line 445, in do_run_mode
    return run_client(error_cb, options, args, mode)
  File "/usr/local/lib/python3.8/site-packages/xpra/scripts/main.py", line 1618, in run_client
    check_gtk()
  File "/usr/local/lib/python3.8/site-packages/xpra/scripts/main.py", line 312, in check_gtk
    raise InitExit(EXIT_NO_DISPLAY, "failed to initialize Gtk, no display?")
xpra.scripts.config.InitExit: failed to initialize Gtk, no display?
failed to initialize Gtk, no display?

I must be missing some gtk python modules...

patrickSpaceSurfer avatar Jun 11 '21 22:06 patrickSpaceSurfer

On FreeBSD, I can't get it to work with the proxy command. I can run XDG_RUNTIME_DIR=/tmp/500 xpra proxy :14500 --tcp-auth allow --bind-tcp=127.0.0.1:14500 --daemon=no and it will start fine.

However when I visit http://localhost:14500/connect.html I get the error:

Server Error

Error code 400.

Message: this port does not support HTTP requests.

Error code explanation: 400 = Bad request syntax or unsupported method. 

The log in the xpra daemon terminal has the messages:

021-06-12 00:14:42,309 created tcp socket '127.0.0.1:14500'
2021-06-12 00:14:42,451 Error: cannot find the html web root
2021-06-12 00:14:42,451  '/usr/local/share/xpra/www' does not exist
2021-06-12 00:14:42,451  install the html-xpra package
2021-06-12 00:14:42,460 creating '/run/xpra' with permissions 0o775 and group 'xpra'
2021-06-12 00:14:42,460 Error: failed to create or change the permissions on '/run/xpra':
2021-06-12 00:14:42,460  [Errno 2] No such file or directory: '/run/xpra'
2021-06-12 00:14:42,461 Warning: failed to create socket directory '/var/run/xpra'
2021-06-12 00:14:42,461  [Errno 13] Permission denied: '/var/run/xpra'
2021-06-12 00:14:42,461 cannot create group socket '/var/run/xpra/patrick.ritherdon-14500'
2021-06-12 00:14:42,461  [Errno 2] No such file or directory
2021-06-12 00:14:42,461  /var/run/xpra does not exist
2021-06-12 00:14:42,461 created unix domain socket '/home/pim/.xpra/patrick.ritherdon-14500'
2021-06-12 00:14:42,464 Warning: failed to load the mdns publisher
2021-06-12 00:14:42,464  No module named 'zeroconf'
2021-06-12 00:14:42,464  either install the 'python-avahi' module
2021-06-12 00:14:42,464  or use the 'mdns=no' option
2021-06-12 00:14:42,472 xpra is ready.
2021-06-12 00:14:42,472 xpra proxy version 4.2-r0 64-bit
2021-06-12 00:14:42,475  uid=500 (pim), gid=500 (uknet)
2021-06-12 00:14:42,475  running with pid 80656 on FreeBSD 13.0-RELEASE
2021-06-12 00:14:42,480 watching for applications menu changes in:
2021-06-12 00:14:42,480  '/usr/local/share/applications'
2021-06-12 00:14:59,853 Error: tcp connection failed:
2021-06-12 00:14:59,853  packet from 127.0.0.1:19257
2021-06-12 00:14:59,853  received on 127.0.0.1:14500
2021-06-12 00:14:59,853  this packet looks like a 'http' packet
2021-06-12 00:14:59,853  http upgrades are not enabled
2021-06-12 00:15:00,418 Error: tcp connection failed:
2021-06-12 00:15:00,418  packet from 127.0.0.1:52959
2021-06-12 00:15:00,418  received on 127.0.0.1:14500
2021-06-12 00:15:00,418  this packet looks like a 'http' packet
2021-06-12 00:15:00,418  http upgrades are not enabled

I installed xpra via the freebsd port, I guess it doesn't include all the functionality needed.

patrickSpaceSurfer avatar Jun 11 '21 23:06 patrickSpaceSurfer

On arch linux it also doesn't work, but I get a different error in my web browser. It simply says The connection was reset

The log in the xpra daemon terminal has the messages:

XDG_XDG_RUNTIME_DIR=/tmp/500 xpra proxy :14500 --tcp-auth allow --bind-tcp=0.0.0.0:14500 --daemon=no 
2021-06-12 00:18:41,704 Error: cannot enable SSH socket upgrades:
2021-06-12 00:18:41,704  No module named 'paramiko'
2021-06-12 00:18:41,704 created tcp socket '0.0.0.0:14500'
2021-06-12 00:18:41,713 created unix domain socket '/tmp/500/xpra/mediapc.ritherdon-14500'
2021-06-12 00:18:41,713 cannot create group socket '/run/xpra/mediapc.ritherdon-14500'
2021-06-12 00:18:41,714  [Errno 13] Permission denied
2021-06-12 00:18:41,714 created unix domain socket '/home/pim/.xpra/mediapc.ritherdon-14500'
2021-06-12 00:18:41,757 Error: cannot find the html web root
2021-06-12 00:18:41,757  '/usr/share/xpra/www' does not exist
2021-06-12 00:18:41,780 Error setting up server dbus instance:
2021-06-12 00:18:41,780  No module named 'dbus'
2021-06-12 00:18:41,781 Warning: failed to load the mdns publisher
2021-06-12 00:18:41,781  No module named 'zeroconf'
2021-06-12 00:18:41,781  either install the 'python-avahi' module
2021-06-12 00:18:41,781  or use the 'mdns=no' option
2021-06-12 00:18:41,782 xpra is ready.
2021-06-12 00:18:41,782 xpra proxy version 4.1.3 64-bit
2021-06-12 00:18:41,782  uid=1000 (pim), gid=995 (audio)
2021-06-12 00:18:41,783  running with pid 70423 on Linux unknown unknown unknown
2021-06-12 00:19:14,942 Error: tcp connection failed:
2021-06-12 00:19:14,942  packet from 10.0.0.1:36849
2021-06-12 00:19:14,942  received on 0.0.0.0:14500
2021-06-12 00:19:14,943  this packet looks like a 'http' packet
2021-06-12 00:19:14,943  http upgrades are not enabled

Looks like the arch linux package also doesn't support connections via http

patrickSpaceSurfer avatar Jun 11 '21 23:06 patrickSpaceSurfer

Ah, you need spalso xpra-html5 for this functionality.

basilgello avatar Jun 11 '21 23:06 basilgello

@totaam want your approval for unsetting XDG_RUNTIME_DIR for pactl if it was initially not set.

basilgello avatar Jun 14 '21 17:06 basilgello

@totaam want your approval for unsetting XDG_RUNTIME_DIR for pactl if it was initially not set.

@basilgello Sorry, lost track. Which pactl command is this for? The server usually starts its own private pulseaudio server. There's a risk of damaging a pulseaudio server we have no business interacting with.

Assuming that this is not a problem, rather than unsetting XDG_RUNTIME_DIR you may want to use xpra.os_util.saved_env - which is saved very early.

totaam avatar Jun 14 '21 17:06 totaam

@basilgello bump!

totaam avatar Jul 13 '21 13:07 totaam

ah? :)

basilgello avatar Jul 13 '21 13:07 basilgello

@basilgello see https://github.com/Xpra-org/xpra/issues/3160#issuecomment-860867011 Which pactl command is this for?

totaam avatar Nov 01 '21 14:11 totaam

I recall it was pactl sink management, but let me refresh it pls

basilgello avatar Nov 01 '21 20:11 basilgello

@basilgello bump!

totaam avatar May 17 '22 15:05 totaam

@totaam meow!! :D

Let me build the trunk and re-test tonight.

basilgello avatar May 17 '22 15:05 basilgello