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

    • Type: Bug
    • Status: Closed
    • Priority: P2: Important
    • Resolution: Done
    • Affects Version/s: 5.15.2
    • Fix Version/s: 6.2.0 Beta3
    • Component/s: Core: Date/Time
    • Labels:
      None

      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

              Assignee:
              ievgenii.meshcheriakov Ievgenii Meshcheriakov
              Reporter:
              alexium Alex
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:

                  Gerrit Reviews

                  There are no open Gerrit changes