From 3410caa6f2e1fd74d55f7a1717420bb2e1052b12 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 29 May 2010 23:48:47 +0200 Subject: Fixed the plugin termination. The master of a plugin will now wait for the plugin it kills to terminate. --- Hsbot/Plugin.hs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'Hsbot/Plugin.hs') diff --git a/Hsbot/Plugin.hs b/Hsbot/Plugin.hs index e545e8d..3feffa8 100644 --- a/Hsbot/Plugin.hs +++ b/Hsbot/Plugin.hs @@ -5,8 +5,9 @@ module Hsbot.Plugin , unloadPlugin ) where -import Control.Concurrent -import Control.Concurrent.Chan () +import Control.Concurrent (forkIO) +import Control.Concurrent.Chan +import Control.Concurrent.MVar import Control.Exception import Control.Monad.State import qualified Data.Map as M @@ -29,12 +30,13 @@ spawnPlugin (IrcBotConfig ircConfig) = do bot <- get let chan = botChan bot pchan <- liftIO (newChan :: IO (Chan BotMsg)) - threadId <- liftIO $ forkIO (startIrcbot ircConfig chan pchan) + mvar <- liftIO newEmptyMVar + threadId <- liftIO . forkIO $ finally (startIrcbot ircConfig chan pchan) (putMVar mvar ()) let plugin = PluginState { pluginName = ircConfigName ircConfig , pluginChan = pchan , pluginHandles = M.empty } plugins = botPlugins bot - put $ bot { botPlugins = M.insert (pluginName plugin) (plugin, threadId) plugins } + put $ bot { botPlugins = M.insert (pluginName plugin) (plugin, mvar, threadId) plugins } resumeData <- gets botResumeData liftIO $ modifyMVar_ resumeData (\oldData -> return $ M.insert (ircConfigName ircConfig) M.empty oldData) @@ -52,9 +54,10 @@ killPlugin name = do let oldPlugins = botPlugins bot -- We check if the plugin exists case M.lookup name oldPlugins of - Just (_, threadId) -> do + Just (_, mvar, threadId) -> do let newPlugins = M.delete name oldPlugins liftIO $ throwTo threadId UserInterrupt put $ bot { botPlugins = newPlugins } + liftIO $ takeMVar mvar Nothing -> return () -- cgit v1.2.3