networkmanager: the watcher redials the system bus only at its socket's event

A refused or dropped system bus is dialled again only at the file event that makes its
socket and at rating-again, never because the socket is merely there, so a bus that
refuses the watcher is not dialled in a loop.

Builds: design 08 sign-in-page §14; lead answer note (d) (hq df22a138)
This commit is contained in:
jochen
2026-11-24 21:50:49 +01:00
parent 42b5a7aef3
commit 55a90d963e
3 changed files with 97 additions and 56 deletions
@@ -136,57 +136,11 @@ func (d *dbusSystem) rating() (Rating, error) {
return r, nil
}
// systemSocketThere fires once the system bus's socket is there: at once when it is, else when it is made in its
// folder (a file event of the kernel). Closing stop ends the wait.
// 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.
func systemSocketThere(stop <-chan struct{}) <-chan struct{} {
return fileThere(systemSocket, stop)
}
// fileThere fires once path is there: at once when it is, else at the file event that makes it in its folder.
func fileThere(path string, stop <-chan struct{}) <-chan struct{} {
out := make(chan struct{})
fd, err := unix.InotifyInit1(unix.IN_CLOEXEC | unix.IN_NONBLOCK)
if err != nil {
journal("watching %s failed: %v", filepath.Dir(path), err)
return out
}
if _, err := unix.InotifyAddWatch(fd, filepath.Dir(path), unix.IN_CREATE|unix.IN_MOVED_TO); err != nil {
journal("watching %s failed: %v", filepath.Dir(path), err)
unix.Close(fd)
return out
}
f := os.NewFile(uintptr(fd), "inotify")
go func() {
<-stop
f.Close()
}()
go func() {
defer f.Close()
if _, err := os.Stat(path); err == nil {
sendOnce(out, stop)
return
}
for {
names, err := readInotify(f)
if err != nil {
return
}
for _, name := range names {
if name == filepath.Base(path) {
sendOnce(out, stop)
return
}
}
}
}()
return out
}
func sendOnce(out chan<- struct{}, stop <-chan struct{}) {
select {
case out <- struct{}{}:
case <-stop:
}
return fileMade(filepath.Dir(systemSocket), filepath.Base(systemSocket), stop)
}
// readInotify reads the names of one read's file events; an error once the descriptor is closed.
@@ -133,8 +133,8 @@ type Watcher struct {
Folder Folder
// Dial connects to the system bus.
Dial func() (systemBus, error)
// SocketThere fires once the system bus's socket is there: at once when it is, else at the file event of its
// folder; closing stop ends the wait.
// 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
// UID is the watcher's account, whose sessions are the person's.
@@ -2,7 +2,10 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"testing"
@@ -17,10 +20,12 @@ type fakeSystem struct {
mu sync.Mutex
now Rating
sessions []loginSession
reads int
dials int
buses []*fakeBus
socket chan struct{}
// refuse is how many dials, from the next, are refused.
refuse int
reads int
dials int
buses []*fakeBus
socket chan struct{}
}
type fakeBus struct {
@@ -33,6 +38,10 @@ func (s *fakeSystem) dial() (systemBus, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.dials++
if s.refuse > 0 {
s.refuse--
return nil, errors.New("connection refused")
}
b := &fakeBus{sys: s, signals: make(chan signal)}
s.buses = append(s.buses, b)
return b, nil
@@ -382,3 +391,81 @@ func TestTheWatcherHearsTheSystemBusSignals(t *testing.T) {
}
}
}
// The watcher dials the system bus again only at the file event that makes its socket and at rating-again, never in a
// loop: a bus that refuses it, at its start or after a drop, is not dialled again until one of the two (§14; note d).
func TestTheWatcherRedialsOnlyAtTheSocketsEventAndRatingAgain(t *testing.T) {
journalLines(t)
sys := &fakeSystem{socket: make(chan struct{}), refuse: 1}
sys.set(guest(ratingFull))
w := aWatcher(t, sys)
stop, done := make(chan struct{}), make(chan struct{})
go func() { w.run(stop); close(done) }()
defer func() { close(stop); <-done }()
state := func() watcherState {
s, _ := w.askLoop(askState)
return s.(watcherState)
}
dials := func() int { _, d := sys.counts(); return d }
for range 3 {
if s := state(); s.Subscribed {
t.Fatal("subscribed through a refused dial")
}
}
if d := dials(); d != 1 {
t.Fatalf("a refused bus was dialled %d times with no event", d)
}
sys.mu.Lock()
sys.refuse = 1
sys.mu.Unlock()
sys.socket <- struct{}{}
state()
if d := dials(); d != 2 || state().Subscribed {
t.Fatalf("at the socket's event: %d dials", d)
}
state()
if d := dials(); d != 2 {
t.Fatalf("dialled again with no event: %d dials", d)
}
sys.socket <- struct{}{}
if s := state(); !s.Subscribed || dials() != 3 {
t.Fatalf("at the next socket event: %+v, %d dials", s, dials())
}
close(sys.last().signals)
state()
if d := dials(); d != 3 {
t.Fatalf("a dropped connection was dialled again before the socket's event: %d dials", d)
}
if _, err := w.askLoop(askRatingAgain); err != nil || dials() != 4 || !state().Subscribed {
t.Fatalf("rating-again: %v, %d dials", err, dials())
}
}
// 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)
dir := t.TempDir()
was := systemSocket
systemSocket = filepath.Join(dir, "system_bus_socket")
t.Cleanup(func() { systemSocket = was })
if err := os.WriteFile(systemSocket, nil, 0o600); err != nil {
t.Fatal(err)
}
stop := make(chan struct{})
defer close(stop)
there := systemSocketThere(stop)
select {
case <-there:
t.Fatal("fired for a socket that was already there")
default:
}
os.WriteFile(filepath.Join(dir, "other"), nil, 0o600)
os.Remove(systemSocket)
if err := os.WriteFile(systemSocket+".made", nil, 0o600); err != nil {
t.Fatal(err)
}
if err := os.Rename(systemSocket+".made", systemSocket); err != nil {
t.Fatal(err)
}
<-there
}