Skip to content

Commit 1c727a9

Browse files
committed
fix: runner defer executed too early
The recent separation of a blocking Run() and a non-blocking RunBackground() introduced a bug that prematurely executed registered defer functions on the runner. This is a critical error that is now fixed by having RunBackground return the defer function as well.
1 parent be512c0 commit 1c727a9

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

rolling-shutter/keyper/epochkghandler/trigger_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestHandleDecryptionTriggerIntegration(t *testing.T) {
4444
Messaging: messaging,
4545
Trigger: decrTrigChan,
4646
}
47-
group := service.RunBackground(
47+
group, cleanup := service.RunBackground(
4848
ctx,
4949
ksh,
5050
)
@@ -58,6 +58,7 @@ func TestHandleDecryptionTriggerIntegration(t *testing.T) {
5858
decrTrigChan <- broker.NewEvent(trig)
5959
close(decrTrigChan)
6060
err = group.Wait()
61+
cleanup()
6162
assert.NilError(t, err)
6263

6364
// send decryption key share when first trigger is received

rolling-shutter/keyperimpl/rollup/trigger_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ func TestHandleDecryptionTriggerIntegration(t *testing.T) {
4747
assert.NilError(t, err)
4848
sender.AddMessageHandler(triggerHandler)
4949

50-
group := service.RunBackground(
50+
group, cleanup := service.RunBackground(
5151
ctx,
5252
sender,
5353
)
54+
defer cleanup()
5455

5556
testsetup.InitializeEon(ctx, t, dbpool, config, keyperIndex)
5657

rolling-shutter/medley/service/service.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,22 @@ func (r *runner) runShutdownFuncs() {
6565
}
6666

6767
func Run(ctx context.Context, services ...Service) error {
68-
group := RunBackground(ctx, services...)
68+
group, deferFn := RunBackground(ctx, services...)
69+
defer deferFn()
6970
return group.Wait()
7071
}
7172

72-
func RunBackground(ctx context.Context, services ...Service) *errgroup.Group {
73+
// RunBackground runs the services within the context of an errgroup.Group.
74+
// It returns the errgroup.Group as well as the cleanup function to be deferred.
75+
// This is a low-level executor of services, the defer function is expected
76+
// to be called!
77+
func RunBackground(ctx context.Context, services ...Service) (*errgroup.Group, func()) {
7378
group, ctx := errgroup.WithContext(ctx)
7479
r := runner{group: group, ctx: ctx}
7580
group.Go(func() error {
7681
return r.StartService(services...)
7782
})
78-
defer r.runShutdownFuncs()
79-
return group
83+
return group, r.runShutdownFuncs
8084
}
8185

8286
// notifyTermination creates a context that is canceled, when the process receives SIGINT or

0 commit comments

Comments
 (0)