Results 1 to 8 of 8

Thread: MySql: my_thread_global_end(): 1 threads didn't exit

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default MySql: my_thread_global_end(): 1 threads didn't exit

    When QSqlDatabase::removeDatabase() is called from a different thread than QSqlDatabase::addDatabase() was called from, I get the following error message:
    Error in my_thread_global_end(): 1 threads didn't exit
    It is easy to reproduce with the following code sample even without MySql database. But it seems to happen on Ubuntu and NOT on Windows.

    Qt Code:
    1. #include <QtCore>
    2. #include <QtSql>
    3.  
    4. class Client: public QObject
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. Client();
    10. ~Client();
    11.  
    12. public slots:
    13. void start();
    14.  
    15. signals:
    16. void done();
    17. }; // Client
    18.  
    19. Client::Client()
    20. {
    21. QSqlDatabase::addDatabase("QMYSQL", "my_connection");
    22. }
    23.  
    24. Client::~Client()
    25. {
    26. }
    27.  
    28. void Client::start()
    29. {
    30. // we could do some work here, but for the test simply remove the connection
    31. QSqlDatabase::removeDatabase("my_connection");
    32. emit done();
    33. }
    34.  
    35.  
    36. int main(int argc, char* argv[])
    37. {
    38. QCoreApplication app(argc, argv);
    39.  
    40. Client client;
    41. client.moveToThread(&t);
    42. QObject::connect(&t, SIGNAL(started()), &client, SLOT(start()));
    43. QObject::connect(&client, SIGNAL(done()), &t, SLOT(quit()));
    44. QObject::connect(&t, SIGNAL(finished()), &app, SLOT(quit()));
    45. t.start();
    46.  
    47. return app.exec();
    48. }
    49.  
    50. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MySql: my_thread_global_end(): 1 threads didn't exit

    Quote Originally Posted by mentalmushroom View Post
    What am I doing wrong?
    You are removing the database from a wrong thread. QSqlDatabase (and friends) objects are not to be used from within different threads.

    Threads and the sql module
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: MySql: my_thread_global_end(): 1 threads didn't exit

    Ok, good to know. But I noticed the error happens when I add and remove the connection from the same thread either, but less regular.

    The issue can be noticed with the following code sample:
    Qt Code:
    1. #include <QtCore>
    2. #include <QtSql>
    3.  
    4. class Client: public QRunnable
    5. {
    6. public:
    7. virtual void run();
    8. };
    9.  
    10. void Client::run()
    11. {
    12. {
    13. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", QString::number((unsigned long long)QThread::currentThreadId()));
    14.  
    15. db.setHostName("localhost");
    16. db.setDatabaseName("test");
    17. db.setUserName("me");
    18. db.setPassword("mypass");
    19.  
    20. db.open();
    21.  
    22. //QSqlQuery q(strQuery);
    23. }
    24.  
    25. QSqlDatabase::removeDatabase(QString::number((unsigned long long)QThread::currentThreadId()));
    26.  
    27. }
    28.  
    29. int main(int argc, char *argv[])
    30. {
    31. QCoreApplication a(argc, argv);
    32.  
    33. QThreadPool pool;
    34. pool.setMaxThreadCount(100);
    35.  
    36. for (int i = 0; i < 100; ++i)
    37. {
    38. Client *client = new Client;
    39. bool started = pool.tryStart(client);
    40. Q_ASSERT(started);
    41. }
    42.  
    43. pool.waitForDone();
    44.  
    45. //return a.exec();
    46. return 0;
    47. }
    To copy to clipboard, switch view to plain text mode 

    Here goes the output of two successive runs:
    mushroom@ubuntu:~/projects/mysqltest/mysqlthread/debug$ ./mysqlthread
    Error in my_thread_global_end(): 48 threads didn't exit

    mushroom@ubuntu:~/projects/mysqltest/mysqlthread/debug$ ./mysqlthread
    Error in my_thread_global_end(): 33 threads didn't exit
    Error in my_thread_global_end(): 85 threads didn't exit

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MySql: my_thread_global_end(): 1 threads didn't exit

    Close the database first before removing it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: MySql: my_thread_global_end(): 1 threads didn't exit

    I've updated the code, so it is closed:
    Qt Code:
    1. #include <QtCore>
    2. #include <QtSql>
    3.  
    4. class Client: public QRunnable
    5. {
    6. public:
    7. virtual void run();
    8. };
    9.  
    10. void Client::run()
    11. {
    12. {
    13. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", QString::number((unsigned long long)QThread::currentThreadId()));
    14.  
    15. db.setHostName("localhost");
    16. db.setDatabaseName("test");
    17. db.setUserName("me");
    18. db.setPassword("mypass");
    19.  
    20. db.open();
    21.  
    22. db.close(); // ! close the database before removing it !
    23. }
    24.  
    25. QSqlDatabase::removeDatabase(QString::number((unsigned long long)QThread::currentThreadId()));
    26.  
    27. }
    28.  
    29. int main(int argc, char *argv[])
    30. {
    31. QCoreApplication a(argc, argv);
    32.  
    33. QThreadPool pool;
    34. pool.setMaxThreadCount(100);
    35.  
    36. for (int i = 0; i < 100; ++i)
    37. {
    38. Client *client = new Client;
    39. bool started = pool.tryStart(client);
    40. Q_ASSERT(started);
    41. }
    42.  
    43. pool.waitForDone();
    44.  
    45. return 0;
    46. }
    To copy to clipboard, switch view to plain text mode 

    The error still persists:
    mushroom@ubuntu:~/projects/mysqltest/mysqlthread/debug$ ./mysqlthread
    Error in my_thread_global_end(): 51 threads didn't exit
    Error in my_thread_global_end(): 65 threads didn't exit

  6. #6
    Join Date
    Aug 2013
    Posts
    25
    Thanks
    7
    Qt products
    Qt5

    Default Re: MySql: my_thread_global_end(): 1 threads didn't exit

    Hi,

    I have the same issue. Did you resolve your issue? If yes, could you please share your solution?

    Thank you

  7. #7
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: MySql: my_thread_global_end(): 1 threads didn't exit

    No, I didn't, but so far I can't see any obvious issues, except that warning message. However, I've reported it to the bug tracker. Vote for it, if it worries you.

  8. #8
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: MySql: my_thread_global_end(): 1 threads didn't exit

    From what I found out, there may be two causes for that:
    1. QSqlDatabase::addDatabase() and QSqlDatabase::removeDatabase() are not entirely thread-safe when using MySQL driver (maybe also when using other drivers, I didn't check them)
    2. MySQL driver doesn't clean up properly after itself when call to QSqlDatabase::open() fails.

    See my response to your bug for more details.
    The Wheel weaves as the Wheel wills.

Similar Threads

  1. Replies: 5
    Last Post: 3rd July 2014, 13:46
  2. Qt Designer QGridLayout didn't maintain ColumnStretch ratio
    By Sanje2v in forum Qt Tools
    Replies: 0
    Last Post: 3rd April 2011, 15:25
  3. MySql Database and Threads
    By moosa in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2010, 13:00
  4. Replies: 3
    Last Post: 2nd November 2010, 17:23
  5. phonon4QtmingW didn't work ??
    By Lakshmi.Bollavaram in forum Installation and Deployment
    Replies: 17
    Last Post: 18th November 2009, 16:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.