changeset 402:2a11d0e07a53

Irccd: fix possible race in timer destruction/signal
author David Demelier <markand@malikania.fr>
date Wed, 11 Jan 2017 14:15:28 +0100
parents 4ded9be54086
children 8b7d1366c75c
files libirccd-js/irccd/mod-timer.cpp
diffstat 1 files changed, 4 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libirccd-js/irccd/mod-timer.cpp	Wed Jan 11 12:38:51 2017 +0100
+++ b/libirccd-js/irccd/mod-timer.cpp	Wed Jan 11 14:15:28 2017 +0100
@@ -35,15 +35,13 @@
 const char *Signature("\xff""\xff""irccd-timer-ptr");
 const char *CallbackTable("\xff""\xff""irccd-timer-callbacks");
 
-void handleSignal(std::weak_ptr<JsPlugin> ptr, std::string key)
+void handleSignal(Irccd& irccd, std::weak_ptr<JsPlugin> ptr, std::string key)
 {
     auto plugin = ptr.lock();
 
     if (!plugin)
         return;
 
-    auto &irccd = dukx_get_irccd(plugin->context());
-
     irccd.post([plugin, key] (Irccd &) {
         StackAssert sa(plugin->context());
 
@@ -139,10 +137,12 @@
         duk_error(ctx, DUK_ERR_TYPE_ERROR, "missing callback function");
 
     // Construct the timer in 'this'.
+    auto &irccd = dukx_get_irccd(ctx);
     auto timer = std::make_shared<Timer>(static_cast<TimerType>(type), delay);
     auto hash = std::to_string(reinterpret_cast<std::uintptr_t>(timer.get()));
 
-    timer->onSignal.connect(std::bind(handleSignal, std::weak_ptr<JsPlugin>(dukx_get_plugin(ctx)), hash));
+    timer->onSignal.connect(std::bind(handleSignal, std::ref(irccd),
+        std::weak_ptr<JsPlugin>(dukx_get_plugin(ctx)), hash));
 
     duk_push_this(ctx);
     duk_push_pointer(ctx, new std::shared_ptr<Timer>(std::move(timer)));