From 220508534a941161b1782651a12f6af88f10c688 Mon Sep 17 00:00:00 2001 From: Ian Chua Date: Thu, 24 Sep 2026 19:03:58 +0800 Subject: [PATCH] fix: cancel inflight cloud signaling requests --- src/slic3r/Utils/Http.cpp | 11 +++--- .../Utils/OrcaCloudSignalingChannel.cpp | 39 +++++++++++++++++-- .../Utils/OrcaCloudSignalingChannel.hpp | 5 ++- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/slic3r/Utils/Http.cpp b/src/slic3r/Utils/Http.cpp index 86c2d681bc..05e1d6c77c 100644 --- a/src/slic3r/Utils/Http.cpp +++ b/src/slic3r/Utils/Http.cpp @@ -1,5 +1,6 @@ #include "Http.hpp" +#include #include #include #include @@ -133,7 +134,7 @@ struct Http::priv std::string error_buffer; // Used for CURLOPT_ERRORBUFFER std::string headers; size_t limit; - bool cancel; + std::atomic_bool cancel; std::unique_ptr putFile; std::thread io_thread; @@ -271,9 +272,9 @@ int Http::priv::xfercb(void *userp, curl_off_t dltotal, curl_off_t dlnow, curl_o self->progressfn(progress, cb_cancel); } - if (cb_cancel) { self->cancel = true; } + if (cb_cancel) { self->cancel.store(true); } - return self->cancel; + return self->cancel.load(); } int Http::priv::xfercb_legacy(void *userp, double dltotal, double dlnow, double ultotal, double ulnow) @@ -484,7 +485,7 @@ void Http::priv::http_perform() if (res != CURLE_OK) { if (res == CURLE_ABORTED_BY_CALLBACK) { - if (cancel) { + if (cancel.load()) { // The abort comes from the request being cancelled programatically Progress dummyprogress(0, 0, 0, 0, std::string()); bool cancel = true; @@ -795,7 +796,7 @@ void Http::perform_sync() void Http::cancel() { - if (p) { p->cancel = true; } + if (p) { p->cancel.store(true); } } void Http::print() const diff --git a/src/slic3r/Utils/OrcaCloudSignalingChannel.cpp b/src/slic3r/Utils/OrcaCloudSignalingChannel.cpp index 692789a02c..3e7b0bfa82 100644 --- a/src/slic3r/Utils/OrcaCloudSignalingChannel.cpp +++ b/src/slic3r/Utils/OrcaCloudSignalingChannel.cpp @@ -42,10 +42,16 @@ void OrcaCloudSignalingChannel::close() { m_stop.store(true); std::shared_ptr conn; + Http::Ptr request; { std::lock_guard lock(m_mutex); conn = m_conn; + request = m_inflight_requests; } + // m_conn is installed only after the live-token request completes, so + // cancel the request independently before waiting for the worker. + if (request) + request->cancel(); if (conn) { // Established session: close the socket on the io_context's own thread so // the pending async_read completes and io_context.run() unwinds. @@ -123,8 +129,9 @@ void OrcaCloudSignalingChannel::run() nlohmann::json token_response; std::string token_body; unsigned int http_code = 0; - auto request = Http::post(live_token_url); - request.set_post_body(std::string("{}")) + + auto request = std::make_shared(Http::post(live_token_url)); + request->set_post_body(std::string("{}")) .header("Authorization", "Bearer " + token) .header("Content-Type", "application/json") .tls_verify(true) @@ -135,8 +142,32 @@ void OrcaCloudSignalingChannel::run() }) .on_error([&http_code](std::string, std::string, unsigned status) { http_code = status; - }) - .perform_sync(); + }); + + { + std::lock_guard lock(m_mutex); + m_inflight_requests = request; + } + + // If close() raced with request setup, make sure this request is + // cancelled before entering the blocking call. + if (m_stop.load()) + request->cancel(); + try { + request->perform_sync(); + } catch (...) { + std::lock_guard lock(m_mutex); + if (m_inflight_requests == request) + m_inflight_requests.reset(); + throw; + } + + { + std::lock_guard lock(m_mutex); + if (m_inflight_requests == request) + m_inflight_requests.reset(); + } + try { token_response = nlohmann::json::parse(token_body); } catch (const std::exception&) { diff --git a/src/slic3r/Utils/OrcaCloudSignalingChannel.hpp b/src/slic3r/Utils/OrcaCloudSignalingChannel.hpp index a277a952b0..ff84190853 100644 --- a/src/slic3r/Utils/OrcaCloudSignalingChannel.hpp +++ b/src/slic3r/Utils/OrcaCloudSignalingChannel.hpp @@ -1,8 +1,8 @@ #pragma once #include "ICameraSignalingChannel.hpp" -#include "ICloudServiceAgent.hpp" #include "OrcaCloudServiceAgent.hpp" +#include "Http.hpp" #include #include @@ -52,6 +52,9 @@ private: static std::string encode_path_component(const std::string& value); OrcaCloudServiceAgent* m_cloud; + + Http::Ptr m_inflight_requests{nullptr}; + std::string m_dev_id; std::atomic m_stop{false}; std::atomic m_open{false};