Skip to content

Commit c14e105

Browse files
committed
Capture InterruptMain during takeMVar
Otherwise if exception wasn't captured during execution it will surface when we lock on MVar
1 parent 73beb3f commit c14e105

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
NEXT VERSION []
2+
------------------
3+
* Crash of python's main thread when one attempts to interrupt it fixed.
4+
15
0.1.1 [2025.02.13]
26
------------------
37
* Number of deadlocks in `runPyInMain` fixed:

src/Python/Internal/Eval.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ mainThread lock_init lock_eval = do
362362
case r_init of
363363
False -> pure ()
364364
True -> mask_ $ fix $ \loop ->
365-
takeMVar lock_eval >>= \case
365+
(takeMVar lock_eval `catch` (\InterruptMain -> pure HereWeGoAgain)) >>= \case
366366
EvalReq py resp -> do
367367
res <- (Right <$> runPy py) `catch` (pure . Left)
368368
putMVar resp res
@@ -373,6 +373,7 @@ mainThread lock_init lock_eval = do
373373
Py_Finalize();
374374
} |]
375375
putMVar resp ()
376+
HereWeGoAgain -> loop
376377

377378

378379
doInializePythonIO :: IO Bool
@@ -431,6 +432,7 @@ doInializePythonIO = do
431432
data EvalReq
432433
= forall a. EvalReq (Py a) (MVar (Either SomeException a))
433434
| StopReq (MVar ())
435+
| HereWeGoAgain
434436

435437
data InterruptMain = InterruptMain
436438
deriving stock Show

0 commit comments

Comments
 (0)