nymea / nymea-app

A nymea frontend app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A potential issue in AppLogController

stepanove opened this issue · comments

Hello!
I was browsing your code and I noticed one potential issue that I’ve faced once myself. I used similar implementation to display debug output in QML ListView item and got very rare spontaneous crashes without any visible reasons, the log was useless and even misleading in this case.

The problem was in a message handler, similar to your AppLogController::append. It’s not thread safe and since this message handler is executed in the thread that generates the message, you might get race condition on your model if you have qDebug or friends in different threads.

Oh, interesting... Thanks for the hint! I do the same in nymea core. Haven't noticed any crashes which could be related to this, but it sure does sound like your reasoning makes sense. I'll have to look at this.

It's not easy to catch in the wild, but quite easy to reproduce with something like this:
QThreadPool p;
p.setMaxThreadCount(10);
QtConcurrent::run(&p,&{while(true){QThread::msleep(1); qDebug()<<"";}});
QtConcurrent::run(&p,&{while(true){QThread::msleep(2); qDebug()<<"";}});
QtConcurrent::run(&p,&{while(true){QThread::msleep(3); qDebug()<<"";}});

@stepanove thanks again for reporting this. I could reproduce it with your snippet. Great catch!