aboutsummaryrefslogtreecommitdiffstats
path: root/sources
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-06-01 12:36:33 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-06-03 18:47:34 +0200
commite141ea41cf6f2840281ab6e52b0861a6d1dcc137 (patch)
treec91285c554112b8bb07796d2aa5ae91f05a6ccd9 /sources
parentdf8e46c33e517688ff341cffd4a2cd648a4b5255 (diff)
libshiboken: Fix occasional crashes when using QML
QML calls into the generated QObject::metaObject() from threads without GIL, causing crashes for example in retrieveWrapper(). Use a mutex to guard access. Pick-to: 6.3 6.2 5.15 Change-Id: I374ada7fc207d86a062f950751503764a5e5dddf Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources')
-rw-r--r--sources/shiboken6/libshiboken/bindingmanager.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/sources/shiboken6/libshiboken/bindingmanager.cpp b/sources/shiboken6/libshiboken/bindingmanager.cpp
index b283fe0f7..4ae6be455 100644
--- a/sources/shiboken6/libshiboken/bindingmanager.cpp
+++ b/sources/shiboken6/libshiboken/bindingmanager.cpp
@@ -13,6 +13,7 @@
#include <cstddef>
#include <fstream>
+#include <mutex>
#include <unordered_map>
namespace Shiboken
@@ -105,6 +106,11 @@ struct BindingManager::BindingManagerPrivate {
using DestructorEntries = std::vector<DestructorEntry>;
WrapperMap wrapperMapper;
+ // Guard wrapperMapper mainly for QML which calls into the generated
+ // QObject::metaObject() and elsewhere from threads without GIL, causing
+ // crashes for example in retrieveWrapper(). std::shared_mutex was rejected due to:
+ // https://stackoverflow.com/questions/50972345/when-is-stdshared-timed-mutex-slower-than-stdmutex-and-when-not-to-use-it
+ std::mutex wrapperMapLock;
Graph classHierarchy;
DestructorEntries deleteInMainThread;
bool destroying;
@@ -120,6 +126,7 @@ bool BindingManager::BindingManagerPrivate::releaseWrapper(void *cptr, SbkObject
// The wrapper argument is checked to ensure that the correct wrapper is released.
// Returns true if the correct wrapper is found and released.
// If wrapper argument is NULL, no such check is performed.
+ std::lock_guard<std::mutex> guard(wrapperMapLock);
auto iter = wrapperMapper.find(cptr);
if (iter != wrapperMapper.end() && (wrapper == nullptr || iter->second == wrapper)) {
wrapperMapper.erase(iter);
@@ -131,6 +138,7 @@ bool BindingManager::BindingManagerPrivate::releaseWrapper(void *cptr, SbkObject
void BindingManager::BindingManagerPrivate::assignWrapper(SbkObject *wrapper, const void *cptr)
{
assert(cptr);
+ std::lock_guard<std::mutex> guard(wrapperMapLock);
auto iter = wrapperMapper.find(cptr);
if (iter == wrapperMapper.end())
wrapperMapper.insert(std::make_pair(cptr, wrapper));
@@ -157,6 +165,7 @@ BindingManager::~BindingManager()
* the BindingManager is being destroyed the interpreter is alredy
* shutting down. */
if (Py_IsInitialized()) { // ensure the interpreter is still valid
+ std::lock_guard<std::mutex> guard(m_d->wrapperMapLock);
while (!m_d->wrapperMapper.empty()) {
Object::destroy(m_d->wrapperMapper.begin()->second, const_cast<void *>(m_d->wrapperMapper.begin()->first));
}
@@ -172,6 +181,7 @@ BindingManager &BindingManager::instance() {
bool BindingManager::hasWrapper(const void *cptr)
{
+ std::lock_guard<std::mutex> guard(m_d->wrapperMapLock);
return m_d->wrapperMapper.find(cptr) != m_d->wrapperMapper.end();
}
@@ -232,6 +242,7 @@ void BindingManager::addToDeletionInMainThread(const DestructorEntry &e)
SbkObject *BindingManager::retrieveWrapper(const void *cptr)
{
+ std::lock_guard<std::mutex> guard(m_d->wrapperMapLock);
auto iter = m_d->wrapperMapper.find(cptr);
if (iter == m_d->wrapperMapper.end())
return nullptr;
@@ -341,6 +352,7 @@ PyTypeObject *BindingManager::resolveType(void **cptr, PyTypeObject *type)
std::set<PyObject *> BindingManager::getAllPyObjects()
{
std::set<PyObject *> pyObjects;
+ std::lock_guard<std::mutex> guard(m_d->wrapperMapLock);
const WrapperMap &wrappersMap = m_d->wrapperMapper;
auto it = wrappersMap.begin();
for (; it != wrappersMap.end(); ++it)