Skip to content

Commit ede96b7

Browse files
committed
oversight: handle deletion of completed childprocs
1 parent f8b311d commit ede96b7

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

tree.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,15 @@ func (t *Tree) Terminate(name string) error {
463463
// trees, if the tree is not started yet, it is going to block. If the tree is
464464
// halted, it is going to fail with ErrTreeNotRunning.
465465
func (t *Tree) Delete(name string) error {
466-
if err := t.Terminate(name); err != nil {
466+
if err := t.Terminate(name); err != nil && !errors.Is(err, ErrProcessNotRunning) {
467467
return err
468468
}
469469
t.semaphore.Lock()
470470
defer t.semaphore.Unlock()
471471
t.childrenOrder = slices.DeleteFunc(t.childrenOrder, func(cp *childProcess) bool {
472472
return cp.spec.Name == name
473473
})
474+
delete(t.children, name)
474475
return nil
475476
}
476477

tree_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -1148,3 +1148,36 @@ func TestTerminateNoExistingChildProcess(t *testing.T) {
11481148
t.Fatal("unexpected error", err)
11491149
}
11501150
}
1151+
1152+
func Test_deleteFinishedTransient(t *testing.T) {
1153+
tree := oversight.New(
1154+
oversight.NeverHalt(),
1155+
)
1156+
ready := make(chan struct{})
1157+
tree.Add(oversight.ChildProcessSpecification{
1158+
Restart: oversight.Permanent(),
1159+
Start: func(ctx context.Context) error {
1160+
<-ctx.Done()
1161+
return nil
1162+
},
1163+
})
1164+
tree.Add(oversight.ChildProcessSpecification{
1165+
Name: "failed-transient",
1166+
Restart: oversight.Transient(),
1167+
Start: func(context.Context) error {
1168+
t.Log("running transient process")
1169+
defer close(ready)
1170+
return nil
1171+
},
1172+
})
1173+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1174+
t.Cleanup(cancel)
1175+
go tree.Start(ctx)
1176+
<-ready
1177+
time.Sleep(1 * time.Second)
1178+
err := tree.Delete("failed-transient")
1179+
if err != nil {
1180+
t.Fatal(err)
1181+
}
1182+
<-ctx.Done()
1183+
}

0 commit comments

Comments
 (0)