fix: cancel inflight cloud signaling requests

This commit is contained in:
Ian Chua
2026-09-24 19:03:58 +08:00
parent 5a0810365b
commit 220508534a
3 changed files with 45 additions and 10 deletions
+6 -5
View File
@@ -1,5 +1,6 @@
#include "Http.hpp"
#include <atomic>
#include <cstdlib>
#include <functional>
#include <thread>
@@ -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<form_file> 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
+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};