Skip to content

Commit

Permalink
dbus/methods: observe context for Cancellation to remove job listener
Browse files Browse the repository at this point in the history
Oberserve the given context from the function e.g. StartUnitContext,
which are using the internal function startJob. If this context is done
remove the job Listener if existing.

Fixes coreos#428
  • Loading branch information
j-licht committed Oct 13, 2023
1 parent d843340 commit 085c827
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions dbus/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 085c827

Please sign in to comment.