Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deadlock when canceling the slicing while gcode is creating thumbnails #6476

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/libslic3r/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,9 +886,14 @@ namespace DoExport {
// Write thumbnails using base64 encoding
if (thumbnail_cb != nullptr)
{
//Create the thumbnails
const size_t max_row_length = 78;
ThumbnailsList thumbnails;
thumbnail_cb(thumbnails, sizes, true, true, true, true);
// note that it needs the gui thread, so can create deadlock if job is canceled.
bool can_create_thumbnail = thumbnail_cb(thumbnails, sizes, true, true, true, true);
throw_if_canceled();
if (!can_create_thumbnail) return;

for (const ThumbnailData& data : thumbnails)
{
if (data.is_valid())
Expand Down
2 changes: 1 addition & 1 deletion src/libslic3r/GCode/ThumbnailData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct ThumbnailData
};

typedef std::vector<ThumbnailData> ThumbnailsList;
typedef std::function<void(ThumbnailsList & thumbnails, const Vec2ds & sizes, bool printable_only, bool parts_only, bool show_bed, bool transparent_background)> ThumbnailsGeneratorCallback;
typedef std::function<bool(ThumbnailsList & thumbnails, const Vec2ds & sizes, bool printable_only, bool parts_only, bool show_bed, bool transparent_background)> ThumbnailsGeneratorCallback;

} // namespace Slic3r

Expand Down
39 changes: 34 additions & 5 deletions src/slic3r/GUI/Plater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1760,14 +1760,43 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
background_process.set_fff_print(&fff_print);
background_process.set_sla_print(&sla_print);
background_process.set_gcode_result(&gcode_result);
background_process.set_thumbnail_cb([this](ThumbnailsList& thumbnails, const Vec2ds& sizes, bool printable_only, bool parts_only, bool show_bed, bool transparent_background)
background_process.set_thumbnail_cb([this](ThumbnailsList& thumbnails, const Vec2ds& sizes, bool printable_only, bool parts_only, bool show_bed, bool transparent_background)->bool
{
std::packaged_task<void(ThumbnailsList&, const Vec2ds&, bool, bool, bool, bool)> task([this](ThumbnailsList& thumbnails, const Vec2ds& sizes, bool printable_only, bool parts_only, bool show_bed, bool transparent_background) {
auto task = std::make_shared<std::packaged_task<void(ThumbnailsList&, const Vec2ds&, bool, bool, bool, bool)>>([this](ThumbnailsList& thumbnails, const Vec2ds& sizes, bool printable_only, bool parts_only, bool show_bed, bool transparent_background) {
generate_thumbnails(thumbnails, sizes, printable_only, parts_only, show_bed, transparent_background);
});
std::future<void> result = task.get_future();
wxTheApp->CallAfter([&]() { task(thumbnails, sizes, printable_only, parts_only, show_bed, transparent_background); });
result.wait();

std::future<void> future_result = task->get_future();
std::shared_ptr<std::mutex> protect_bool = std::make_shared<std::mutex>();
std::shared_ptr<bool> is_started = std::make_shared<bool>(false);
std::shared_ptr<bool> cancel = std::make_shared<bool>(false);
wxTheApp->CallAfter([task, protect_bool, is_started, cancel, &thumbnails, &sizes, &printable_only, &parts_only, &show_bed, &transparent_background]()
{
{
std::lock_guard<std::mutex> lock(*protect_bool);
if (*cancel)
return;
*is_started = true;
}
(*task)(thumbnails, sizes, printable_only, parts_only, show_bed, transparent_background);
});
// can deadlock if background processing is cancelled / locked
// have to cancel the process if we're exiting here as the parameters will be deleted.
// if the process is already started, then we have to wait its end. and there is no deadlock with generate_thumbnails
// 2 seconds is plenty to
std::future_status result = future_result.wait_for(std::chrono::seconds(2));
if (result == std::future_status::ready)
return true;
{
std::lock_guard<std::mutex> lock(*protect_bool);
if (*is_started) {
future_result.wait();
result = std::future_status::ready;
} else {
*cancel = true;
}
}
return result == std::future_status::ready;
});
background_process.set_slicing_completed_event(EVT_SLICING_COMPLETED);
background_process.set_finished_event(EVT_PROCESS_COMPLETED);
Expand Down