From 6dbb95e69ced3682500b4ecdbbfb01e5b9145cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Wed, 12 Feb 2025 13:08:24 +0100 Subject: [PATCH] QNetworkInfo: Document threading restriction Specifically on Windows we use COM, which requires being de-initialized in the same thread as it was initialized. Given that QNetworkInfo is a GLOBAL_STATIC with a qAddPostCondition (iirc APPLICATION_STATIC was not yet implemented at the time) to delete the backend when qApp is destructed, we need to cope with destruction happening on the main thread. If the backend is loaded on a secondary thread, the thread may have shut down, or we may not otherwise be able to rely on emitting Queued emissions to perform the destruction. Conversely we also can't trivially move the construction to the application thread (eg. using BlockingQueued emission during first load) because we may cause a deadlock. This leaves us with just documenting where the first load should happen. Fixes: QTBUG-133644 Pick-to: 6.8 6.5 Change-Id: I7f52e884151c6c24acc34f1112faabc897d9b0f9 Reviewed-by: Thiago Macieira (cherry picked from commit 6ad2918de4a300d11472edcafe62f07ae2f63cd8) Reviewed-by: Qt Cherry-pick Bot --- src/network/kernel/qnetworkinformation.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/network/kernel/qnetworkinformation.cpp b/src/network/kernel/qnetworkinformation.cpp index 5115372b207..ab5d6621a22 100644 --- a/src/network/kernel/qnetworkinformation.cpp +++ b/src/network/kernel/qnetworkinformation.cpp @@ -419,6 +419,12 @@ QNetworkInformationBackendFactory::~QNetworkInformationBackendFactory() If you destroy and re-create the QCoreApplication object you must call load() again. + \note Because the class is a singleton while also relying on + QCoreApplication, QNetworkInformation should always first be loaded + in the same thread as the QCoreApplication object. This is because the + object will also be destroyed in this thread, and various backend-specific + components may rely on being destroyed in the same thread as it is created. + \sa QNetworkInformation::Feature */