networkmanager: a watcher refused at its start by a bus that is there exits

When the first subscribe fails while the system bus's socket is already there, no file
event will make it again: the watcher writes the reason to its journal and exits
non-zero, so its unit shows failing instead of staying active and deaf.

Builds: design 08 sign-in-page §14; lead answer C2-1 (hq f15bc2fd)
This commit is contained in:
jochen
2026-11-25 01:12:23 +01:00
parent 397e627b02
commit 349ef43a36
3 changed files with 43 additions and 2 deletions
@@ -136,6 +136,12 @@ func (d *dbusSystem) rating() (Rating, error) {
return r, nil
}
// systemSocketExists says whether the system bus's socket is there now.
func systemSocketExists() bool {
_, err := os.Stat(systemSocket)
return err == nil
}
// systemSocketThere fires at each file event of the kernel that makes the system bus's socket in its folder, until stop
// closes: the one moment, beside rating-again, the watcher dials the system bus again (§14; note d). It never fires
// for a socket that is merely there, so a bus that refuses the watcher is not dialled again in a loop.
@@ -136,7 +136,9 @@ type Watcher struct {
// SocketThere fires at the file event of the kernel that makes the system bus's socket in its folder; closing stop
// ends the wait.
SocketThere func(stop <-chan struct{}) <-chan struct{}
Now func() time.Time
// SocketExists says whether the system bus's socket is there now.
SocketExists func() bool
Now func() time.Time
// UID is the watcher's account, whose sessions are the person's.
UID uint32
// Environment reads the user manager's environment over a session's bus.
@@ -170,7 +172,7 @@ type Watcher struct {
func newWatcher(f Folder) *Watcher {
w := &Watcher{Folder: f, Dial: dialSystemBus, SocketThere: systemSocketThere, Now: time.Now, asks: make(chan asked),
UID: uint32(os.Getuid()), Environment: userEnvironment, FileEvents: fileEvents, After: time.After,
UID: uint32(os.Getuid()), SocketExists: systemSocketExists, Environment: userEnvironment, FileEvents: fileEvents, After: time.After,
Desk: newSessionDesk(), ends: make(chan func()), Made: pathsMade}
w.Beside = w.besideLoop
return w
@@ -220,6 +222,13 @@ func (w *Watcher) run(stop <-chan struct{}) error {
w.strange(p)
}
w.subscribe()
if !w.state.Subscribed && w.SocketExists != nil && w.SocketExists() {
// The socket is there and refused the watcher: no file event will make it again, so waiting would leave the
// unit active and the watcher deaf (design 08 sign-in-page §14; lead answer C2-1).
w.unsubscribe()
journal("the system bus's socket is there and the watcher could not subscribe on it at its start")
return errors.New("the system bus's socket is there and the watcher could not subscribe on it at its start")
}
for {
select {
case <-stop:
@@ -439,6 +439,32 @@ func TestTheWatcherRedialsOnlyAtTheSocketsEventAndRatingAgain(t *testing.T) {
close(stop)
}
// When its first subscribe at its start fails while the system bus's socket is there, no file event will come: the
// watcher writes the reason to its journal and ends with an error, so its process exits non-zero (§14; lead answer
// C2-1). With no socket there it waits for the one that makes it, as above.
func TestARefusedStartWithTheSocketThereEndsTheWatcher(t *testing.T) {
lines := journalLines(t)
sys := &fakeSystem{socket: make(chan struct{}), refuse: 1}
w := aWatcher(t, sys)
w.SocketExists = func() bool { return true }
ended := make(chan error, 1)
go func() { ended <- w.run(make(chan struct{})) }()
answered := make(chan struct{})
go func() { w.askLoop(askState); close(answered) }()
var err error
select {
case err = <-ended:
case <-answered:
t.Fatal("the watcher waits for a file event that will not come, its unit left active")
}
if _, dials := sys.counts(); err == nil || !strings.Contains(err.Error(), "at its start") || dials != 1 {
t.Fatalf("the watcher ended with %v after %d dials", err, dials)
}
if !strings.Contains(strings.Join(lines(), "\n"), "could not subscribe on it at its start") {
t.Fatalf("the reason is not in the journal: %q", lines())
}
}
// The real wait fires at the file event that makes the system bus's socket, not for one that is merely there (note d).
func TestTheSocketsWaitFiresWhenTheSocketIsMade(t *testing.T) {
journalLines(t)