fix: cancel inflight cloud signaling requests

(cherry picked from commit 220508534a)
This commit is contained in:
Ian Chua
2026-10-06 20:05:34 +08:00
parent b870835b01
commit 44675778e2
3 changed files with 39 additions and 8 deletions
-3
View File
@@ -1,10 +1,7 @@
#include "Http.hpp"
#include "libslic3r_version.h"
#include <algorithm>
#include <atomic>
#include <boost/filesystem/operations.hpp>
#include <cassert>
#include <cstdlib>
#include <curl/curlver.h>
#include <fstream>
+35 -4
View File
@@ -42,10 +42,16 @@ void OrcaCloudSignalingChannel::close()
{
m_stop.store(true);
std::shared_ptr<Connection> conn;
Http::Ptr request;
{
std::lock_guard<std::mutex> 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<Slic3r::Http>(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<std::mutex> 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<std::mutex> lock(m_mutex);
if (m_inflight_requests == request)
m_inflight_requests.reset();
throw;
}
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_inflight_requests == request)
m_inflight_requests.reset();
}
try {
token_response = nlohmann::json::parse(token_body);
} catch (const std::exception&) {
@@ -1,8 +1,8 @@
#pragma once
#include "ICameraSignalingChannel.hpp"
#include "ICloudServiceAgent.hpp"
#include "OrcaCloudServiceAgent.hpp"
#include "Http.hpp"
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
@@ -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<bool> m_stop{false};
std::atomic<bool> m_open{false};