From c14e10503ed853e0eb2be20698c3cb8559c2c591 Mon Sep 17 00:00:00 2001 From: Alexey Khudyakov Date: Sun, 16 Feb 2025 23:50:01 +0300 Subject: [PATCH] Capture InterruptMain during takeMVar Otherwise if exception wasn't captured during execution it will surface when we lock on MVar --- ChangeLog.md | 4 ++++ src/Python/Internal/Eval.hs | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 429037b..ed87066 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,7 @@ +NEXT VERSION [] +------------------ +* Crash of python's main thread when one attempts to interrupt it fixed. + 0.1.1 [2025.02.13] ------------------ * Number of deadlocks in `runPyInMain` fixed: diff --git a/src/Python/Internal/Eval.hs b/src/Python/Internal/Eval.hs index faf1f67..ca5970d 100644 --- a/src/Python/Internal/Eval.hs +++ b/src/Python/Internal/Eval.hs @@ -362,7 +362,7 @@ mainThread lock_init lock_eval = do case r_init of False -> pure () True -> mask_ $ fix $ \loop -> - takeMVar lock_eval >>= \case + (takeMVar lock_eval `catch` (\InterruptMain -> pure HereWeGoAgain)) >>= \case EvalReq py resp -> do res <- (Right <$> runPy py) `catch` (pure . Left) putMVar resp res @@ -373,6 +373,7 @@ mainThread lock_init lock_eval = do Py_Finalize(); } |] putMVar resp () + HereWeGoAgain -> loop doInializePythonIO :: IO Bool @@ -431,6 +432,7 @@ doInializePythonIO = do data EvalReq = forall a. EvalReq (Py a) (MVar (Either SomeException a)) | StopReq (MVar ()) + | HereWeGoAgain data InterruptMain = InterruptMain deriving stock Show