diff --git a/dbus/methods.go b/dbus/methods.go index 074148cb..10bafa9c 100644 --- a/dbus/methods.go +++ b/dbus/methods.go @@ -67,12 +67,27 @@ func (c *Conn) startJob(ctx context.Context, ch chan<- string, job string, args c.jobListener.jobs[p] = ch } + go c.observeContextCancellation(ctx, p) + // ignore error since 0 is fine if conversion fails jobID, _ := strconv.Atoi(path.Base(string(p))) return jobID, nil } +func (c *Conn) observeContextCancellation(ctx context.Context, jobId dbus.ObjectPath) { + <-ctx.Done() + + // remove job listener if one exists + c.jobListener.Lock() + defer c.jobListener.Unlock() + + _, ok := c.jobListener.jobs[jobId] + if ok { + delete(c.jobListener.jobs, jobId) + } +} + // Deprecated: use StartUnitContext instead. func (c *Conn) StartUnit(name string, mode string, ch chan<- string) (int, error) { return c.StartUnitContext(context.Background(), name, mode, ch)