Skip to content
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

WindowsServiceBackend can't start. #17

Open
stephenlang84 opened this issue Oct 20, 2020 · 2 comments
Open

WindowsServiceBackend can't start. #17

stephenlang84 opened this issue Oct 20, 2020 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@stephenlang84
Copy link

Sometimes Windows can't start a backend service, the status is always Starting.
There are logs at that time.

qt.service.plugin.windows.backend: waiting for control thread...
qt.service.plugin.windows.backend: entered control thread
qt.service.plugin.windows.backend: registering service
qt.service.plugin.windows.backend: passing start arguments to main thread
qt.service.plugin.windows.backend: setting status to start pending
qt.service.backend: Running pre start service routine
qt.service.plugin.windows.backend: continuing control thread
qt.service.plugin.windows.backend: running application
qt.service.plugin.windows.backend: wait for main thread to finish startup 

And compare with a normal log.

qt.service.plugin.windows.backend: waiting for control thread...
qt.service.plugin.windows.backend: entered control thread
qt.service.plugin.windows.backend: registering service
qt.service.plugin.windows.backend: passing start arguments to main thread
qt.service.plugin.windows.backend: wait for main thread to finish startup
qt.service.plugin.windows.backend: setting status to start pending
qt.service.backend: Running pre start service routine
qt.service.plugin.windows.backend: continuing control thread
qt.service.plugin.windows.backend: running application
qt.service.plugin.windows.backend: handle service start event

Maybe the control thread is deadlock because the below line in function WindowsServiceBackend::serviceMain
_backendInstance->_startCondition.wait(&_backendInstance->_svcLock);

I tried a simple test to reproduce.
I see when runService of main thread is completed before serviceMain of control thread, a same issue is occurred.

#ifndef WORKERS_H
#define WORKERS_H
#include <QThread>
#include <QDebug>
#include <QWaitCondition>
#include <QMutexLocker>
#include <QMutex>
#include <QCoreApplication>

static QMutex _svcLock;
static QWaitCondition _startCondition;

class SvcControlThread : public QThread {
public:
    void run() {
        qDebug() << "entered control thread";
        qDebug() << "registering service";
        qDebug() << "passing start arguments to main thread";
        QMutexLocker lock(&_svcLock);
        _startCondition.wakeAll();
        lock.unlock();

        QThread::sleep(10);
        // wait for the mainthread to finish startup, then register the service handler
        lock.relock();
        qDebug() << "wait for main thread to finish startup";
        qDebug() << _startCondition.wait(&_svcLock);
        lock.unlock();

        // handle the start event
        qDebug() << "handle service start event";
    }
};

class WindowsServiceBackend {
public:
    void runService(int &argc, char **argv) {
        SvcControlThread controlThread;
        qDebug() << "waiting for control thread...";
        controlThread.start(QThread::LowestPriority);
        QMutexLocker lock(&_svcLock);
        if(!_startCondition.wait(&_svcLock, 20000))
            return;
        lock.unlock();
        qDebug() << "setting status to start pending";
        QCoreApplication app(argc, argv);

        lock.relock();
        qDebug() << "continuing control thread";
        _startCondition.wakeAll();
        lock.unlock();

        //execute the app
        qDebug() << "running application";
        app.exec();

    }
};

#endif // WORKERS_H
#include <QCoreApplication>
#include <Workers.h>

int main(int argc, char *argv[])
{
    WindowsServiceBackend wsb;
    wsb.runService(argc, argv);
}
@Skycoder42 Skycoder42 self-assigned this Oct 21, 2020
@Skycoder42 Skycoder42 added the bug Something isn't working label Oct 21, 2020
@stephenlang84
Copy link
Author

Do you have a plan to solve it ? @Skycoder42

@Skycoder42
Copy link
Owner

I am currently very busy. I might find the time someday, but don't expect a fix very soon. If you want, you can create a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants