SIGSEGV in libQt6Gui.so.6 when generating book cover

Description:

Calibre crashes under some CLI applications (e.g. fetch news, ebook-convert). The root cause is that Calibre created QApplication in CLI mode which doesn't initialize the GUI subsystem of Qt.

For instance, consider the scenario of using ebook-convert to create a epub file fro a txt file. The converter automatically generates a cover for the text file via calibre.ebooks.covers::generate_cover(). During the cover generation, QFontMetrics is used to help the typesetting process. However, since ebook-convert only initializes QApplication instead of QGuiApplication, QFontconfigDatabase is not initialized, which causes QFontMetric.leading accesses a null pointer.

Additional info:

I tried to fix the problem by creating a script which calls generate_cover(). Specifically, directly calling generate_cover() crashes the python process, while creating a QGuiApplication first and calling generate_cover() next allows python to complete normally.

  • package version(s):
    • calibre 7.17.0-2
    • python 3.12.7-1
    • python-pyqt6 6.8.0dev2410211537-1
    • qt6-base 6.8.0-1
  • config and/or log files:
  • link to upstream bug report, if any: reported to the upstream calibre's Launchpad (https://bugs.launchpad.net/calibre/+bug/2085661), but rejected by Kovid. Indeed, the official release does not demonstrate this issue.

Stack

                Stack trace of thread 116171:
                #0 0x00007dbb5bbe5acf _ZNK19QFontconfigDatabase15setupFontEngineEP13QFontEngineFTRK8QFontDef (libQt6Gui.so.6 + 0x5e5acf)
                #1 0x00007dbb5bbe6315 _ZN19QFontconfigDatabase10fontEngineERK8QFontDefPv (libQt6Gui.so.6 + 0x5e6315)
                #2 0x00007dbb5b9f76fd _ZN20QFontDatabasePrivate16loadSingleEngineEiRK8QFontDefP12QtFontFamilyP13QtFontFoundryP11QtFontStyleP10QtFontSize (libQt6Gui.so.6 + 0x3f76fd)
                #3 0x00007dbb5b9f7946 _ZN20QFontDatabasePrivate10loadEngineEiRK8QFontDefP12QtFontFamilyP13QtFontFoundryP11QtFontStyleP10QtFontSize (libQt6Gui.so.6 + 0x3f7946)
                #4 0x00007dbb5ba0462a _ZN20QFontDatabasePrivate8findFontERK8QFontDefib (libQt6Gui.so.6 + 0x40462a)
                #5 0x00007dbb5ba05615 _ZN20QFontDatabasePrivate4loadEPK12QFontPrivatei (libQt6Gui.so.6 + 0x405615)
                #6 0x00007dbb5bd1811a n/a (libQt6Gui.so.6 + 0x71811a)
                #7 0x00007dbb5ba0d250 _ZNK12QFontMetrics7leadingEv (libQt6Gui.so.6 + 0x40d250)
                #8 0x00007dbb5c146bbd n/a (QtGui.abi3.so + 0x146bbd)
                #9 0x00007dbb61db1bfc n/a (libpython3.12.so.1.0 + 0x1b1bfc)
                #10 0x00007dbb61d8286b _PyObject_MakeTpCall (libpython3.12.so.1.0 + 0x18286b)
                #11 0x00007dbb61d8b5b7 _PyEval_EvalFrameDefault (libpython3.12.so.1.0 + 0x18b5b7)
                #12 0x00007dbb61d85706 _PyObject_FastCallDictTstate (libpython3.12.so.1.0 + 0x185706)
                #13 0x00007dbb61dc1d26 n/a (libpython3.12.so.1.0 + 0x1c1d26)
                #14 0x00007dbb61d82818 _PyObject_MakeTpCall (libpython3.12.so.1.0 + 0x182818)
                #15 0x00007dbb61d8b5b7 _PyEval_EvalFrameDefault (libpython3.12.so.1.0 + 0x18b5b7)
                #16 0x00007dbb61d85706 _PyObject_FastCallDictTstate (libpython3.12.so.1.0 + 0x185706)
                #17 0x00007dbb61dc20d2 _PyObject_Call_Prepend (libpython3.12.so.1.0 + 0x1c20d2)
                #18 0x00007dbb61e98b46 n/a (libpython3.12.so.1.0 + 0x298b46)
                #19 0x00007dbb61d8286b _PyObject_MakeTpCall (libpython3.12.so.1.0 + 0x18286b)
                #20 0x00007dbb61d8b5b7 _PyEval_EvalFrameDefault (libpython3.12.so.1.0 + 0x18b5b7)
                #21 0x00007dbb61e4de35 PyEval_EvalCode (libpython3.12.so.1.0 + 0x24de35)
                #22 0x00007dbb61e724aa n/a (libpython3.12.so.1.0 + 0x2724aa)
                #23 0x00007dbb61e6d24f n/a (libpython3.12.so.1.0 + 0x26d24f)
                #24 0x00007dbb61e879d4 n/a (libpython3.12.so.1.0 + 0x2879d4)
                #25 0x00007dbb61e87261 _PyRun_SimpleFileObject (libpython3.12.so.1.0 + 0x287261)
                #26 0x00007dbb61e869bf _PyRun_AnyFileObject (libpython3.12.so.1.0 + 0x2869bf)
                #27 0x00007dbb61e7f174 Py_RunMain (libpython3.12.so.1.0 + 0x27f174)
                #28 0x00007dbb61e3c5ec Py_BytesMain (libpython3.12.so.1.0 + 0x23c5ec)
                #29 0x00007dbb61a34e08 n/a (libc.so.6 + 0x25e08)
                #30 0x00007dbb61a34ecc __libc_start_main (libc.so.6 + 0x25ecc)
                #31 0x000060d35cd7a045 _start (python3.12 + 0x1045)

Steps to reproduce:

  • Run ebook-convert example.txt example.epub
  • Calibre will generate a book cover (and crash during the process)