Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-93004

Assert and crash in qcalendar.cpp when the first call to QDateTime::toString is done from two simultaneous threads

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 6.2.0 Beta3
    • 5.15.2
    • Core: Date/Time
    • None
    • Windows
    • 21
    • d0ae1ef33a6eed02acde7304298794f4f0119e16 (qt/qtbase/dev) 93345662b05359e0b895faf2be0f2358af08339b (qt/qtbase/6.2)
    • Qt6_Foundation_Sprint 38, Qt6_Foundation_Sprint 39

    Description

      The following code reproduces the assert (in debug mode) and the following crash. Tested on Windows with msvc (5.15.2\msvc2019_64). I've also found a workaround, which might hint at the nature of the problem.

      If this is unavoidable behavior then I suggest it should be documented, but I hope that a transparent fix can be worked out in the implementation itself to gracefully handle multi-threaded access.

      #include <QDateTime>
      #include <atomic>
      #include <thread>
      
      static std::atomic_bool g_startThreads = false;
      
      static void thread_func() {
      	for (; !g_startThreads; );
      	const auto date = QDateTime::currentDateTimeUtc();
      	date.toString("yyyyMMdd_hhmmss");
      }
      
      int main(int, char*[]) {
      	std::vector<std::thread> threads;
      	for (int i = 0; i < 2; ++i)
      		threads.emplace_back(&thread_func);
      
      	// The following line fixes the problem (presumably, by performing orderly first initialization of the internal Qt structures used for QDateTime::toString()
      	//QDateTime::currentDateTimeUtc().toString("yyyyMMdd_hhmmss");
      
      	g_startThreads = true;
      
      	for (auto& th: threads)
      		th.join();
      
      	return 0;
      }
      

      Here's the assertion triggered by this demo:

      Program: E:\Development\Tools\Qt\5.15.2\msvc2019_64\bin\Qt5Cored.dll
      Module: 5.15.2
      File: time\qcalendar.cpp
      Line: 105
      
      ASSERT: "byId[size_t(id)] == nullptr" in file time\qcalendar.cpp, line 105
      

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              ievgenii.meshcheriakov Ievgenii Meshcheriakov
              alexium Alex
              Vladimir Minenko Vladimir Minenko
              Alex Blasche Alex Blasche
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes