From fe6259ca08032a0f78071edfa475dfc51f0c4071 Mon Sep 17 00:00:00 2001 From: SoftFever Date: Thu, 29 Oct 2026 00:17:44 +0800 Subject: [PATCH] Refuse Design tab reads while a rebuild owns the document --- src/slic3r/GUI/CAD/DesignPanel.cpp | 9 ++++++--- src/slic3r/GUI/CAD/DesignPanel.hpp | 8 +++++--- src/slic3r/GUI/CAD/McpControl.cpp | 20 ++++++++++---------- src/slic3r/GUI/CAD/McpControl.hpp | 5 +++-- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/slic3r/GUI/CAD/DesignPanel.cpp b/src/slic3r/GUI/CAD/DesignPanel.cpp index cf74674b46..c702bc1237 100644 --- a/src/slic3r/GUI/CAD/DesignPanel.cpp +++ b/src/slic3r/GUI/CAD/DesignPanel.cpp @@ -4849,8 +4849,8 @@ void DesignPanel::on_import_svg() // touched while the worker owns it. Exceptions must not escape the worker: `work` is expected // to swallow them. // True while a worker owns the document. wxYield below runs pending events — and a queued MCP -// request is one — so the control socket must check this and refuse rather than change the -// document under the worker (DesignPanel::mcp_busy). +// request is one — so the control socket must check this and refuse rather than read or change +// the document under the worker (DesignPanel::mcp_busy). static std::atomic s_doc_worker_busy{0}; static void run_off_ui_thread(wxWindow* parent, const wxString& message, const std::function& work) @@ -4881,9 +4881,12 @@ static void run_off_ui_thread(wxWindow* parent, const wxString& message, const s worker.join(); } -bool DesignPanel::mcp_busy(bool sketch_method, std::string& why) const +bool DesignPanel::mcp_busy(bool read_only, bool sketch_method, std::string& why) const { if (s_doc_worker_busy.load() > 0) { why = "the model is being rebuilt; try again when it finishes"; return true; } + // Reading is safe under the rest: an open card, dialog or sketch only keeps a change from + // landing under it. + if (read_only) return false; // A GUI editor holds a candidate built from the document as it was when it opened; a // feature added or removed underneath it would be overwritten, or overwrite, on Confirm. if (m_active != Tool::None || m_edit_index >= 0) { why = "a feature card is open in the Design tab"; return true; } diff --git a/src/slic3r/GUI/CAD/DesignPanel.hpp b/src/slic3r/GUI/CAD/DesignPanel.hpp index ff7152241c..543dcefef0 100644 --- a/src/slic3r/GUI/CAD/DesignPanel.hpp +++ b/src/slic3r/GUI/CAD/DesignPanel.hpp @@ -108,9 +108,11 @@ public: // does not include (the table is generated and belongs to the offer-menu code). bool mcp_run_verb(const char* verb_id); // Would a request from the control socket collide with what the GUI is doing — a rebuild - // in progress, an open feature card, a sketch session? `sketch_method` = it drives the live - // sketch rather than the feature list. Fills `why` when it would. - bool mcp_busy(bool sketch_method, std::string& why) const; + // in progress, an open feature card, a sketch session? A rebuild refuses every request, since + // its worker owns the document; the rest refuse only one that changes something + // (`read_only` false). `sketch_method` = it drives the live sketch rather than the feature + // list. Fills `why` when it would. + bool mcp_busy(bool read_only, bool sketch_method, std::string& why) const; // Load the project's recipe into an empty document, as showing the tab does. The control // socket may be the first thing to touch the Design tab after a project was opened. void hydrate_from_model(); diff --git a/src/slic3r/GUI/CAD/McpControl.cpp b/src/slic3r/GUI/CAD/McpControl.cpp index 2c493cb5d6..77b8f1a63c 100644 --- a/src/slic3r/GUI/CAD/McpControl.cpp +++ b/src/slic3r/GUI/CAD/McpControl.cpp @@ -2022,22 +2022,22 @@ std::string handle_on_main(const std::string& method, const json& params, const DesignPanel* panel = DesignPanel::ensure(); if (!panel) return rpc_error(id, -32001, "Design panel not ready"); - // A project opened without ever showing the Design tab has its recipe only in the Model; - // load it, as showing the tab would, before anything reads or writes the document. - panel->hydrate_from_model(); // Methods that only LOOK. Everything else changes the document or the live sketch, and - // must not do it under a GUI editor, a sketch session it does not own, or a rebuild in - // progress (the GUI's worker thread holds the document then). + // must not do it under a GUI editor or a sketch session it does not own. No method may run + // during a rebuild, not even these: the rebuild's busy loop runs this request while the + // GUI's worker thread holds the document. Checked before hydrating, which writes it. static const std::set kReadOnly = { "describe_tools", "describe_scene", "query_topology", "measure", "mass_properties", "slice_body", "validate_against", "list_verbs", "sketch_describe", "sketch_validate", "check_interference" }; - if (kReadOnly.count(method) == 0) { - std::string why; - if (panel->mcp_busy(method.rfind("sketch_", 0) == 0, why)) - return rpc_error(id, -32002, "Design tab busy: " + why); - } + std::string why; + if (panel->mcp_busy(kReadOnly.count(method) != 0, method.rfind("sketch_", 0) == 0, why)) + return rpc_error(id, -32002, "Design tab busy: " + why); + + // A project opened without ever showing the Design tab has its recipe only in the Model; + // load it, as showing the tab would, before anything reads or writes the document. + panel->hydrate_from_model(); // Stale-id guard, checked here rather than in each handler. // diff --git a/src/slic3r/GUI/CAD/McpControl.hpp b/src/slic3r/GUI/CAD/McpControl.hpp index d1ec2eeb62..bc03683544 100644 --- a/src/slic3r/GUI/CAD/McpControl.hpp +++ b/src/slic3r/GUI/CAD/McpControl.hpp @@ -9,8 +9,9 @@ // left alone and the server does not start) // The socket is created 0600 and removed at exit. All CAD work is marshalled onto the wx main // thread and runs through the SAME CadDocument kernel the GUI uses (no parallel engine); -// describe_tools lists the methods. A method that changes the document is refused (-32002) -// while the Design tab is busy with it — a rebuild, an open feature card, a sketch session. +// describe_tools lists the methods. Every method is refused (-32002) during a rebuild, whose +// worker owns the document; one that changes the document is also refused while the Design tab +// is busy with it — an open feature card, a sketch session. // // The socket is POSIX only; on Windows the same methods are reached through the app's MCP server // (its cad_call tool), which calls cad_control_call().