From dc302393d04e8c074830cd3e69c8172c435bef3c Mon Sep 17 00:00:00 2001 From: Florian Schmaus <flow@cs.fau.de> Date: Wed, 19 Feb 2020 16:44:43 +0100 Subject: [PATCH] Ensure that the current fiber is recycled in discardAndResume() --- emper/ContextManager.cpp | 12 ++++++++++++ emper/ContextManager.hpp | 3 +++ emper/Dispatcher.hpp | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/emper/ContextManager.cpp b/emper/ContextManager.cpp index 88a651c8..7a15eb4b 100644 --- a/emper/ContextManager.cpp +++ b/emper/ContextManager.cpp @@ -64,5 +64,17 @@ void ContextManager::discardAndResume(Context* context) { LOGD("Freeing context " << contextToFree); putFreeContext(contextToFree); }); + + // Since we are going to discard this context, it will never reach + // the end of its dispatch loop, and hence we need to ensure that + // the fiber is recycled. + const Fiber* currentFiber = Dispatcher::currentFiber; + if (currentFiber) { + Dispatcher::recycle(currentFiber); + + // Set currentFiber to nullptr to avoid double frees here. + Dispatcher::currentFiber = nullptr; + } + contextToFree->discardAndResume(context); } diff --git a/emper/ContextManager.hpp b/emper/ContextManager.hpp index e39f5b32..35cbfc87 100644 --- a/emper/ContextManager.hpp +++ b/emper/ContextManager.hpp @@ -1,6 +1,7 @@ #pragma once #include "Common.hpp" +#include "Dispatcher.hpp" #include "WsClQueue.hpp" #include "Runtime.hpp" #include "Context.hpp" @@ -17,6 +18,8 @@ private: friend Context; + friend Dispatcher; + public: ContextManager(Runtime& runtime); diff --git a/emper/Dispatcher.hpp b/emper/Dispatcher.hpp index 16a6b884..f7b8d117 100644 --- a/emper/Dispatcher.hpp +++ b/emper/Dispatcher.hpp @@ -35,7 +35,7 @@ protected: return fiber->doAtomicDecrRefCount(); } - inline void recycle(const Fiber* fiber) { + static inline void recycle(const Fiber* fiber) { delete fiber; } -- GitLab