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
129129 // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using
130130 // them. We don't need to stop scripts that aren't running.
131131 if (scriptEngine->isRunning()) {
132+ qCDebug(scriptengine) << "about to shutdown script:" << scriptName;
132133
133134 // If the script is running, but still evaluating then we need to wait for its evaluation step to
134135 // complete. After that we can handle the stop process appropriately
135136 if (scriptEngine->evaluatePending()) {
137+ qCDebug(scriptengine) << "script still evaluating:" << scriptName;
138+ auto startedWaiting = usecTimestampNow();
136139 while (scriptEngine->evaluatePending()) {
137140
138141 // This event loop allows any started, but not yet finished evaluate() calls to complete
139142 // we need to let these complete so that we can be guaranteed that the script engine isn't
140143 // in a partially setup state, which can confuse our shutdown unwinding.
141144 QEventLoop loop;
142− QObject::connect(scriptEngine, &ScriptEngine::evaluationFinished, &loop, &QEventLoop::quit);
143− loop.exec();
145+ static const int MAX_PROCESSING_TIME = 500; // in MS
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+ }
144155 }
156+ qCDebug(scriptengine) << "script DONE evaluating:" << scriptName;
145157 }
146158
147159 // We disconnect any script engine signals from the application because we don't want to do any