K.Liu on Discord: `magpie web` next to the systemd `magpie daemon` logged
"gateway: listen tcp 0.0.0.0:3425: bind: address already in use".
Cause: before listening, serveGatewayLocked asks the gateway's address
whether a magpie serves it (ServedBy, one GET with a 700ms timeout). A
magpie that holds the port but doesn't answer in time (busy on a small
server, or still starting) reads as "nobody", so the page's process
listened itself and the bind failed. With the service answering, no
second gateway was started and the page's state matched.
Now, after ServedBy says no, gateway.Taken() dials the address (500ms).
When something accepts, this magpie doesn't listen; it logs once that the
port is taken and leaves it to the 15s watch, which serves the gateway
once the port is free.
Run:
- Sandbox HOME, settings {"port":3461,"lan":true}: `magpie serve`, then
`kill -STOP` it, then `magpie web --no-open --addr 127.0.0.1:3462`. The
old binary printed the exact bind error. The new one logs "is taken but
doesn't answer as magpie yet" and doesn't bind. After SIGCONT,
/v1/models and the page's /api/state show the same 3 models. With the
serve killed, the page's process took 3461 over in about 16s.
- TestGatewayNotServedBesideASlowOne (a listener whose handler blocks):
it goes red with the Taken check reverted by hand.
- gofmt -l; go vet (gui), and go vet -tags nogui for darwin, linux and
windows, on cli, daemon, gui and gateway; go test -tags nogui ./... in a
sandbox HOME.
102 lines
2.8 KiB
Go
102 lines
2.8 KiB
Go
package gui
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
// A magpie that found the gateway taken serves it once the one that had it
|
|
// is gone (one left running from before an update, a magpie serve).
|
|
func TestGatewayTakenOver(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("MAGPIE_ADDR", ln.Addr().String())
|
|
other := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte(`{"name":"magpie"}`))
|
|
})}
|
|
go other.Serve(ln)
|
|
defer other.Close()
|
|
// the watch and the gateway it takes up end with the test, before its
|
|
// HOME and MAGPIE_ADDR are put back
|
|
ctx, stop := context.WithCancel(context.Background())
|
|
watched := make(chan struct{})
|
|
t.Cleanup(func() {
|
|
stop()
|
|
<-watched
|
|
gatewayMu.Lock()
|
|
defer gatewayMu.Unlock()
|
|
if served.Load() != nil && servedRun.stop != nil {
|
|
servedRun.stop()
|
|
<-servedRun.done
|
|
}
|
|
served.Store(nil)
|
|
})
|
|
go func() {
|
|
defer close(watched)
|
|
watchGateway(ctx, 50*time.Millisecond)
|
|
}()
|
|
if serveGateway() != nil {
|
|
t.Fatal("served over another magpie")
|
|
}
|
|
time.Sleep(200 * time.Millisecond)
|
|
if served.Load() != nil {
|
|
t.Fatal("took the gateway while the other had it")
|
|
}
|
|
other.Close()
|
|
for deadline := time.Now().Add(5 * time.Second); served.Load() == nil || !gateway.Running(); time.Sleep(50 * time.Millisecond) {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("the gateway wasn't taken up")
|
|
}
|
|
}
|
|
}
|
|
|
|
// A magpie that holds the gateway's port but doesn't answer in time (the
|
|
// service busy on a small server, or starting) still has it: magpie web
|
|
// beside it doesn't listen a second time, which only failed with "bind:
|
|
// address already in use" (K.Liu on Discord), and leaves the port to it.
|
|
func TestGatewayNotServedBesideASlowOne(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("MAGPIE_ADDR", ln.Addr().String())
|
|
slow := make(chan struct{})
|
|
other := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
<-slow
|
|
w.Write([]byte(`{"name":"magpie"}`))
|
|
})}
|
|
go other.Serve(ln)
|
|
defer other.Close()
|
|
defer close(slow)
|
|
gw := serveGateway()
|
|
t.Cleanup(func() {
|
|
gatewayMu.Lock()
|
|
defer gatewayMu.Unlock()
|
|
if served.Load() != nil && servedRun.stop != nil {
|
|
servedRun.stop()
|
|
<-servedRun.done
|
|
}
|
|
served.Store(nil)
|
|
takenSaid.Store(false)
|
|
})
|
|
if gw != nil {
|
|
t.Fatal("listened for the gateway on a port another magpie holds")
|
|
}
|
|
}
|