# Buggy code
def create_app(config):
    mode = config.MODE

    if mode & App.GuiMode:
        from quamash import QEventLoop
        from PyQt5.QtGui import QIcon, QPixmap
        from PyQt5.QtWidgets import QApplication, QWidget

        q_app = QApplication(sys.argv)
        q_app.setQuitOnLastWindowClosed(True)
        q_app.setApplicationName("FeelUOwn")

        app_event_loop = QEventLoop(q_app)
        asyncio.set_event_loop(app_event_loop)

        class GuiApp(QWidget):
            mode = App.GuiMode

            def __init__(self):
                super().__init__()
                self.setObjectName("app")
                QApplication.setWindowIcon(QIcon(QPixmap(APP_ICON)))

            def closeEvent(self, e):
                self.ui.mpv_widget.close()
                event_loop = asyncio.get_event_loop()
                event_loop.stop()

        class FApp(App, GuiApp):
            def __init__(self, config):
                App.__init__(self, config)
                GuiApp.__init__(self)

    else:
        FApp = App

    Signal.setup_aio_support()
    Resolver.setup_aio_support()
    app = FApp(config)
    attach_attrs(app)
    Resolver.library = app.library
    return app


# Traceback (RuntimeError)
[2020-02-15 23:44:20,386 ERROR __init__] : Task exception was never retrieved
future: <Task finished name='Task-22' coro=<fetch_album_cover_wrapper.<locals>.fetch_album_cover() done, defined at /usr/lib/python3.8/site-packages/feeluown/containers/table.py:28> exception=RuntimeError('no running event loop')>
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/feeluown/containers/table.py", line 34, in fetch_album_cover
await asyncio.sleep(random.randrange(100) / 100)
File "/usr/lib/python3.8/asyncio/tasks.py", line 637, in sleep
loop = events.get_running_loop()
RuntimeError: no running event loop
[2020-02-15 23:44:20,387 ERROR __init__] : Task exception was never retrieved
future: <Task finished name='Task-23' coro=<fetch_album_cover_wrapper.<locals>.fetch_album_cover() done, defined at /usr/lib/python3.8/site-packages/feeluown/containers/table.py:28> exception=RuntimeError('no running event loop')>
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/feeluown/containers/table.py", line 34, in fetch_album_cover
await asyncio.sleep(random.randrange(100) / 100)
File "/usr/lib/python3.8/asyncio/tasks.py", line 637, in sleep
loop = events.get_running_loop()
RuntimeError: no running event loop
[2020-02-15 23:44:20,388 ERROR __init__] : Task exception was never retrieved
future: <Task finished name='Task-24' coro=<fetch_album_cover_wrapper.<locals>.fetch_album_cover() done, defined at /usr/lib/python3.8/site-packages/feeluown/containers/table.py:28> exception=RuntimeError('no running event loop')>
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/feeluown/containers/table.py", line 34, in fetch_album_cover
await asyncio.sleep(random.randrange(100) / 100)
File "/usr/lib/python3.8/asyncio/tasks.py", line 637, in sleep
loop = events.get_running_loop()
RuntimeError: no running event loop
[2020-02-15 23:44:20,388 ERROR __init__] : Task exception was never retrieved
future: <Task finished name='Task-25' coro=<fetch_album_cover_wrapper.<locals>.fetch_album_cover() done, defined at /usr/lib/python3.8/site-packages/feeluown/containers/table.py:28> exception=RuntimeError('no running event loop')>
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/feeluown/containers/table.py", line 34, in fetch_album_cover
await asyncio.sleep(random.randrange(100) / 100)
File "/usr/lib/python3.8/asyncio/tasks.py", line 637, in sleep
loop = events.get_running_loop()
RuntimeError: no running event loop
[2020-02-15 23:44:20,389 ERROR __init__] : Task exception was never retrieved
future: <Task finished name='Task-26' coro=<fetch_album_cover_wrapper.<locals>.fetch_album_cover() done, defined at /usr/lib/python3.8/site-packages/feeluown/containers/table.py:28> exception=RuntimeError('no running event loop')>
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/feeluown/containers/table.py", line 34, in fetch_album_cover
await asyncio.sleep(random.randrange(100) / 100)
File "/usr/lib/python3.8/asyncio/tasks.py", line 637, in sleep
loop = events.get_running_loop()
RuntimeError: no running event loop
[2020-02-15 23:44:20,389 ERROR __init__] : Task exception was never retrieved
future: <Task finished name='Task-27' coro=<fetch_album_cover_wrapper.<locals>.fetch_album_cover() done, defined at /usr/lib/python3.8/site-packages/feeluown/containers/table.py:28> exception=RuntimeError('no running event loop')>
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/feeluown/containers/table.py", line 34, in fetch_album_cover
await asyncio.sleep(random.randrange(100) / 100)
File "/usr/lib/python3.8/asyncio/tasks.py", line 637, in sleep
loop = events.get_running_loop()
RuntimeError: no running event loop
[2020-02-15 23:44:20,389 ERROR __init__] : Task exception was never retrieved
future: <Task finished name='Task-28' coro=<fetch_album_cover_wrapper.<locals>.fetch_album_cover() done, defined at /usr/lib/python3.8/site-packages/feeluown/containers/table.py:28> exception=RuntimeError('no running event loop')>
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/feeluown/containers/table.py", line 34, in fetch_album_cover
await asyncio.sleep(random.randrange(100) / 100)
File "/usr/lib/python3.8/asyncio/tasks.py", line 637, in sleep
loop = events.get_running_loop()
RuntimeError: no running event loop

# Fixed code
def create_app(config):
    mode = config.MODE

    if mode & App.GuiMode:
        from PyQt5.QtGui import QIcon, QPixmap
        from PyQt5.QtWidgets import QApplication, QWidget

        from feeluown.compat import QEventLoop

        q_app = QApplication(sys.argv)
        q_app.setQuitOnLastWindowClosed(True)
        q_app.setApplicationName("FeelUOwn")

        app_event_loop = QEventLoop(q_app)
        asyncio.set_event_loop(app_event_loop)

        class GuiApp(QWidget):
            mode = App.GuiMode

            def __init__(self):
                super().__init__()
                self.setObjectName("app")
                QApplication.setWindowIcon(QIcon(QPixmap(APP_ICON)))

            def closeEvent(self, e):
                self.ui.mpv_widget.close()
                event_loop = asyncio.get_event_loop()
                event_loop.stop()

        class FApp(App, GuiApp):
            def __init__(self, config):
                App.__init__(self, config)
                GuiApp.__init__(self)

    else:
        FApp = App

    Signal.setup_aio_support()
    Resolver.setup_aio_support()
    app = FApp(config)
    attach_attrs(app)
    Resolver.library = app.library
    return app