aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2024-12-02 22:53:51 +0000
committersaturneric <[email protected]>2024-12-02 22:53:51 +0000
commit78a5ece0391cb0e8cecde95f43afe4479a6709ab (patch)
tree9114b46fb28d58ef63546a78994cc23b6caa909a
parentfix: solve memory issues of sdk apis (diff)
downloadGpgFrontend-78a5ece0391cb0e8cecde95f43afe4479a6709ab.tar.gz
GpgFrontend-78a5ece0391cb0e8cecde95f43afe4479a6709ab.zip
fix: discovered issues
m---------modules0
-rw-r--r--src/cmd.cpp19
-rw-r--r--src/core/GpgCoreInit.cpp14
-rw-r--r--src/core/function/gpg/GpgAdvancedOperator.cpp5
-rw-r--r--src/core/function/gpg/GpgCommandExecutor.cpp26
-rw-r--r--src/core/thread/Task.cpp9
-rw-r--r--src/core/thread/Task.h3
-rw-r--r--src/init.cpp8
8 files changed, 53 insertions, 31 deletions
diff --git a/modules b/modules
-Subproject 7df672124472e5bfd08bfda867ff4188e3be4f7
+Subproject ec7541da6532619225e580e2b76185ef6ab2fbc
diff --git a/src/cmd.cpp b/src/cmd.cpp
index c9cdbc17..2ab0b323 100644
--- a/src/cmd.cpp
+++ b/src/cmd.cpp
@@ -37,6 +37,7 @@
#include "core/GpgCoreInit.h"
#include "core/function/GlobalSettingStation.h"
#include "core/module/ModuleManager.h"
+#include "core/thread/TaskRunnerGetter.h"
#include "core/utils/BuildInfoUtils.h"
// GpgFrontend
@@ -199,7 +200,23 @@ auto RunTest(const GFCxtWPtr& p_ctx) -> int {
test_init_args.argc = ctx->argc;
test_init_args.argv = ctx->argv;
- return GpgFrontend::Test::ExecuteAllTestCase(test_init_args);
+ QEventLoop looper;
+
+ auto* task = new GpgFrontend::Thread::Task(
+ [=](const DataObjectPtr&) -> int {
+ return GpgFrontend::Test::ExecuteAllTestCase(test_init_args);
+ },
+ "unit-test", TransferParams());
+
+ QObject::connect(task, &GpgFrontend::Thread::Task::SignalTaskEnd, &looper,
+ &QEventLoop::quit);
+
+ GpgFrontend::Thread::TaskRunnerGetter::GetInstance()
+ .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
+ ->PostTask(task);
+
+ looper.exec();
+ return 0;
}
} // namespace GpgFrontend \ No newline at end of file
diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp
index 9ebc9622..0659377a 100644
--- a/src/core/GpgCoreInit.cpp
+++ b/src/core/GpgCoreInit.cpp
@@ -47,17 +47,7 @@
namespace GpgFrontend {
-void DestroyGpgFrontendCore() {
- // kill all daemon if necessary
- auto settings = GlobalSettingStation::GetInstance().GetSettings();
- auto kill_all_gnupg_daemon_at_close =
- settings.value("gnupg/kill_all_gnupg_daemon_at_close", false).toBool();
- if (kill_all_gnupg_daemon_at_close) {
- GpgAdvancedOperator::KillAllGpgComponents();
- }
-
- SingletonStorageCollection::Destroy();
-}
+void DestroyGpgFrontendCore() { SingletonStorageCollection::Destroy(); }
auto VerifyGpgconfPath(const QFileInfo& gnupg_install_fs_path) -> bool {
return gnupg_install_fs_path.isAbsolute() && gnupg_install_fs_path.exists() &&
@@ -651,7 +641,7 @@ auto InitGpgFrontendCore(CoreInitArgs args) -> int {
.GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
->PostTask(task);
- if (restart_all_gnupg_components_on_start) {
+ if (!args.unit_test_mode && restart_all_gnupg_components_on_start) {
GpgAdvancedOperator::RestartGpgComponents();
}
return 0;
diff --git a/src/core/function/gpg/GpgAdvancedOperator.cpp b/src/core/function/gpg/GpgAdvancedOperator.cpp
index 4a373f0a..6b56d867 100644
--- a/src/core/function/gpg/GpgAdvancedOperator.cpp
+++ b/src/core/function/gpg/GpgAdvancedOperator.cpp
@@ -46,12 +46,17 @@ void GpgFrontend::GpgAdvancedOperator::ClearGpgPasswordCache(
return;
}
+#if defined(__APPLE__) && defined(__MACH__)
+ FLOG_I("kill all gpg components in order to clear gpg password cache");
+ KillAllGpgComponents();
+#else
GpgFrontend::GpgCommandExecutor::ExecuteSync(
{gpgconf_path, QStringList{"--reload", "gpg-agent"},
[=](int exit_code, const QString & /*p_out*/,
const QString & /*p_err*/) {
cb(exit_code == 0 ? 0 : -1, TransferParams());
}});
+#endif
}
void GpgFrontend::GpgAdvancedOperator::ReloadGpgComponents(
diff --git a/src/core/function/gpg/GpgCommandExecutor.cpp b/src/core/function/gpg/GpgCommandExecutor.cpp
index 555c710d..3e218b92 100644
--- a/src/core/function/gpg/GpgCommandExecutor.cpp
+++ b/src/core/function/gpg/GpgCommandExecutor.cpp
@@ -139,36 +139,38 @@ auto BuildTaskFromExecCtx(const GpgCommandExecutor::ExecuteContext &context)
};
return new Thread::Task(
- std::move(runner), QString("GpgCommamdExecutor(%1){%2}").arg(cmd),
+ std::move(runner),
+ QString("GpgCommamdExecutor(%1){%2}").arg(cmd).arg(arguments.join(' ')),
TransferParams(cmd, arguments, interact_function, cmd_executor_callback),
std::move(result_callback));
}
void GpgCommandExecutor::ExecuteSync(ExecuteContext context) {
Thread::Task *task = BuildTaskFromExecCtx(context);
+ QPointer<Thread::Task> p_t = task;
- QEventLoop looper;
- QObject::connect(task, &Thread::Task::SignalTaskEnd, &looper,
+ auto *looper = new QEventLoop(QCoreApplication::instance());
+ QObject::connect(task, &Thread::Task::SignalTaskEnd, looper,
&QEventLoop::quit);
- Thread::TaskRunnerPtr target_task_runner = nullptr;
+ Thread::TaskRunnerPtr target_task_runner = context.task_runner;
- if (context.task_runner != nullptr) {
- target_task_runner = context.task_runner;
- } else {
+ if (target_task_runner == nullptr) {
target_task_runner =
GpgFrontend::Thread::TaskRunnerGetter::GetInstance().GetTaskRunner(
Thread::TaskRunnerGetter::kTaskRunnerType_External_Process);
}
target_task_runner->PostTask(task);
- // to arvoid dead lock issue we need to check if current thread is the same as
- // target thread. if it is, we can't call exec() because it will block the
- // current thread.
- FLOG_D("blocking until gpg command finish...");
+ // to arvoid dead lock issue we need to check if current thread is the
+ // same as target thread. if it is, we can't call exec() because it will
+ // block the current thread.
+ FLOG_D() << "blocking until gpg command " << context.cmd << context.arguments
+ << " finish...";
// block until task finished
// this is to keep reference vaild until task finished
- looper.exec();
+ looper->exec();
+ looper->deleteLater();
}
void GpgCommandExecutor::ExecuteConcurrentlyAsync(ExecuteContexts contexts) {
diff --git a/src/core/thread/Task.cpp b/src/core/thread/Task.cpp
index 71c3f7b2..d96d3c8c 100644
--- a/src/core/thread/Task.cpp
+++ b/src/core/thread/Task.cpp
@@ -120,7 +120,7 @@ class Task::Impl {
const QString name_;
TaskRunnable runnable_; ///<
TaskCallback callback_; ///<
- int rtn_ = -99; ///<
+ int rtn_ = Task::kInitialRTN; ///<
QThread *callback_thread_ = nullptr; ///<
DataObjectPtr data_object_ = nullptr; ///<
@@ -139,6 +139,7 @@ class Task::Impl {
[this](int rtn) {
// set task returning code
SetRTN(rtn);
+
try {
if (callback_) {
callback_(rtn_, data_object_);
@@ -147,6 +148,9 @@ class Task::Impl {
LOG_W() << "task: {}, " << GetFullID()
<< "callback caught exception, rtn: " << rtn;
}
+
+ LOG_D() << "task" << this->name_
+ << "sending task end signal, rtn:" << rtn;
emit parent_->SignalTaskEnd();
});
@@ -224,5 +228,6 @@ void Task::slot_exception_safe_run() noexcept {
// raise signal to anounce after runnable returned
if (this->autoDelete()) emit this->SignalTaskShouldEnd(rtn);
}
-auto Task::GetRTN() { return p_->GetRTN(); }
+
+auto Task::GetRTN() -> int { return p_->GetRTN(); }
} // namespace GpgFrontend::Thread \ No newline at end of file
diff --git a/src/core/thread/Task.h b/src/core/thread/Task.h
index 02952096..0a9b0c84 100644
--- a/src/core/thread/Task.h
+++ b/src/core/thread/Task.h
@@ -43,6 +43,7 @@ class GPGFRONTEND_CORE_EXPORT Task : public QObject, public QRunnable {
using TaskRunnable = std::function<int(DataObjectPtr)>; ///<
using TaskCallback = std::function<void(int, DataObjectPtr)>; ///<
+ static const int kInitialRTN = -99;
class TaskHandler {
public:
@@ -119,7 +120,7 @@ class GPGFRONTEND_CORE_EXPORT Task : public QObject, public QRunnable {
*
* @return auto
*/
- [[nodiscard]] auto GetRTN();
+ [[nodiscard]] auto GetRTN() -> int;
public slots:
diff --git a/src/init.cpp b/src/init.cpp
index 2c421040..e1668ec5 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -183,11 +183,13 @@ void ShutdownGlobalBasicEnv(const GFCxtWPtr &p_ctx) {
return;
}
- // clear password cache
- if (GlobalSettingStation::GetInstance()
+ auto clear_gpg_password_cache =
+ GlobalSettingStation::GetInstance()
.GetSettings()
.value("basic/clear_gpg_password_cache", false)
- .toBool()) {
+ .toBool();
+ // clear password cache
+ if (!ctx->unit_test_mode && clear_gpg_password_cache) {
GpgAdvancedOperator::ClearGpgPasswordCache([](int, DataObjectPtr) {});
}