Files
OrcaSlicer/src/slic3r/GUI/IdleScheduler.cpp
T
HMITCHR 3610d8bb35 Do not prebuild pages while a modal dialog is open (#16994)
On macOS the app could hang at 100% CPU when a project with customized
presets was opened into a running app and the message box was closed
while the idle prebuild was building the Project page's web view. The
web view waits for WebKit in a wxYield() loop, and closing the dialog
aborts the modal session, so the reply never arrives.

The idle scheduler now runs a slice only in the main event loop, so no
pages are built while a modal dialog is open. Building resumes when the
dialog closes.
2026-10-29 14:43:38 -05:00

113 lines
3.4 KiB
C++

#include "IdleScheduler.hpp"
#include <chrono>
#include <boost/log/trivial.hpp>
#include <functional>
#include <utility>
#include <string>
#include <wx/evtloop.h>
#include <wx/timer.h>
#include "libslic3r/Utils.hpp"
#include "slic3r/GUI/PrebuildQueue.hpp"
#ifdef _WIN32
#include <windows.h>
#endif
namespace Slic3r { namespace GUI {
namespace {
// The timer's period while waiting for the user to go quiet.
constexpr int tick_ms = 250;
// Input-free time before a slice may start, about a double-click interval, so a user
// mid-gesture is left alone.
constexpr int quiet_ms = 500;
// Budget of one slice. The next slice is a timer message, so paint, timers and input
// queued meanwhile are handled first; a click waits at most a slice plus the unit that
// overran it.
constexpr int slice_ms = 40;
// Delay before the next slice; on GTK a due timer runs ahead of repaints and posted events,
// and wxOSX rejects a 0 ms timer.
constexpr int next_slice_ms = 5;
// True when unhandled keyboard, button, touch or pen input is queued; only Windows can ask.
bool input_pending()
{
#ifdef _WIN32
// Windows synthesizes a mouse move whenever a window appears under the cursor, so those
// do not count.
return (GetQueueStatus(QS_INPUT & ~QS_MOUSEMOVE) >> 16) != 0;
#else
return false;
#endif
}
// True inside a wxYield() or a nested loop such as a modal dialog's, where a slice would build pages in
// the middle of the code that is waiting. A new web view waits for WebKit in a yield loop; if the dialog
// closes meanwhile, macOS aborts its modal session, the reply never arrives and the app hangs.
bool in_nested_loop()
{
const wxEventLoopBase* loop = wxEventLoopBase::GetActive();
return loop != nullptr && (loop->IsYielding() || !loop->IsMain());
}
} // namespace
IdleScheduler::IdleScheduler(std::function<int()> input_idle_ms) : m_input_idle_ms(std::move(input_idle_ms))
{
m_timer.Bind(wxEVT_TIMER, [this](wxTimerEvent&) { tick(); });
}
void IdleScheduler::start()
{
if (!m_timer.IsRunning())
m_timer.Start(tick_ms);
}
void IdleScheduler::stop()
{
m_timer.Stop();
}
void IdleScheduler::tick()
{
// A unit that pumps the event loop lets the timer fire inside its own slice.
if (m_in_slice)
return;
if (!m_queue.pending()) {
stop();
return;
}
if (m_input_idle_ms() < quiet_ms || input_pending() || in_nested_loop()) {
start();
return;
}
const auto now_ms = [] {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
};
const char* const fn = __FUNCTION__;
m_in_slice = true;
PrebuildQueue::Slice slice;
{
ScopeGuard in_slice([this] { m_in_slice = false; });
slice = m_queue.run_slice(slice_ms, now_ms, input_pending, [fn](const std::string& name, long long ms) {
BOOST_LOG_TRIVIAL(debug) << fn << ": " << name << ": unit took " << ms << " ms";
});
}
if (slice.completed)
BOOST_LOG_TRIVIAL(info) << fn << ": task complete: " << slice.name << ", " << slice.units << " unit(s) in " << slice.ms << " ms this slice";
else
BOOST_LOG_TRIVIAL(debug) << fn << ": " << slice.name << ": " << slice.units << " unit(s) in " << slice.ms << " ms, yielding";
if (!slice.remaining)
stop();
else if (input_pending())
m_timer.Start(tick_ms);
else
m_timer.StartOnce(next_slice_ms);
}
}} // namespace Slic3r::GUI