MuseScore icon indicating copy to clipboard operation
MuseScore copied to clipboard

Can't run MuseScore CLI in headless mode

Open j-tai opened this issue 1 year ago • 20 comments

Issue type

Crash or freeze

Bug description

In MuseScore 3, we could run the MuseScore command-line interface without a display server (for example, on a headless server) by passing the option -platform offscreen. For example:

$ mscore -platform offscreen -o output.mid input.mscz

This no longer works in MuseScore 4, with or without that option. It fails to start in both cases:

$ mscore -v
QEventLoop: Cannot be used without QApplication
QEventLoop: Cannot be used without QApplication
qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: eglfs, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb.

$ mscore -platform offscreen -v
QEventLoop: Cannot be used without QApplication
QEventLoop: Cannot be used without QApplication

(mscore:2): Gtk-WARNING **: 11:47:02.947: cannot open display:

Expected behavior: the above two commands should print the MuseScore version (because of -v flag).

Steps to reproduce

On a Linux machine:

$ unset DISPLAY  # remove the DISPLAY env var so MuseScore can't connect to a display server
$ mscore3 -platform offscreen -v

Screenshots/Screen recordings

No response

MuseScore Version

4.0.2 (Flatpak)

Regression

Yes, this used to work in Musescore 3.x and now is broken

Operating system

Ubuntu 22.04.2 LTS

Additional context

No response

j-tai avatar Apr 14 '23 19:04 j-tai

The Flatpak version is not supported here, so does the same issue exist with the AppImages?

Jojo-Schmitz avatar Apr 14 '23 20:04 Jojo-Schmitz

The Flatpak version is not supported here, so does the same issue exist with the AppImages?

Ah got it. The same issue occurs with the AppImage downloaded from musescore.org, with a slightly different error message:

$ unset DISPLAY
$ ./MuseScore-4.0.2.230651545-x86_64.AppImage -v
/usr/lib/x86_64-linux-gnu/pipewire-0.3/jack/libjack.so.0
/lib/x86_64-linux-gnu/libnss3.so
QEventLoop: Cannot be used without QApplication
QEventLoop: Cannot be used without QApplication
qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.

/tmp/.mount_MuseScfNQmnA/AppRun: line 26: 394950 Aborted                 (core dumped) "${APPDIR}/bin/mscore4portable" "$@"
$ ./MuseScore-4.0.2.230651545-x86_64.AppImage -platform offscreen -v
/usr/lib/x86_64-linux-gnu/pipewire-0.3/jack/libjack.so.0
/lib/x86_64-linux-gnu/libnss3.so
QEventLoop: Cannot be used without QApplication
QEventLoop: Cannot be used without QApplication
qt.qpa.plugin: Could not find the Qt platform plugin "offscreen" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.

/tmp/.mount_MuseScCxj8yw/AppRun: line 26: 394922 Aborted                 (core dumped) "${APPDIR}/bin/mscore4portable" "$@"

It looks like the offscreen plugin is not available at all in the AppImage version.

j-tai avatar Apr 14 '23 20:04 j-tai

I created a pull request (#17253) which attempts to deploy libqoffscreen.so to the AppImage. Would be great if you could try the artifacts from that PR when they are ready! (the download link will appear here: https://github.com/musescore/MuseScore/actions/runs/4713219125)

cbjeukendrup avatar Apr 16 '23 12:04 cbjeukendrup

Thanks!! I'm still having some trouble using it headlessly. It seems like MuseScore is trying to parse the -platform option as one of its own options. I think that option should be handled by Qt though.

$ unset DISPLAY
$ ./MuseScore-4.1.0.231061215-x86_64.AppImage -o out.pdf test.mscz 
/usr/lib/x86_64-linux-gnu/pipewire-0.3/jack/libjack.so.0
/lib/x86_64-linux-gnu/libnss3.so
qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: offscreen, xcb.

/tmp/.mount_MuseScolR9Aq/AppRun: line 26: 35023 Aborted                 (core dumped) "${APPDIR}/bin/mscore4portabledev" "$@"
$ ./MuseScore-4.1.0.231061215-x86_64.AppImage -platform offscreen -o out.pdf test.mscz 
/usr/lib/x86_64-linux-gnu/pipewire-0.3/jack/libjack.so.0
/lib/x86_64-linux-gnu/libnss3.so
: Unknown options: p, l, a.

j-tai avatar Apr 16 '23 16:04 j-tai

Then try to use the QT_QPA_PLATFORM environment variable instead, see https://doc.qt.io/qt-5/qguiapplication.html#supported-command-line-options

Jojo-Schmitz avatar Apr 16 '23 17:04 Jojo-Schmitz

Then try to use the QT_QPA_PLATFORM environment variable instead, see https://doc.qt.io/qt-5/qguiapplication.html#supported-command-line-options

Ah, looks like that works, thank you!!!

$ QT_QPA_PLATFORM=offscreen ./MuseScore-4.1.0.231061215-x86_64.AppImage -o out.pdf test.mscz                                                                                                   
/usr/lib/x86_64-linux-gnu/pipewire-0.3/jack/libjack.so.0
/lib/x86_64-linux-gnu/libnss3.so

j-tai avatar Apr 16 '23 17:04 j-tai

Regarding the command line option: what you say is true, the problem is that MuseScore does not know which arguments are for Qt and which not. This is a relatively recent regression.

Before, it worked correctly because we first instantiated QApplication, which removes Qt-related arguments from argv and argc.

But since recently, we need to parse the arguments ourselves before instantiating QApplication, because based on the arguments we need to instantiate either QApplication or QCoreApplication (namely in GUI mode versus command line mode).

I don't see any better solution than looking at Qt's source code to see which arguments are handled by Qt, and skip those when parsing the arguments ourselves.

cbjeukendrup avatar Apr 16 '23 17:04 cbjeukendrup

The supported commandline options are listed in https://doc.qt.io/qt-5/qguiapplication.html#supported-command-line-options

Jojo-Schmitz avatar Apr 16 '23 18:04 Jojo-Schmitz

@cbjeukendrup what is the status on this issue?

bkunda avatar May 03 '23 14:05 bkunda

I have a fix for the core of the problem of this issue, but due to a general problem with command line arguments, it is still not completely working. So let's keep this open until that general problem gets fixed, so that my fix for this issue is unblocked.

cbjeukendrup avatar May 03 '23 18:05 cbjeukendrup

Should work after # 17510

@j-tai Can you try this build please https://github.com/musescore/MuseScore/actions/runs/4925941141 ? (or the next master nightly)

Thanks!

DmitryArefiev avatar May 11 '23 10:05 DmitryArefiev

Should work after # 17510

@j-tai Can you try this build please https://github.com/musescore/MuseScore/actions/runs/4925941141 ? (or the next master nightly)

Thanks!

With that build, the -platform option gives a different error. It still tries to open the display, even though I passed -display offscreen.

$ unset DISPLAY
$ ./MuseScore-4.1.0.231291258-x86_64.AppImage -platform offscreen -v
/usr/lib/x86_64-linux-gnu/pipewire-0.3/jack/libjack.so.0
/lib/x86_64-linux-gnu/libnss3.so

(mscore4portabledev:14632): Gtk-WARNING **: 19:50:40.797: cannot open display: 
$ QT_QPA_PLATFORM=offscreen ./MuseScore-4.1.0.231291258-x86_64.AppImage -v
/usr/lib/x86_64-linux-gnu/pipewire-0.3/jack/libjack.so.0
/lib/x86_64-linux-gnu/libnss3.so
MuseScore4Development 4.1.0-dev

j-tai avatar May 12 '23 02:05 j-tai

What is the status of this issue? Would like to be able to go headless in Windows, or is that impossible?

komjum6 avatar Oct 15 '23 20:10 komjum6

As far as I know this issue was only about Ubuntu, so on Windows it should work anyway. I'm not sure what's the status on Ubuntu; there has been quite a number of attempts to fix this, but based on the latest comment apparently still something is wrong.

cbjeukendrup avatar Oct 15 '23 20:10 cbjeukendrup

As far as I know this issue was only about Ubuntu, so on Windows it should work anyway. I'm not sure what's the status on Ubuntu; there has been quite a number of attempts to fix this, but based on the latest comment apparently still something is wrong.

Right now, you can set the environment variable QT_QPA_PLATFORM=offscreen to run it headless.

Previously, you could also pass -platform offscreen to run it headless, but that no longer works in MuseScore 4.

j-tai avatar Oct 15 '23 20:10 j-tai

If I run it on windows with the -platform offscreen option I get the following:

This application failed to start because no Qt platform plugin could be initialized. Reinstalling may fix this problem. Available platform plugins are: windows.

komjum6 avatar Oct 15 '23 20:10 komjum6

I investigated a bit and it turns out that a change from our side is needed to package the qoffscreen plugin with MuseScore. I've made that change here: #19726 You can try it out by following the steps at https://github.com/musescore/MuseScore/wiki/Downloading-and-running-test-builds#downloading-builds-from-pull-requests.

cbjeukendrup avatar Oct 15 '23 22:10 cbjeukendrup

I investigated a bit and it turns out that a change from our side is needed to package the qoffscreen plugin with MuseScore. I've made that change here: #19726 You can try it out by following the steps at https://github.com/musescore/MuseScore/wiki/Downloading-and-running-test-builds#downloading-builds-from-pull-requests.

I got a new error message: This application failed to start because no Qt platform plugin could be initialized. Reinstalling may fix this problem. Available platform plugins are: offscreen, windows. (so it does now see offscreen as available plugin, but the error persists)

EDIT: Made a mistake in the commandline, seems to work!

komjum6 avatar Oct 16 '23 00:10 komjum6

EDIT: Made a mistake in the commandline, seems to work!

In that case I'll merge that PR, so that the fix will be included in 4.2!

cbjeukendrup avatar Oct 16 '23 08:10 cbjeukendrup

For MuseScore 3.6 in AppImage, I also couldn't run headless mode on Debian 12, same happened with the -platform offscreen option, any thoughts? thanks!

$ sudo apt install fuse
$ wget https://cdn.jsdelivr.net/musescore/v3.6.2/MuseScore-3.6.2.548021370-x86_64.AppImage
$ chmod u+x MuseScore-3.6.2.548021370-x86_64.AppImage

$ QT_QPA_PLATFORM=offscreen ./MuseScore-3.6.2.548021370-x86_64.AppImage
/lib/x86_64-linux-gnu/libjack.so.0
/lib/x86_64-linux-gnu/libnss3.so
This application failed to start because it could not find or load the Qt platform plugin "offscreen"
in "".

Available platform plugins are: xcb.

Reinstalling the application may fix this problem.
/tmp/.mount_MuseScykWLI8/AppRun: line 26: 495523 Aborted                 "${APPDIR}/bin/mscore-portable" "$@"

xjlin0 avatar Feb 22 '24 17:02 xjlin0

Workaround found: https://medium.com/@dinukadesilva/how-to-use-musescore-as-a-headless-command-line-tool-c3f2f0a6f224

for V3.6 or 4.2 under Linux, using xvfb instead of setting QT_QPA_PLATFORM, works like magic!

$ Xvfb -ac :99 -screen 0 1280x1024x16 &
$ export DISPLAY=:99

$ ./MuseScore-3.6.2.548021370-x86_64.AppImage -v
/lib/x86_64-linux-gnu/libjack.so.0
/lib/x86_64-linux-gnu/libnss3.so
MuseScore3 3.6.2

$ ./MuseScore-4.2.1.240230938-x86_64.AppImage -v
/lib/x86_64-linux-gnu/libjack.so.0
/lib/x86_64-linux-gnu/libnss3.so
MuseScore4 4.2.1

xjlin0 avatar Mar 06 '24 02:03 xjlin0