Skip to content

Commit

Permalink
Merge pull request #853 from overte-org/fix/script_deadlocks
Browse files Browse the repository at this point in the history
Move helper script engines to their own threads
  • Loading branch information
HifiExperiments authored Apr 30, 2024
2 parents ed88ab5 + 268de19 commit 46787ee
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 59 deletions.
27 changes: 20 additions & 7 deletions assignment-client/src/avatars/ScriptableAvatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <NetworkingConstants.h>


ScriptableAvatar::ScriptableAvatar(): _scriptEngine(newScriptEngine()) {
ScriptableAvatar::ScriptableAvatar() {
_clientTraitsHandler.reset(new ClientTraitsHandler(this));
static std::once_flag once;
std::call_once(once, [] {
Expand Down Expand Up @@ -344,7 +344,9 @@ AvatarEntityMap ScriptableAvatar::getAvatarEntityDataInternal(bool allProperties
EntityItemProperties properties = entity->getProperties(desiredProperties);

QByteArray blob;
EntityItemProperties::propertiesToBlob(*_scriptEngine, sessionID, properties, blob, allProperties);
_helperScriptEngine.run( [&] {
EntityItemProperties::propertiesToBlob(*_helperScriptEngine.get(), sessionID, properties, blob, allProperties);
});
data[id] = blob;
}
});
Expand All @@ -368,8 +370,12 @@ void ScriptableAvatar::setAvatarEntityData(const AvatarEntityMap& avatarEntityDa
while (dataItr != avatarEntityData.end()) {
EntityItemProperties properties;
const QByteArray& blob = dataItr.value();
if (!blob.isNull() && EntityItemProperties::blobToProperties(*_scriptEngine, blob, properties)) {
newProperties[dataItr.key()] = properties;
if (!blob.isNull()) {
_helperScriptEngine.run([&] {
if (EntityItemProperties::blobToProperties(*_helperScriptEngine.get(), blob, properties)) {
newProperties[dataItr.key()] = properties;
}
});
}
++dataItr;
}
Expand Down Expand Up @@ -448,9 +454,16 @@ void ScriptableAvatar::updateAvatarEntity(const QUuid& entityID, const QByteArra

EntityItemPointer entity;
EntityItemProperties properties;
if (!EntityItemProperties::blobToProperties(*_scriptEngine, entityData, properties)) {
// entityData is corrupt
return;
{
// TODO: checking how often this happens and what is the performance impact of having the script engine on separate thread
// If it's happening often, a method to move HelperScriptEngine into the current thread would be a good idea
bool result = _helperScriptEngine.runWithResult<bool> ( [&]() {
return EntityItemProperties::blobToProperties(*_helperScriptEngine.get(), entityData, properties);
});
if (!result) {
// entityData is corrupt
return;
}
}

std::map<QUuid, EntityItemPointer>::iterator itr = _entities.find(entityID);
Expand Down
3 changes: 2 additions & 1 deletion assignment-client/src/avatars/ScriptableAvatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <EntityItem.h>
#include "model-networking/ModelCache.h"
#include "Rig.h"
#include <HelperScriptEngine.h>

/*@jsdoc
* The <code>Avatar</code> API is used to manipulate scriptable avatars on the domain. This API is a subset of the
Expand Down Expand Up @@ -228,7 +229,7 @@ public slots:
QHash<QString, int> _fstJointIndices; ///< 1-based, since zero is returned for missing keys
QStringList _fstJointNames; ///< in order of depth-first traversal
QUrl _skeletonModelFilenameURL; // This contains URL from filename field in fst file
mutable ScriptEnginePointer _scriptEngine;
mutable HelperScriptEngine _helperScriptEngine;
std::map<QUuid, EntityItemPointer> _entities;

/// Loads the joint indices, names from the FST file (if any)
Expand Down
42 changes: 19 additions & 23 deletions interface/src/avatar/MyAvatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1740,10 +1740,11 @@ void MyAvatar::handleChangedAvatarEntityData() {
blobFailed = true; // blob doesn't exist
return;
}
std::lock_guard<std::mutex> guard(_scriptEngineLock);
if (!EntityItemProperties::blobToProperties(*_scriptEngine, itr.value(), properties)) {
blobFailed = true; // blob is corrupt
}
_helperScriptEngine.run( [&] {
if (!EntityItemProperties::blobToProperties(*_helperScriptEngine.get(), itr.value(), properties)) {
blobFailed = true; // blob is corrupt
}
});
});
if (blobFailed) {
// remove from _cachedAvatarEntityBlobUpdatesToSkip just in case:
Expand Down Expand Up @@ -1776,10 +1777,11 @@ void MyAvatar::handleChangedAvatarEntityData() {
skip = true;
return;
}
std::lock_guard<std::mutex> guard(_scriptEngineLock);
if (!EntityItemProperties::blobToProperties(*_scriptEngine, itr.value(), properties)) {
skip = true;
}
_helperScriptEngine.run( [&] {
if (!EntityItemProperties::blobToProperties(*_helperScriptEngine.get(), itr.value(), properties)) {
skip = true;
}
});
});
if (!skip && canRezAvatarEntites) {
sanitizeAvatarEntityProperties(properties);
Expand Down Expand Up @@ -1884,10 +1886,9 @@ bool MyAvatar::updateStaleAvatarEntityBlobs() const {
if (found) {
++numFound;
QByteArray blob;
{
std::lock_guard<std::mutex> guard(_scriptEngineLock);
EntityItemProperties::propertiesToBlob(*_scriptEngine, getID(), properties, blob);
}
_helperScriptEngine.run( [&] {
EntityItemProperties::propertiesToBlob(*_helperScriptEngine.get(), getID(), properties, blob);
});
_avatarEntitiesLock.withWriteLock([&] {
_cachedAvatarEntityBlobs[id] = blob;
});
Expand Down Expand Up @@ -1948,10 +1949,9 @@ AvatarEntityMap MyAvatar::getAvatarEntityData() const {
EntityItemProperties properties = entity->getProperties(desiredProperties);

QByteArray blob;
{
std::lock_guard<std::mutex> guard(_scriptEngineLock);
EntityItemProperties::propertiesToBlob(*_scriptEngine, getID(), properties, blob, true);
}
_helperScriptEngine.run( [&] {
EntityItemProperties::propertiesToBlob(*_helperScriptEngine.get(), getID(), properties, blob, true);
});

data[entityID] = blob;
}
Expand Down Expand Up @@ -2093,9 +2093,6 @@ void MyAvatar::avatarEntityDataToJson(QJsonObject& root) const {
}

void MyAvatar::loadData() {
if (!_scriptEngine) {
_scriptEngine = newScriptEngine();
}
getHead()->setBasePitch(_headPitchSetting.get());

_yawSpeed = _yawSpeedSetting.get(_yawSpeed);
Expand Down Expand Up @@ -2704,11 +2701,10 @@ QVariantList MyAvatar::getAvatarEntitiesVariant() {
QVariantMap avatarEntityData;
avatarEntityData["id"] = entityID;
EntityItemProperties entityProperties = entity->getProperties(desiredProperties);
{
std::lock_guard<std::mutex> guard(_scriptEngineLock);
ScriptValue scriptProperties = EntityItemPropertiesToScriptValue(_scriptEngine.get(), entityProperties);
_helperScriptEngine.run( [&] {
ScriptValue scriptProperties = EntityItemPropertiesToScriptValue(_helperScriptEngine.get(), entityProperties);
avatarEntityData["properties"] = scriptProperties.toVariant();
}
});
avatarEntitiesData.append(QVariant(avatarEntityData));
}
}
Expand Down
7 changes: 5 additions & 2 deletions interface/src/avatar/MyAvatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <controllers/Pose.h>
#include <controllers/Actions.h>
#include <EntityItem.h>
#include <HelperScriptEngine.h>
#include <ThreadSafeValueCache.h>
#include <Rig.h>
#include <SettingHandle.h>
Expand Down Expand Up @@ -3102,8 +3103,10 @@ private slots:
mutable std::set<EntityItemID> _staleCachedAvatarEntityBlobs;
//
// keep a ScriptEngine around so we don't have to instantiate on the fly (these are very slow to create/delete)
mutable std::mutex _scriptEngineLock;
ScriptEnginePointer _scriptEngine { nullptr };
// TODO: profile if it performs better when script engine is on avatar thread or on its own thread
// Own thread is safer from deadlocks
mutable HelperScriptEngine _helperScriptEngine;

bool _needToSaveAvatarEntitySettings { false };

bool _reactionTriggers[NUM_AVATAR_TRIGGER_REACTIONS] { false, false };
Expand Down
19 changes: 11 additions & 8 deletions libraries/baking/src/MaterialBaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ MaterialBaker::MaterialBaker(const QString& materialData, bool isURL, const QStr
_isURL(isURL),
_destinationPath(destinationPath),
_bakedOutputDir(bakedOutputDir),
_textureOutputDir(bakedOutputDir + "/materialTextures/" + QString::number(materialNum++)),
_scriptEngine(newScriptEngine())
_textureOutputDir(bakedOutputDir + "/materialTextures/" + QString::number(materialNum++))
{
}

Expand Down Expand Up @@ -214,16 +213,20 @@ void MaterialBaker::outputMaterial() {
if (_materialResource->parsedMaterials.networkMaterials.size() == 1) {
auto networkMaterial = _materialResource->parsedMaterials.networkMaterials.begin();
auto scriptableMaterial = scriptable::ScriptableMaterial(networkMaterial->second);
QVariant materialVariant =
scriptable::scriptableMaterialToScriptValue(_scriptEngine.get(), scriptableMaterial).toVariant();
json.insert("materials", QJsonDocument::fromVariant(materialVariant).object());
_helperScriptEngine.run( [&] {
QVariant materialVariant =
scriptable::scriptableMaterialToScriptValue(_helperScriptEngine.get(), scriptableMaterial).toVariant();
json.insert("materials", QJsonDocument::fromVariant(materialVariant).object());
});
} else {
QJsonArray materialArray;
for (auto networkMaterial : _materialResource->parsedMaterials.networkMaterials) {
auto scriptableMaterial = scriptable::ScriptableMaterial(networkMaterial.second);
QVariant materialVariant =
scriptable::scriptableMaterialToScriptValue(_scriptEngine.get(), scriptableMaterial).toVariant();
materialArray.append(QJsonDocument::fromVariant(materialVariant).object());
_helperScriptEngine.run( [&] {
QVariant materialVariant =
scriptable::scriptableMaterialToScriptValue(_helperScriptEngine.get(), scriptableMaterial).toVariant();
materialArray.append(QJsonDocument::fromVariant(materialVariant).object());
});
}
json.insert("materials", materialArray);
}
Expand Down
3 changes: 2 additions & 1 deletion libraries/baking/src/MaterialBaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "TextureBaker.h"
#include "baking/TextureFileNamer.h"

#include <HelperScriptEngine.h>
#include <procedural/ProceduralMaterialCache.h>
#include <ScriptEngine.h>

Expand Down Expand Up @@ -72,7 +73,7 @@ private slots:
QString _textureOutputDir;
QString _bakedMaterialData;

ScriptEnginePointer _scriptEngine;
HelperScriptEngine _helperScriptEngine;
static std::function<QThread*()> _getNextOvenWorkerThreadOperator;
TextureFileNamer _textureFileNamer;

Expand Down
27 changes: 12 additions & 15 deletions libraries/entities/src/EntityTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2553,11 +2553,10 @@ bool EntityTree::writeToMap(QVariantMap& entityDescription, OctreeElementPointer
}
entityDescription["DataVersion"] = _persistDataVersion;
entityDescription["Id"] = _persistID;
const std::lock_guard<std::mutex> scriptLock(scriptEngineMutex);
RecurseOctreeToMapOperator theOperator(entityDescription, element, scriptEngine.get(), skipDefaultValues,
skipThoseWithBadParents, _myAvatar);
withReadLock([&] {
recurseTreeWithOperator(&theOperator);
_helperScriptEngine.run( [&] {
RecurseOctreeToMapOperator theOperator(entityDescription, element, _helperScriptEngine.get(), skipDefaultValues,
skipThoseWithBadParents, _myAvatar);
withReadLock([&] { recurseTreeWithOperator(&theOperator); });
});
return true;
}
Expand Down Expand Up @@ -2728,11 +2727,10 @@ bool EntityTree::readFromMap(QVariantMap& map, const bool isImport) {
}

EntityItemProperties properties;
{
const std::lock_guard<std::mutex> scriptLock(scriptEngineMutex);
ScriptValue entityScriptValue = variantMapToScriptValue(entityMap, *scriptEngine);
_helperScriptEngine.run( [&] {
ScriptValue entityScriptValue = variantMapToScriptValue(entityMap, *_helperScriptEngine.get());
EntityItemPropertiesFromScriptValueIgnoreReadOnly(entityScriptValue, properties);
}
});

EntityItemID entityItemID;
if (entityMap.contains("id")) {
Expand Down Expand Up @@ -2881,13 +2879,12 @@ bool EntityTree::readFromMap(QVariantMap& map, const bool isImport) {
}

bool EntityTree::writeToJSON(QString& jsonString, const OctreeElementPointer& element) {
const std::lock_guard<std::mutex> scriptLock(scriptEngineMutex);
RecurseOctreeToJSONOperator theOperator(element, scriptEngine.get(), jsonString);
withReadLock([&] {
recurseTreeWithOperator(&theOperator);
});
_helperScriptEngine.run( [&] {
RecurseOctreeToJSONOperator theOperator(element, _helperScriptEngine.get(), jsonString);
withReadLock([&] { recurseTreeWithOperator(&theOperator); });

jsonString = theOperator.getJson();
jsonString = theOperator.getJson();
});
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions libraries/entities/src/EntityTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <QSet>
#include <QVector>

#include <HelperScriptEngine.h>
#include <Octree.h>
#include <SpatialParentFinder.h>

Expand Down Expand Up @@ -387,8 +388,7 @@ class EntityTree : public Octree, public SpatialParentTree {
MovingEntitiesOperator& moveOperator, bool force, bool tellServer);

// Script engine for writing entity tree data to and from JSON
std::mutex scriptEngineMutex;
ScriptEnginePointer scriptEngine{ newScriptEngine() };
HelperScriptEngine _helperScriptEngine;
};

void convertGrabUserDataToProperties(EntityItemProperties& properties);
Expand Down
31 changes: 31 additions & 0 deletions libraries/script-engine/src/HelperScriptEngine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// HelperScriptEngine.h
// libraries/script-engine/src/HelperScriptEngine.h
//
// Created by dr Karol Suprynowicz on 2024/04/28.
// Copyright 2024 Overte e.V.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//

#include "HelperScriptEngine.h"

HelperScriptEngine::HelperScriptEngine() {
std::lock_guard<std::mutex> lock(_scriptEngineLock);
_scriptEngine = newScriptEngine();
_scriptEngineThread.reset(new QThread());
_scriptEngine->setThread(_scriptEngineThread.get());
_scriptEngineThread->start();
}

HelperScriptEngine::~HelperScriptEngine() {
std::lock_guard<std::mutex> lock(_scriptEngineLock);
if (_scriptEngine) {
if (_scriptEngineThread) {
_scriptEngineThread->quit();
_scriptEngineThread->wait();
}
_scriptEngine.reset();
}
}
65 changes: 65 additions & 0 deletions libraries/script-engine/src/HelperScriptEngine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// HelperScriptEngine.h
// libraries/script-engine/src/HelperScriptEngine.h
//
// Created by dr Karol Suprynowicz on 2024/04/28.
// Copyright 2024 Overte e.V.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//

#ifndef overte_HelperScriptEngine_h
#define overte_HelperScriptEngine_h

#include <mutex>
#include "QThread"

#include "ScriptEngine.h"

/**
* @brief Provides a wrapper around script engine that does not have ScriptManager
*
* HelperScriptEngine is used for performing smaller tasks, like for example conversions between entity
* properties and JSON data.
* For thread safety all accesses to helper script engine need to be done either through HelperScriptEngine::run()
* or HelperScriptEngine::runWithResult().
*
*/


class HelperScriptEngine {
public:
HelperScriptEngine();
~HelperScriptEngine();

template <typename F>
inline void run(F&& f) {
std::lock_guard<std::mutex> guard(_scriptEngineLock);
f();
}

template <typename T, typename F>
inline T runWithResult(F&& f) {
T result;
{
std::lock_guard<std::mutex> guard(_scriptEngineLock);
result = f();
}
return result;
}

/**
* @brief Returns pointer to the script engine
*
* This function should be used only inside HelperScriptEngine::run() or HelperScriptEngine::runWithResult()
*/
ScriptEngine* get() { return _scriptEngine.get(); };
ScriptEnginePointer getShared() { return _scriptEngine; };
private:
std::mutex _scriptEngineLock;
ScriptEnginePointer _scriptEngine { nullptr };
std::shared_ptr<QThread> _scriptEngineThread { nullptr };
};

#endif //overte_HelperScriptEngine_h

0 comments on commit 46787ee

Please sign in to comment.