History

add support to abort evaluation of long running scripts on shutdown

Brad Hefta-Gaub committed 10 years ago

Commit a21bc661 parent 28b5ef62 files · +32 −3Browse files

Showing the changes to libraries/script-engine/src/ScriptEngines.cpp (1 of 2 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngines.cpp+14−2
@@ -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 using129 // "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 to134 // 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 appropriately135 // 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 complete141 // 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't142 // 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 any159 // We disconnect any script engine signals from the application because we don't want to do any