mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-08-31 05:47:02 +00:00
fix lock bug, and remove not work code.
This commit is contained in:
@@ -29,21 +29,13 @@ std::string DownloadManager::get_unique_file_path(const boost::filesystem::path&
|
|||||||
|
|
||||||
boost::filesystem::path parent_dir = file_path.parent_path();
|
boost::filesystem::path parent_dir = file_path.parent_path();
|
||||||
std::string filename = file_path.filename().string();
|
std::string filename = file_path.filename().string();
|
||||||
|
|
||||||
// Properly extract extension (includes the dot, e.g., ".txt")
|
|
||||||
// For "file.txt", extension() returns ".txt"
|
|
||||||
// For "file", extension() returns ""
|
|
||||||
std::string extension = file_path.extension().string();
|
std::string extension = file_path.extension().string();
|
||||||
|
|
||||||
// Extract name without extension
|
|
||||||
// If extension is empty, name_without_ext is the full filename
|
|
||||||
std::string name_without_ext;
|
std::string name_without_ext;
|
||||||
if (extension.empty()) {
|
if (extension.empty()) {
|
||||||
name_without_ext = filename;
|
name_without_ext = filename;
|
||||||
BOOST_LOG_TRIVIAL(debug) << boost::format("DownloadManager::get_unique_file_path: No extension found, filename='%1%'") % filename;
|
BOOST_LOG_TRIVIAL(debug) << boost::format("DownloadManager::get_unique_file_path: No extension found, filename='%1%'") % filename;
|
||||||
} else {
|
} else {
|
||||||
// Remove extension from filename (extension includes the dot)
|
|
||||||
// For "file.txt", filename="file.txt", extension=".txt", so name_without_ext="file"
|
|
||||||
name_without_ext = filename.substr(0, filename.size() - extension.size());
|
name_without_ext = filename.substr(0, filename.size() - extension.size());
|
||||||
BOOST_LOG_TRIVIAL(debug) << boost::format("DownloadManager::get_unique_file_path: filename='%1%', extension='%2%', name_without_ext='%3%'")
|
BOOST_LOG_TRIVIAL(debug) << boost::format("DownloadManager::get_unique_file_path: filename='%1%', extension='%2%', name_without_ext='%3%'")
|
||||||
% filename % extension % name_without_ext;
|
% filename % extension % name_without_ext;
|
||||||
@@ -252,15 +244,11 @@ void DownloadManager::start_download_impl(std::shared_ptr<DownloadTask> task) {
|
|||||||
// Step 3: Set complete callback
|
// Step 3: Set complete callback
|
||||||
http.on_complete([this, task](std::string body, unsigned status) {
|
http.on_complete([this, task](std::string body, unsigned status) {
|
||||||
wxGetApp().CallAfter([this, task, body]() {
|
wxGetApp().CallAfter([this, task, body]() {
|
||||||
// Check if task still exists and is not canceled
|
// Check if task still exists and is not canceled (without lock to avoid deadlock with cleanup_task)
|
||||||
{
|
if (task->state == DownloadTaskState::Canceled) {
|
||||||
std::lock_guard<std::mutex> lock(m_tasks_mutex);
|
// Task has been canceled, ignore completion
|
||||||
if (m_tasks.find(task->task_id) == m_tasks.end() ||
|
BOOST_LOG_TRIVIAL(debug) << "DownloadManager: Ignoring complete callback for canceled task " << task->task_id;
|
||||||
task->state == DownloadTaskState::Canceled) {
|
return;
|
||||||
// Task has been canceled or cleaned up, ignore completion
|
|
||||||
BOOST_LOG_TRIVIAL(debug) << "DownloadManager: Ignoring complete callback for canceled/cleaned task " << task->task_id;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -301,19 +289,11 @@ void DownloadManager::start_download_impl(std::shared_ptr<DownloadTask> task) {
|
|||||||
// Step 4: Set error callback
|
// Step 4: Set error callback
|
||||||
http.on_error([this, task](std::string body, std::string error, unsigned status) {
|
http.on_error([this, task](std::string body, std::string error, unsigned status) {
|
||||||
wxGetApp().CallAfter([this, task, error, status]() {
|
wxGetApp().CallAfter([this, task, error, status]() {
|
||||||
// Check if task still exists and is not canceled
|
// Check if task was canceled (without lock to avoid deadlock with cleanup_task)
|
||||||
{
|
if (task->state == DownloadTaskState::Canceled) {
|
||||||
std::lock_guard<std::mutex> lock(m_tasks_mutex);
|
// Task was canceled, ignore error callback (cancel already handled cleanup)
|
||||||
if (m_tasks.find(task->task_id) == m_tasks.end()) {
|
BOOST_LOG_TRIVIAL(debug) << "DownloadManager: Ignoring error callback for canceled task " << task->task_id;
|
||||||
// Task has been cleaned up, ignore error callback
|
return;
|
||||||
BOOST_LOG_TRIVIAL(debug) << "DownloadManager: Ignoring error callback for cleaned task " << task->task_id;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (task->state == DownloadTaskState::Canceled) {
|
|
||||||
// Task was canceled, ignore error callback (cancel already handled cleanup)
|
|
||||||
BOOST_LOG_TRIVIAL(debug) << "DownloadManager: Ignoring error callback for canceled task " << task->task_id;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string error_msg = boost::str(boost::format("HTTP error: %1% (status: %2%)") % error % status);
|
std::string error_msg = boost::str(boost::format("HTTP error: %1% (status: %2%)") % error % status);
|
||||||
@@ -368,11 +348,6 @@ bool DownloadManager::cancel_download(size_t task_id) {
|
|||||||
if (task->is_wcp_download()) {
|
if (task->is_wcp_download()) {
|
||||||
wcp_to_destroy = task->wcp_instance.lock();
|
wcp_to_destroy = task->wcp_instance.lock();
|
||||||
} else {
|
} else {
|
||||||
// For internal downloads, don't call error callback if task is being canceled
|
|
||||||
// during destruction (e.g., when dialog is closing). The callback may reference
|
|
||||||
// a destroyed dialog object, causing a crash.
|
|
||||||
// Note: We clear the callbacks before cleanup to prevent any delayed callbacks
|
|
||||||
// from accessing destroyed objects.
|
|
||||||
task->callbacks.on_error = nullptr;
|
task->callbacks.on_error = nullptr;
|
||||||
task->callbacks.on_progress = nullptr;
|
task->callbacks.on_progress = nullptr;
|
||||||
task->callbacks.on_complete = nullptr;
|
task->callbacks.on_complete = nullptr;
|
||||||
@@ -498,9 +473,7 @@ void DownloadManager::call_internal_progress_callback(std::shared_ptr<DownloadTa
|
|||||||
int percent,
|
int percent,
|
||||||
size_t downloaded,
|
size_t downloaded,
|
||||||
size_t total) {
|
size_t total) {
|
||||||
// Only check if callback is still valid (cleared during cancellation)
|
|
||||||
// Don't check m_tasks because this is called from CallAfter which may execute
|
|
||||||
// after cleanup_task, but the callback should still be valid if not canceled
|
|
||||||
if (task->callbacks.on_progress && task->state != DownloadTaskState::Canceled) {
|
if (task->callbacks.on_progress && task->state != DownloadTaskState::Canceled) {
|
||||||
task->callbacks.on_progress(task->task_id, percent, downloaded, total);
|
task->callbacks.on_progress(task->task_id, percent, downloaded, total);
|
||||||
}
|
}
|
||||||
@@ -541,9 +514,7 @@ void DownloadManager::send_wcp_complete_update(std::shared_ptr<DownloadTask> tas
|
|||||||
|
|
||||||
void DownloadManager::call_internal_complete_callback(std::shared_ptr<DownloadTask> task,
|
void DownloadManager::call_internal_complete_callback(std::shared_ptr<DownloadTask> task,
|
||||||
const std::string& file_path) {
|
const std::string& file_path) {
|
||||||
// Only check if callback is still valid (cleared during cancellation)
|
|
||||||
// Don't check m_tasks because cleanup_task may have been called, but the callback
|
|
||||||
// should still be valid if not canceled
|
|
||||||
if (task->callbacks.on_complete && task->state != DownloadTaskState::Canceled) {
|
if (task->callbacks.on_complete && task->state != DownloadTaskState::Canceled) {
|
||||||
task->callbacks.on_complete(task->task_id, file_path);
|
task->callbacks.on_complete(task->task_id, file_path);
|
||||||
}
|
}
|
||||||
@@ -582,9 +553,7 @@ void DownloadManager::send_wcp_error_update(std::shared_ptr<DownloadTask> task,
|
|||||||
|
|
||||||
void DownloadManager::call_internal_error_callback(std::shared_ptr<DownloadTask> task,
|
void DownloadManager::call_internal_error_callback(std::shared_ptr<DownloadTask> task,
|
||||||
const std::string& error) {
|
const std::string& error) {
|
||||||
// Only check if callback is still valid (cleared during cancellation)
|
|
||||||
// Don't check m_tasks because cleanup_task may have been called, but the callback
|
|
||||||
// should still be valid if not canceled
|
|
||||||
if (task->callbacks.on_error && task->state != DownloadTaskState::Canceled) {
|
if (task->callbacks.on_error && task->state != DownloadTaskState::Canceled) {
|
||||||
task->callbacks.on_error(task->task_id, error);
|
task->callbacks.on_error(task->task_id, error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user