Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 513473f

Browse files
committed
Docs and code should not talk about 'terminating children'
1 parent d8fe916 commit 513473f

File tree

5 files changed

+151
-137
lines changed

5 files changed

+151
-137
lines changed

src/Control/Distributed/Process/Supervisor.hs

Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
-- The supervisors children are defined as a list of child specifications
3636
-- (see 'ChildSpec'). When a supervisor is started, its children are started
3737
-- in left-to-right (insertion order) according to this list. When a supervisor
38-
-- stops (or exits for any reason), it will terminate its children in reverse
38+
-- stops (or exits for any reason), it will stop its children in reverse
3939
-- (i.e., from right-to-left of insertion) order. Child specs can be added to
4040
-- the supervisor after it has started, either on the left or right of the
4141
-- existing list of children.
4242
--
4343
-- When the supervisor spawns its child processes, they are always linked to
4444
-- their parent (i.e., the supervisor), therefore even if the supervisor is
45-
-- terminated abruptly by an asynchronous exception, the children will still be
45+
-- killed abruptly by an asynchronous exception, the children will still be
4646
-- taken down with it, though somewhat less ceremoniously in that case.
4747
--
4848
-- [Restart Strategies]
@@ -52,7 +52,7 @@
5252
-- (see below for the rules governing child restart eligibility). Each restart
5353
-- strategy comprises a 'RestartMode' and 'RestartLimit', which govern how
5454
-- the restart should be handled, and the point at which the supervisor
55-
-- should give up and terminate itself respectively.
55+
-- should give up and stop itself respectively.
5656
--
5757
-- With the exception of the @RestartOne@ strategy, which indicates that the
5858
-- supervisor will restart /only/ the one individual failing child, each
@@ -141,7 +141,7 @@
141141
-- restarts. In order prevent this, each restart strategy is parameterised
142142
-- with a 'RestartLimit' that caps the number of restarts allowed within a
143143
-- specific time period. If the supervisor exceeds this limit, it will stop,
144-
-- terminating all its children (in left-to-right order) and exit with the
144+
-- stopping all its children (in left-to-right order) and exit with the
145145
-- reason @ExitOther "ReachedMaxRestartIntensity"@.
146146
--
147147
-- The 'MaxRestarts' type is a positive integer, and together with a specified
@@ -152,31 +152,31 @@
152152
-- as a single restart attempt, since otherwise it would likely exceed its
153153
-- maximum restart intensity too quickly.
154154
--
155-
-- [Child Restart and Termination Policies]
155+
-- [Child Restart and Stop Policies]
156156
--
157157
-- When the supervisor detects that a child has died, the 'RestartPolicy'
158158
-- configured in the child specification is used to determin what to do. If
159159
-- the this is set to @Permanent@, then the child is always restarted.
160160
-- If it is @Temporary@, then the child is never restarted and the child
161161
-- specification is removed from the supervisor. A @Transient@ child will
162-
-- be restarted only if it terminates /abnormally/, otherwise it is left
162+
-- be restarted only if it exits /abnormally/, otherwise it is left
163163
-- inactive (but its specification is left in place). Finally, an @Intrinsic@
164164
-- child is treated like a @Transient@ one, except that if /this/ kind of child
165165
-- exits /normally/, then the supervisor will also exit normally.
166166
--
167-
-- When the supervisor does terminate a child, the 'ChildTerminationPolicy'
167+
-- When the supervisor does stop a child process, the "ChildStopPolicy"
168168
-- provided with the 'ChildSpec' determines how the supervisor should go
169-
-- about doing so. If this is @TerminateImmediately@, then the child will
169+
-- about doing so. If this is "StopImmediately", then the child will
170170
-- be killed without further notice, which means the child will /not/ have
171171
-- an opportunity to clean up any internal state and/or release any held
172-
-- resources. If the policy is @TerminateTimeout delay@ however, the child
172+
-- resources. If the policy is @StopTimeout delay@ however, the child
173173
-- will be sent an /exit signal/ instead, i.e., the supervisor will cause
174174
-- the child to exit via @exit childPid ExitShutdown@, and then will wait
175175
-- until the given @delay@ for the child to exit normally. If this does not
176176
-- happen within the given delay, the supervisor will revert to the more
177-
-- aggressive @TerminateImmediately@ policy and try again. Any errors that
177+
-- aggressive "StopImmediately" policy and try again. Any errors that
178178
-- occur during a timed-out shutdown will be logged, however exit reasons
179-
-- resulting from @TerminateImmediately@ are ignored.
179+
-- resulting from "StopImmediately" are ignored.
180180
--
181181
-- [Creating Child Specs]
182182
--
@@ -195,7 +195,7 @@
195195
-- who've manually registered with the /remote table/ and don't with to use
196196
-- tempate haskell (e.g. users of the Explicit closures API).
197197
--
198-
-- [Supervision Trees & Supervisor Termination]
198+
-- [Supervision Trees & Supervisor Shutdown]
199199
--
200200
-- To create a supervision tree, one simply adds supervisors below one another
201201
-- as children, setting the @childType@ field of their 'ChildSpec' to
@@ -211,7 +211,7 @@ module Control.Distributed.Process.Supervisor
211211
ChildSpec(..)
212212
, ChildKey
213213
, ChildType(..)
214-
, ChildTerminationPolicy(..)
214+
, ChildStopPolicy(..)
215215
, ChildStart(..)
216216
, RegisteredName(LocalName, CustomRegister)
217217
, RestartPolicy(..)
@@ -246,8 +246,8 @@ module Control.Distributed.Process.Supervisor
246246
, StartChildResult(..)
247247
, startChild
248248
, startNewChild
249-
, terminateChild
250-
, TerminateChildResult(..)
249+
, stopChild
250+
, StopChildResult(..)
251251
, deleteChild
252252
, DeleteChildResult(..)
253253
, restartChild
@@ -342,9 +342,7 @@ import Control.Distributed.Process.ManagedProcess.Server.Priority
342342
, prioritiseCall_
343343
, prioritiseInfo_
344344
, setPriority
345-
, runAfter
346-
, act
347-
, setProcessState
345+
, evalAfter
348346
)
349347
import Control.Distributed.Process.ManagedProcess.Server.Restricted
350348
( RestrictedProcess
@@ -481,10 +479,10 @@ data DelayedRestart = DelayedRestart !ChildKey !DiedReason
481479
instance Binary DelayedRestart where
482480
instance NFData DelayedRestart
483481

484-
data TerminateChildReq = TerminateChildReq !ChildKey
482+
data StopChildReq = StopChildReq !ChildKey
485483
deriving (Typeable, Generic, Show, Eq)
486-
instance Binary TerminateChildReq where
487-
instance NFData TerminateChildReq where
484+
instance Binary StopChildReq where
485+
instance NFData StopChildReq where
488486

489487
data IgnoreChildReq = IgnoreChildReq !ChildPid
490488
deriving (Typeable, Generic)
@@ -574,18 +572,18 @@ startNewChild :: Addressable a
574572
startNewChild addr spec = Unsafe.call addr $ AddChild True spec
575573

576574
-- | Delete a supervised child. The child must already be stopped (see
577-
-- 'terminateChild').
575+
-- 'stopChild').
578576
--
579577
deleteChild :: Addressable a => a -> ChildKey -> Process DeleteChildResult
580578
deleteChild sid childKey = Unsafe.call sid $ DeleteChild childKey
581579

582-
-- | Terminate a running child.
580+
-- | Stop a running child.
583581
--
584-
terminateChild :: Addressable a
582+
stopChild :: Addressable a
585583
=> a
586584
-> ChildKey
587-
-> Process TerminateChildResult
588-
terminateChild sid = Unsafe.call sid . TerminateChildReq
585+
-> Process StopChildResult
586+
stopChild sid = Unsafe.call sid . StopChildReq
589587

590588
-- | Forcibly restart a running child.
591589
--
@@ -595,7 +593,7 @@ restartChild :: Addressable a
595593
-> Process RestartChildResult
596594
restartChild sid = Unsafe.call sid . RestartChildReq
597595

598-
-- | Gracefully terminate a running supervisor. Returns immediately if the
596+
-- | Gracefully stop/shutdown a running supervisor. Returns immediately if the
599597
-- /address/ cannot be resolved.
600598
--
601599
shutdown :: Resolvable a => a -> Process ()
@@ -720,7 +718,7 @@ processDefinition =
720718
apiHandlers = [
721719
Restricted.handleCast handleIgnore
722720
-- adding, removing and (optionally) starting new child specs
723-
, handleCall handleTerminateChild
721+
, handleCall handleStopChild
724722
, Restricted.handleCall handleDeleteChild
725723
, Restricted.handleCallIf (input (\(AddChild immediate _) -> not immediate))
726724
handleAddChild
@@ -867,7 +865,7 @@ handleDelayedRestart state (DelayedRestart key reason) =
867865
let child = findChild key state in do
868866
case child of
869867
Nothing ->
870-
continue state -- a child could've been terminated and removed by now
868+
continue state -- a child could've been stopped and removed by now
871869
Just ((ChildRestarting childPid), spec) -> do
872870
-- TODO: we ignore the unnecessary .active re-assignments in
873871
-- tryRestartChild, in order to keep the code simple - it would be good to
@@ -876,18 +874,18 @@ handleDelayedRestart state (DelayedRestart key reason) =
876874
Just other -> do
877875
die $ ExitOther $ (supErrId ".handleDelayedRestart:InvalidState: ") ++ (show other)
878876

879-
handleTerminateChild :: State
880-
-> TerminateChildReq
881-
-> Process (ProcessReply TerminateChildResult State)
882-
handleTerminateChild state (TerminateChildReq key) =
877+
handleStopChild :: State
878+
-> StopChildReq
879+
-> Process (ProcessReply StopChildResult State)
880+
handleStopChild state (StopChildReq key) =
883881
let child = findChild key state in
884882
case child of
885883
Nothing ->
886-
reply TerminateChildUnknownId state
884+
reply StopChildUnknownId state
887885
Just (ChildStopped, _) ->
888-
reply TerminateChildOk state
886+
reply StopChildOk state
889887
Just (ref, spec) ->
890-
reply TerminateChildOk =<< doTerminateChild ref spec state
888+
reply StopChildOk =<< doStopChild ref spec state
891889

892890
handleGetStats :: StatsReq
893891
-> RestrictedProcess State (Result SupervisorStats)
@@ -916,8 +914,8 @@ handleMonitorSignal state (ProcessMonitorNotification _ childPid reason) = do
916914
--------------------------------------------------------------------------------
917915

918916
handleShutdown :: ExitState State -> ExitReason -> Process ()
919-
handleShutdown state r@(ExitOther reason) = terminateChildren (exitState state) r >> die reason
920-
handleShutdown state r = terminateChildren (exitState state) r
917+
handleShutdown state r@(ExitOther reason) = stopChildren (exitState state) r >> die reason
918+
handleShutdown state r = stopChildren (exitState state) r
921919

922920
--------------------------------------------------------------------------------
923921
-- Child Start/Restart Handling --
@@ -1003,14 +1001,14 @@ tryRestartBranch rs sp dr st = -- TODO: use DiedReason for logging...
10031001
us <- getSelfPid
10041002
cPid <- resolve cr
10051003
report $ SupervisedChildRestarting us cPid (childKey cs) (ExitOther "RestartedBySupervisor")
1006-
doTerminateChild cr cs s >>= (flip startIt) ch
1004+
doStopChild cr cs s >>= (flip startIt) ch
10071005

10081006
stopIt :: State -> Child -> Process State
10091007
stopIt s (cr, cs) = do
10101008
us <- getSelfPid
10111009
cPid <- resolve cr
10121010
report $ SupervisedChildRestarting us cPid (childKey cs) (ExitOther "RestartedBySupervisor")
1013-
doTerminateChild cr cs s
1011+
doStopChild cr cs s
10141012

10151013
startIt :: State -> Child -> Process State
10161014
startIt s (_, cs)
@@ -1077,25 +1075,25 @@ tryRestartBranch rs sp dr st = -- TODO: use DiedReason for logging...
10771075
LeftToRight -> tree
10781076
RightToLeft -> Seq.reverse tree
10791077
1080-
-- TODO: THIS IS INCORRECT... currently (below), we terminate
1078+
-- TODO: THIS IS INCORRECT... currently (below), we stop
10811079
-- the branch in parallel, but wait on all the exits and then
10821080
-- restart sequentially (based on 'order'). That's not what the
10831081
-- 'RestartParallel' mode advertised, but more importantly, it's
10841082
-- not clear what the semantics for error handling (viz restart errors)
10851083
-- should actually be.
10861084
1087-
asyncs <- forM (toList tree') $ \ch -> async $ asyncTerminate ch
1085+
asyncs <- forM (toList tree') $ \ch -> async $ asyncStop ch
10881086
(_errs, st') <- foldlM collectExits ([], activeState) asyncs
10891087
-- TODO: report errs
10901088
apply $ foldlM startIt st' tree'
10911089
where
1092-
asyncTerminate :: Child -> Process (Maybe (ChildKey, ChildPid))
1093-
asyncTerminate (cr, cs) = do
1090+
asyncStop :: Child -> Process (Maybe (ChildKey, ChildPid))
1091+
asyncStop (cr, cs) = do
10941092
mPid <- resolve cr
10951093
case mPid of
10961094
Nothing -> return Nothing
10971095
Just childPid -> do
1098-
void $ doTerminateChild cr cs activeState
1096+
void $ doStopChild cr cs activeState
10991097
return $ Just (childKey cs, childPid)
11001098
11011099
collectExits :: ([ExitReason], State)
@@ -1190,13 +1188,13 @@ doRestartDelay :: ChildPid
11901188
-> State
11911189
-> Process (ProcessAction State)
11921190
doRestartDelay oldPid rDelay spec reason state = do
1193-
act $ do
1194-
void $ runAfter rDelay $ DelayedRestart (childKey spec) reason
1195-
setProcessState $ ( (active ^: Map.filter (/= chKey))
1196-
. (bumpStats Active chType decrement)
1197-
. (restarts ^= [])
1198-
$ maybe state id (updateChild chKey (setChildRestarting oldPid) state)
1199-
)
1191+
evalAfter rDelay
1192+
(DelayedRestart (childKey spec) reason)
1193+
$ ( (active ^: Map.filter (/= chKey))
1194+
. (bumpStats Active chType decrement)
1195+
. (restarts ^= [])
1196+
$ maybe state id (updateChild chKey (setChildRestarting oldPid) state)
1197+
)
12001198
where
12011199
chKey = childKey spec
12021200
chType = childType spec
@@ -1335,39 +1333,39 @@ filterInitFailures sup childPid ex = do
13351333
ChildInitIgnore -> Unsafe.cast sup $ IgnoreChildReq childPid
13361334

13371335
--------------------------------------------------------------------------------
1338-
-- Child Termination/Shutdown --
1336+
-- Child Stop/Shutdown --
13391337
--------------------------------------------------------------------------------
13401338

1341-
terminateChildren :: State -> ExitReason -> Process ()
1342-
terminateChildren state er = do
1339+
stopChildren :: State -> ExitReason -> Process ()
1340+
stopChildren state er = do
13431341
us <- getSelfPid
13441342
let strat = shutdownStrategy state
13451343
report $ SupervisorShutdown us strat er
13461344
case strat of
13471345
ParallelShutdown -> do
13481346
let allChildren = toList $ state ^. specs
13491347
terminatorPids <- forM allChildren $ \ch -> do
1350-
pid <- spawnLocal $ void $ syncTerminate ch $ (active ^= Map.empty) state
1348+
pid <- spawnLocal $ void $ syncStop ch $ (active ^= Map.empty) state
13511349
mRef <- monitor pid
13521350
return (mRef, pid)
13531351
terminationErrors <- collectExits [] $ zip terminatorPids (map snd allChildren)
1354-
-- it seems these would also be logged individually in doTerminateChild
1352+
-- it seems these would also be logged individually in doStopChild
13551353
case terminationErrors of
13561354
[] -> return ()
13571355
_ -> do
13581356
sup <- getSelfPid
13591357
void $ logEntry Log.error $
1360-
mkReport "Errors in terminateChildren / ParallelShutdown"
1358+
mkReport "Errors in stopChildren / ParallelShutdown"
13611359
sup "n/a" (show terminationErrors)
13621360
SequentialShutdown ord -> do
13631361
let specs' = state ^. specs
13641362
let allChildren = case ord of
13651363
RightToLeft -> Seq.reverse specs'
13661364
LeftToRight -> specs'
1367-
void $ foldlM (flip syncTerminate) state (toList allChildren)
1365+
void $ foldlM (flip syncStop) state (toList allChildren)
13681366
where
1369-
syncTerminate :: Child -> State -> Process State
1370-
syncTerminate (cr, cs) state' = doTerminateChild cr cs state'
1367+
syncStop :: Child -> State -> Process State
1368+
syncStop (cr, cs) state' = doStopChild cr cs state'
13711369

13721370
collectExits :: [(ProcessId, DiedReason)]
13731371
-> [((MonitorRef, ProcessId), ChildSpec)]
@@ -1385,13 +1383,13 @@ terminateChildren state er = do
13851383
(DiedNormal, _) -> collectExits errors remaining
13861384
(_, Nothing) -> collectExits errors remaining
13871385
(DiedException _, Just sp') -> do
1388-
if (childStop sp') == TerminateImmediately
1386+
if (childStop sp') == StopImmediately
13891387
then collectExits errors remaining
13901388
else collectExits ((pid, reason):errors) remaining
13911389
_ -> collectExits ((pid, reason):errors) remaining
13921390

1393-
doTerminateChild :: ChildRef -> ChildSpec -> State -> Process State
1394-
doTerminateChild ref spec state = do
1391+
doStopChild :: ChildRef -> ChildSpec -> State -> Process State
1392+
doStopChild ref spec state = do
13951393
us <- getSelfPid
13961394
mPid <- resolve ref
13971395
case mPid of
@@ -1412,16 +1410,16 @@ doTerminateChild ref spec state = do
14121410
chKey = childKey spec
14131411
updateStopped = maybe state id $ updateChild chKey (setChildStopped False) state
14141412

1415-
childShutdown :: ChildTerminationPolicy
1413+
childShutdown :: ChildStopPolicy
14161414
-> ChildPid
14171415
-> State
14181416
-> Process DiedReason
14191417
childShutdown policy childPid st = mask $ \restore -> do
14201418
case policy of
1421-
(TerminateTimeout t) -> exit childPid ExitShutdown >> await restore childPid t st
1419+
(StopTimeout t) -> exit childPid ExitShutdown >> await restore childPid t st
14221420
-- we ignore DiedReason for brutal kills
1423-
TerminateImmediately -> do
1424-
kill childPid "TerminatedBySupervisor"
1421+
StopImmediately -> do
1422+
kill childPid "StoppedBySupervisor"
14251423
void $ await restore childPid Infinity st
14261424
return DiedNormal
14271425
where
@@ -1436,7 +1434,7 @@ childShutdown policy childPid st = mask $ \restore -> do
14361434
Delay t -> receiveTimeout (asTimeout t) (matches mRef)
14371435
-- let recv' = if monitored then recv else withMonitor childPid' recv
14381436
res <- recv `finally` (unmonitor mRef)
1439-
restore' $ maybe (childShutdown TerminateImmediately childPid' state) return res
1437+
restore' $ maybe (childShutdown StopImmediately childPid' state) return res
14401438

14411439
matches :: MonitorRef -> [Match DiedReason]
14421440
matches m = [

0 commit comments

Comments
 (0)