Showing the changes to libraries/script-engine/src/ScriptEngines.cpp (1 of 2 files). Show all
Unified · Split
| @@ -129,19 +129,31 @@ void ScriptEngines::shutdownScripting() { · 128 unmodified lines | |||
| 129 | // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using | 129 | // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using |
| 130 | // them. We don't need to stop scripts that aren't running. | 130 | // them. We don't need to stop scripts that aren't running. |
| 131 | if (scriptEngine->isRunning()) { | 131 | if (scriptEngine->isRunning()) { |
| 132 | qCDebug(scriptengine) << "about to shutdown script:" << scriptName; | ||
| 132 | 133 | ||
| 133 | // If the script is running, but still evaluating then we need to wait for its evaluation step to | 134 | // If the script is running, but still evaluating then we need to wait for its evaluation step to |
| 134 | // complete. After that we can handle the stop process appropriately | 135 | // complete. After that we can handle the stop process appropriately |
| 135 | if (scriptEngine->evaluatePending()) { | 136 | if (scriptEngine->evaluatePending()) { |
| 137 | qCDebug(scriptengine) << "script still evaluating:" << scriptName; | ||
| 138 | auto startedWaiting = usecTimestampNow(); | ||
| 136 | while (scriptEngine->evaluatePending()) { | 139 | while (scriptEngine->evaluatePending()) { |
| 137 | 140 | ||
| 138 | // This event loop allows any started, but not yet finished evaluate() calls to complete | 141 | // This event loop allows any started, but not yet finished evaluate() calls to complete |
| 139 | // we need to let these complete so that we can be guaranteed that the script engine isn't | 142 | // we need to let these complete so that we can be guaranteed that the script engine isn't |
| 140 | // in a partially setup state, which can confuse our shutdown unwinding. | 143 | // in a partially setup state, which can confuse our shutdown unwinding. |
| 141 | QEventLoop loop; | 144 | QEventLoop loop; |
| 142 | QObject::connect(scriptEngine, &ScriptEngine::evaluationFinished, &loop, &QEventLoop::quit); | 145 | static const int MAX_PROCESSING_TIME = 500; // in MS |
| 143 | loop.exec(); | 146 | loop.processEvents(QEventLoop::AllEvents, MAX_PROCESSING_TIME); |
| 147 | auto stillWaiting = usecTimestampNow(); | ||
| 148 | auto elapsed = stillWaiting - startedWaiting; | ||
| 149 | // if we've been waiting for more than 5 seconds, then tell the script engine to stop evaluating | ||
| 150 | static const auto WAITING_TOO_LONG = USECS_PER_SECOND * 5; | ||
| 151 | if (elapsed > WAITING_TOO_LONG) { | ||
| 152 | qCDebug(scriptengine) << "giving up on script evaluation elapsed:" << elapsed << "calling abortEvaluation() script:" << scriptName; | ||
| 153 | scriptEngine->abortEvaluation(); | ||
| 154 | } | ||
| 144 | } | 155 | } |
| 156 | qCDebug(scriptengine) << "script DONE evaluating:" << scriptName; | ||
| 145 | } | 157 | } |
| 146 | 158 | ||
| 147 | // We disconnect any script engine signals from the application because we don't want to do any | 159 | // We disconnect any script engine signals from the application because we don't want to do any |