From 8fb7dd9f89101e11c5d13ab4ef152c1018a8cace Mon Sep 17 00:00:00 2001 From: PJ Date: Sat, 6 Jun 2026 23:51:53 +0530 Subject: [PATCH] fix(ioscompanion): spawn companion under driver-lifetime context --- internal/driver/ioscompanion/driver.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/driver/ioscompanion/driver.go b/internal/driver/ioscompanion/driver.go index 8e0d589..7ad60e2 100644 --- a/internal/driver/ioscompanion/driver.go +++ b/internal/driver/ioscompanion/driver.go @@ -101,6 +101,13 @@ type Driver struct { spawnChild func(ctx context.Context, address string) (*exec.Cmd, error) dial func(address string) (transport.Companion, error) child *exec.Cmd + + // processContext owns the companion child's lifetime: it is derived from + // New's context (so a canceled run still reaps the child) and canceled by + // Close. Spawning under a startup-scoped context would SIGTERM the child + // the moment startup finishes. + processContext context.Context + processCancel context.CancelFunc } // New extracts the embedded companion, spawns it against the configured @@ -146,6 +153,7 @@ func New(ctx context.Context, options Options) (*Driver, error) { driverInstance.address = address driverInstance.restart = driverInstance.respawnAndRedial driverInstance.resetContainer = driverInstance.resetDataContainer + driverInstance.processContext, driverInstance.processCancel = context.WithCancel(ctx) if err := driverInstance.bringUp(ctx); err != nil { return nil, err @@ -167,7 +175,7 @@ func (d *Driver) bringUp(ctx context.Context) error { startupCtx, cancel := context.WithTimeout(ctx, startupTimeout) defer cancel() - child, err := d.spawnChild(startupCtx, d.address) + child, err := d.spawnChild(d.processContext, d.address) if err != nil { return fmt.Errorf("spawn companion: %w", err) } @@ -629,6 +637,9 @@ func (d *Driver) Close() { d.companion = nil } d.stopChild() + if d.processCancel != nil { + d.processCancel() + } } // stopChild terminates the companion child gracefully (SIGTERM, grace window,