go-systemd
go-systemd copied to clipboard
StartUnitContext did not respect context in matter of the jobComplete signal
Conn.StartUnitContext, which actually calls internally startJob uses the given context only for the dbus call but not for the job listener. https://github.com/coreos/go-systemd/blob/d843340ab4bd3815fda02e648f9b09ae2dc722a7/dbus/methods.go#L61 This call returns nearly immediately in most cases, when dbus and systemd is reachable. But the jobComplete signal is only received after the system job is finished, which can take more time.
So the jobListener should be removed after the context is finished. Like this:
if ch != nil {
c.jobListener.jobs[p] = ch
go func(ctx context.Context, p dbus.ObjectPath) {
<-ctx.Done()
c.jobListener.Lock()
defer c.jobListener.Unlock()
_, ok := c.jobListener.jobs[p]
if !ok {
return
}
fmt.Println("delete job listener after context is done")
delete(c.jobListener.jobs, dbus.ObjectPath(p))
}(ctx, p)
}
But this not ideal, because it creates a go function for each call.