From c9162544f6a4b55a0979fc4df5c3f1a63e16fb98 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Tue, 27 Oct 2020 02:29:02 -0400 Subject: [PATCH] fix cache writing to cwd --- filecache.cpp | 7 +++++++ filecache.hpp | 2 ++ 2 files changed, 9 insertions(+) diff --git a/filecache.cpp b/filecache.cpp index c715260..1e15d24 100644 --- a/filecache.cpp +++ b/filecache.cpp @@ -9,6 +9,10 @@ Cache::Cache() { } Cache::~Cache() { + m_canceled = true; + for (auto &future : m_futures) + if (future.valid()) future.get(); + std::error_code err; if (!std::filesystem::remove_all(m_tmp_path, err)) fprintf(stderr, "error removing tmp dir\n"); @@ -45,9 +49,12 @@ void Cache::GetFileFromURL(std::string url, callback_type cb) { } else { m_callbacks[url].push_back(cb); auto future = std::async(std::launch::async, [this, url]() { + if (m_canceled) return; m_semaphore->wait(); + if (m_canceled) return; const auto &r = cpr::Get(cpr::Url { url }); m_semaphore->notify(); + if (m_canceled) return; OnResponse(r); }); m_futures.push_back(std::move(future)); diff --git a/filecache.hpp b/filecache.hpp index eed355a..6c6d979 100644 --- a/filecache.hpp +++ b/filecache.hpp @@ -29,4 +29,6 @@ private: std::unordered_map> m_callbacks; std::vector> m_futures; std::filesystem::path m_tmp_path; + + bool m_canceled = false; };