Lightweight server monitoring with historical data, docker stats, and alerts.
fix(agent): bind connection lifecycle to a cancellable context (#2464)
user010101114aee1668fe67
diff --git a/agent/agent.go b/agent/agent.goindex 0ba042a0a..e9cce805c 100644--- a/agent/agent.go+++ b/agent/agent.go@@ -5,6 +5,7 @@package agentimport (+ "context""log/slog""net""strings"@@ -271,10 +272,11 @@ func (a *Agent) gatherStats(options common.DataRequestOptions) *system.CombinedDreturn a.attachSystemDetails(data, cacheTimeMs, options.IncludeDetails)}-// Start initializes and starts the agent with optional WebSocket connection-func (a *Agent) Start(serverOptions ServerOptions) error {+// Start initializes and starts the agent with optional WebSocket connection.+// It runs until ctx is cancelled or the process receives SIGINT or SIGTERM.+func (a *Agent) Start(ctx context.Context, serverOptions ServerOptions) error {a.keys = serverOptions.Keys- err := a.connectionManager.Start(serverOptions)+ err := a.connectionManager.Start(ctx, serverOptions)if err != nil {a.cleanupSensorShadow()}diff --git a/agent/agent_test_helpers.go b/agent/agent_test_helpers.goindex 6340586b3..ef6f73be5 100644--- a/agent/agent_test_helpers.go+++ b/agent/agent_test_helpers.go@@ -6,3 +6,8 @@ package agentfunc (a *Agent) GetConnectionManager() *ConnectionManager {return a.connectionManager}++// TESTING ONLY: GetState returns a synchronised snapshot of the connection state.+func (c *ConnectionManager) GetState() ConnectionState {+ return c.getState()+}diff --git a/agent/client.go b/agent/client.goindex ca42978e9..280a588ca 100644--- a/agent/client.go+++ b/agent/client.go@@ -1,6 +1,7 @@package agentimport (+ "context""crypto/tls""crypto/x509""errors"@@ -233,7 +234,7 @@ func (client *WebSocketClient) getOptions() *gws.ClientOption {"X-Beszel": []string{beszel.Version},},NewDialer: func() (gws.Dialer, error) {- return proxy.FromEnvironment(), nil+ return agentDialer{client.agent.connectionManager.ctx}, nil},}@@ -249,6 +250,31 @@ func (client *WebSocketClient) getOptions() *gws.ClientOption {return client.options}+// agentDialer keeps proxy selection while binding dialling and the resulting+// connection to the agent lifetime, including a pending WebSocket handshake.+type agentDialer struct{ ctx context.Context }++func (d agentDialer) Dial(network, addr string) (net.Conn, error) {+ conn, err := proxy.Dial(d.ctx, network, addr)+ if err != nil {+ return nil, err+ }+ return &agentConn{+ Conn: conn,+ stop: context.AfterFunc(d.ctx, func() { _ = conn.Close() }),+ }, nil+}++type agentConn struct {+ net.Conn+ stop func() bool+}++func (c *agentConn) Close() error {+ c.stop()+ return c.Conn.Close()+}+// Connect establishes a WebSocket connection to the hub.// It closes any existing connection before attempting to reconnect.func (client *WebSocketClient) Connect() (err error) {@@ -291,7 +317,7 @@ func (client *WebSocketClient) OnClose(conn *gws.Conn, err error) {if err != nil {slog.Warn("Connection closed", "err", strings.TrimPrefix(err.Error(), "gws: "))}- client.agent.connectionManager.eventChan <- WebSocketDisconnect+ client.agent.connectionManager.sendEvent(WebSocketDisconnect)}// OnMessage handles incoming WebSocket messages from the hub.@@ -345,7 +371,7 @@ func (client *WebSocketClient) handleAuthChallenge(msg *common.HubRequest[cbor.R}client.hubVerified = trueclient.connMu.Unlock()- client.agent.connectionManager.eventChan <- WebSocketConnect+ client.agent.connectionManager.sendEvent(WebSocketConnect)response := &common.FingerprintResponse{Fingerprint: client.fingerprint,diff --git a/agent/connection_manager.go b/agent/connection_manager.goindex 88f3a6511..fb768abc0 100644--- a/agent/connection_manager.go+++ b/agent/connection_manager.go@@ -21,7 +21,9 @@ import (// It handles both WebSocket and SSH connections, automatically switching between// them based on availability and managing reconnection attempts.type ConnectionManager struct {- agent *Agent // Reference to the parent agent+ agent *Agent // Reference to the parent agent+ ctx context.Context // Lifetime of the running manager+ reconnectWG sync.WaitGroup // Reconnect attempts must finish before shutdown// mu guards state shared by the event loop, connection attempts and SSH callbacks.mu sync.MutexState ConnectionState // Current connection state@@ -61,6 +63,7 @@ const wsTickerInterval = 10 * time.Secondfunc newConnectionManager(agent *Agent) *ConnectionManager {cm := &ConnectionManager{agent: agent,+ ctx: context.Background(),State: Disconnected,sshChanged: make(chan struct{}, 1),}@@ -160,7 +163,7 @@ func (c *ConnectionManager) isConnectingNow() bool {// Start begins connection attempts and enters the main event loop.// It handles connection events, periodic health updates, and graceful shutdown.-func (c *ConnectionManager) Start(serverOptions ServerOptions) error {+func (c *ConnectionManager) Start(ctx context.Context, serverOptions ServerOptions) error {if c.eventChan != nil {return errors.New("already started")}@@ -187,10 +190,12 @@ func (c *ConnectionManager) Start(serverOptions ServerOptions) error {c.eventChan = make(chan ConnectionEvent, 1)// signal handling for shutdown- sigCtx, stopSignals := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)+ sigCtx, stopSignals := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)defer stopSignals()+ c.ctx = sigCtxc.startWsTicker()+ defer c.stopWsTicker()c.connect()// update health status immediately and every 90 seconds@@ -214,6 +219,7 @@ func (c *ConnectionManager) Start(serverOptions ServerOptions) error {_ = health.Update()case <-sigCtx.Done():slog.Info("Shutting down", "cause", context.Cause(sigCtx))+ c.reconnectWG.Wait()return c.stop()}}@@ -227,25 +233,7 @@ func (c *ConnectionManager) handleSSHChange() {}}-// stop does not stop the connection manager itself, just any active connections. The manager will attempt to reconnect after stopping, so this should only be called immediately before shutting down the entire agent.-//-// If we need or want to expose a graceful Stop method in the future, do something like this to actually stop the manager:-//-// func (c *ConnectionManager) Start(serverOptions ServerOptions) error {-// ctx, cancel := context.WithCancel(context.Background())-// c.cancel = cancel-//-// for {-// select {-// case <-ctx.Done():-// return c.stop()-// }-// }-// }-//-// func (c *ConnectionManager) Stop() {-// c.cancel()-// }+// stop closes active connections after the manager and reconnect attempts finish.func (c *ConnectionManager) stop() error {_ = c.agent.StopServer()c.agent.monitorManager.Stop()@@ -326,7 +314,7 @@ func (c *ConnectionManager) handleStateChange(newState ConnectionState) {// make sure old ws connection is closedc.closeWebSocket()// reconnect- go c.connect()+ c.reconnectWG.Go(c.connect)}}@@ -337,7 +325,11 @@ func (c *ConnectionManager) connect() {defer c.setConnecting(false)if c.wsClient != nil && time.Since(c.wsClient.lastConnectAttempt) < 5*time.Second {- time.Sleep(5 * time.Second)+ select {+ case <-time.After(5 * time.Second):+ case <-c.ctx.Done():+ return+ }}// Try WebSocket first, if it fails, start SSH server@@ -356,6 +348,9 @@ func (c *ConnectionManager) connect() {// startWebSocketConnection attempts to establish a WebSocket connection to the hub.func (c *ConnectionManager) startWebSocketConnection() error {+ if err := c.ctx.Err(); err != nil {+ return err+ }if c.getState() != Disconnected {return errors.New("already connected")}@@ -376,6 +371,9 @@ func (c *ConnectionManager) startWebSocketConnection() error {// startSSHServer starts the SSH server if the agent is currently disconnected.func (c *ConnectionManager) startSSHServer() {+ if c.ctx.Err() != nil {+ return+ }c.mu.Lock()if c.State != Disconnected {c.mu.Unlock()@@ -400,6 +398,14 @@ func (c *ConnectionManager) startSSHServer() {}()}+// sendEvent releases callbacks when the manager has stopped receiving events.+func (c *ConnectionManager) sendEvent(event ConnectionEvent) {+ select {+ case c.eventChan <- event:+ case <-c.ctx.Done():+ }+}+// closeWebSocket closes the WebSocket connection if it exists.func (c *ConnectionManager) closeWebSocket() {if c.wsClient != nil {diff --git a/agent/connection_manager_test.go b/agent/connection_manager_test.goindex 20dd96f80..b7c421561 100644--- a/agent/connection_manager_test.go+++ b/agent/connection_manager_test.go@@ -245,19 +245,22 @@ func TestConnectionManager_WebSocketConnectionFlow(t *testing.T) {// TestConnectionManager_ReconnectionLogic tests reconnection prevention logicfunc TestConnectionManager_ReconnectionLogic(t *testing.T) {+ t.Setenv("BESZEL_AGENT_DISABLE_SSH", "true")agent := createTestAgent(t)cm := agent.connectionManagercm.eventChan = make(chan ConnectionEvent, 1)- // Test that isConnecting flag prevents duplicate reconnection attempts- // Start from connected state, then simulate disconnect- cm.State = WebSocketConnected- cm.setConnecting(false)+ synctest.Test(t, func(t *testing.T) {+ // An in-flight attempt must remain owned by its existing goroutine.+ cm.State = WebSocketConnected+ cm.setConnecting(true)+ cm.handleStateChange(Disconnected)+ defer cm.stopWsTicker()+ synctest.Wait()- // First disconnect should trigger reconnection logic- cm.handleStateChange(Disconnected)- assert.Equal(t, Disconnected, cm.State, "Should change to disconnected")- assert.True(t, cm.isConnectingNow(), "Should set isConnecting flag")+ assert.Equal(t, Disconnected, cm.getState(), "Should change to disconnected")+ assert.True(t, cm.isConnectingNow(), "Must not start another attempt that clears the in-flight flag")+ })}func TestWebSocketDisconnectStartsSSH(t *testing.T) {@@ -360,7 +363,7 @@ func TestConnectionManager_StartWithInvalidConfig(t *testing.T) {// Test starting when already startedcm.eventChan = make(chan ConnectionEvent, 5)- err := cm.Start(serverOptions)+ err := cm.Start(t.Context(), serverOptions)assert.Error(t, err, "Should error when starting already started connection manager")}@@ -371,7 +374,7 @@ func TestConnectionManager_StartRejectsInvalidCACertFile(t *testing.T) {t.Setenv("BESZEL_AGENT_TOKEN", "test-token")t.Setenv("BESZEL_AGENT_CA_CERT_FILE", t.TempDir())- err := cm.Start(ServerOptions{})+ err := cm.Start(t.Context(), ServerOptions{})require.Error(t, err)assert.Contains(t, err.Error(), "read CA_CERT_FILE")assert.Nil(t, cm.eventChan)diff --git a/agent/lifecycle_test.go b/agent/lifecycle_test.gonew file mode 100644index 000000000..a62fe8747--- /dev/null+++ b/agent/lifecycle_test.go@@ -0,0 +1,121 @@+//go:build testing++package agent++import (+ "context"+ "crypto/ed25519"+ "net"+ "net/http"+ "net/http/httptest"+ "testing"+ "time"++ "github.com/stretchr/testify/require"+ "golang.org/x/crypto/ssh"+)++func TestAgentCancellationReleasesListener(t *testing.T) {+ t.Setenv("BESZEL_AGENT_HUB_URL", "")+ opts := createTestServerOptions(t)+ for range 2 {+ a := createTestAgent(t)+ ctx, cancel := context.WithCancel(t.Context())+ done := make(chan error, 1)+ go func() { done <- a.Start(ctx, opts) }()+ require.Eventually(t, func() bool {+ conn, err := net.DialTimeout("tcp", opts.Addr, 20*time.Millisecond)+ if err != nil {+ return false+ }+ conn.Close()+ return true+ }, 2*time.Second, 10*time.Millisecond)+ cancel()+ select {+ case err := <-done:+ require.NoError(t, err)+ case <-time.After(2 * time.Second):+ t.Fatal("agent did not stop after cancellation")+ }+ listener, err := net.Listen("tcp", opts.Addr)+ require.NoError(t, err, "stopped agent must release its listening port")+ require.NoError(t, listener.Close())+ }+}++func TestAgentCancellationJoinsReconnect(t *testing.T) {+ hub := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ http.Error(w, "unavailable", http.StatusServiceUnavailable)+ }))+ defer hub.Close()+ t.Setenv("BESZEL_AGENT_HUB_URL", hub.URL)+ t.Setenv("BESZEL_AGENT_TOKEN", "test-token")+ a := createTestAgent(t)+ opts := createTestServerOptions(t)+ _, key, err := ed25519.GenerateKey(nil)+ require.NoError(t, err)+ signer, err := ssh.NewSignerFromKey(key)+ require.NoError(t, err)+ opts.Keys = []ssh.PublicKey{signer.PublicKey()}+ ctx, cancel := context.WithCancel(t.Context())+ defer cancel()+ done := make(chan error, 1)+ go func() { done <- a.Start(ctx, opts) }()+ var client *ssh.Client+ require.Eventually(t, func() bool {+ client, err = ssh.Dial("tcp", opts.Addr, &ssh.ClientConfig{+ User: "test", Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},+ HostKeyCallback: ssh.InsecureIgnoreHostKey(), Timeout: time.Second,+ })+ return err == nil+ }, 2*time.Second, 10*time.Millisecond)+ defer client.Close()+ session, err := client.NewSession()+ require.NoError(t, err)+ require.NoError(t, session.Shell())+ cm := a.connectionManager+ require.Eventually(t, func() bool { return cm.getState() == SSHConnected }, time.Second, time.Millisecond)+ require.NoError(t, client.Close())+ require.Eventually(t, cm.isConnectingNow, time.Second, time.Millisecond)+ cancel()+ select {+ case err := <-done:+ require.NoError(t, err)+ case <-time.After(2 * time.Second):+ t.Fatal("agent did not join its pending reconnect")+ }+ require.False(t, cm.isConnectingNow())+ listener, err := net.Listen("tcp", opts.Addr)+ require.NoError(t, err)+ require.NoError(t, listener.Close())+}++func TestAgentCancellationDuringHandshake(t *testing.T) {+ entered := make(chan struct{})+ hub := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ close(entered)+ <-r.Context().Done()+ }))+ defer hub.Close()+ t.Setenv("BESZEL_AGENT_HUB_URL", hub.URL)+ t.Setenv("BESZEL_AGENT_TOKEN", "test-token")+ a := createTestAgent(t)+ opts := createTestServerOptions(t)+ ctx, cancel := context.WithCancel(t.Context())+ defer cancel()+ done := make(chan error, 1)+ go func() { done <- a.Start(ctx, opts) }()+ select {+ case <-entered:+ case <-time.After(2 * time.Second):+ t.Fatal("agent did not reach the HTTP upgrade")+ }+ cancel()+ select {+ case err := <-done:+ require.NoError(t, err)+ case <-time.After(2 * time.Second):+ t.Error("agent did not cancel its pending handshake")+ }+}diff --git a/agent/server_test.go b/agent/server_test.goindex b003dd11e..eef45cc3e 100644--- a/agent/server_test.go+++ b/agent/server_test.go@@ -7,6 +7,7 @@ import ("context""crypto/ed25519""encoding/json"+ "errors""fmt""io""net"@@ -43,12 +44,10 @@ func TestStartServer(t *testing.T) {require.NoError(t, err)// Generate a different key pair for bad key test- badPubKey, badPrivKey, err := ed25519.GenerateKey(nil)+ _, badPrivKey, err := ed25519.GenerateKey(nil)require.NoError(t, err)badSigner, err := gossh.NewSignerFromKey(badPrivKey)require.NoError(t, err)- sshBadPubKey, err := gossh.NewPublicKey(badPubKey)- require.NoError(t, err)socketFile := filepath.Join(t.TempDir(), "beszel-test.sock")@@ -58,13 +57,13 @@ func TestStartServer(t *testing.T) {wantErr boolerrContains stringsetup func() error- cleanup func() error+ startServer bool}{{name: "tcp port only",config: ServerOptions{Network: "tcp",- Addr: ":45987",+ Addr: ":0",Keys: []gossh.PublicKey{sshPubKey},},},@@ -72,7 +71,7 @@ func TestStartServer(t *testing.T) {name: "tcp with ipv4",config: ServerOptions{Network: "tcp4",- Addr: "127.0.0.1:45988",+ Addr: "127.0.0.1:0",Keys: []gossh.PublicKey{sshPubKey},},},@@ -80,7 +79,7 @@ func TestStartServer(t *testing.T) {name: "tcp with ipv6",config: ServerOptions{Network: "tcp6",- Addr: "[::1]:45989",+ Addr: "[::1]:0",Keys: []gossh.PublicKey{sshPubKey},},},@@ -92,23 +91,20 @@ func TestStartServer(t *testing.T) {Keys: []gossh.PublicKey{sshPubKey},},setup: func() error {- // Create a socket file that should be removed+ // Create a regular file that should be replaced by the socket.f, err := os.Create(socketFile)if err != nil {return err}return f.Close()},- cleanup: func() error {- return os.Remove(socketFile)- },},{name: "bad key should fail",config: ServerOptions{Network: "tcp",- Addr: ":45987",- Keys: []gossh.PublicKey{sshBadPubKey},+ Addr: ":0",+ Keys: []gossh.PublicKey{sshPubKey},},wantErr: true,errContains: "ssh: handshake failed",@@ -117,9 +113,18 @@ func TestStartServer(t *testing.T) {name: "good key still good",config: ServerOptions{Network: "tcp",- Addr: ":45987",+ Addr: ":0",+ Keys: []gossh.PublicKey{sshPubKey},+ },+ },+ {+ name: "public StartServer",+ config: ServerOptions{+ Network: "tcp",+ Addr: "127.0.0.1:0",Keys: []gossh.PublicKey{sshPubKey},},+ startServer: true,},}@@ -130,24 +135,44 @@ func TestStartServer(t *testing.T) {require.NoError(t, err)}- if tt.cleanup != nil {- defer tt.cleanup()- }-- agent, err := NewAgent("")- require.NoError(t, err)-- // Start server in a goroutine since it blocks+ agent := createTestAgent(t)errChan := make(chan error, 1)- go func() {- errChan <- agent.StartServer(tt.config)- }()-- // Add a short delay to allow the server to start- time.Sleep(100 * time.Millisecond)+ var listener net.Listener+ if tt.startServer {+ go func() { errChan <- agent.StartServer(tt.config) }()+ } else {+ server, ln, err := agent.prepareSSHServer(tt.config)+ require.NoError(t, err)+ listener = ln+ go func() { errChan <- agent.serveSSHServer(server, listener) }()+ }+ t.Cleanup(func() {+ assert.NoError(t, agent.StopServer())+ select {+ case err := <-errChan:+ assert.True(t, errors.Is(err, net.ErrClosed) || errors.Is(err, ssh.ErrServerClosed), "unexpected server exit: %v", err)+ case <-time.After(5 * time.Second):+ t.Error("SSH server did not stop")+ }+ if tt.config.Network == "unix" {+ _, err := os.Lstat(socketFile)+ assert.ErrorIs(t, err, os.ErrNotExist)+ }+ })- // Try to connect to verify server is running- var client *gossh.Client+ if tt.startServer {+ require.Eventually(t, func() bool {+ agent.serverMu.Lock()+ defer agent.serverMu.Unlock()+ listener = agent.serverListener+ return listener != nil+ }, 5*time.Second, time.Millisecond, "SSH listener was not published")+ }+ if tt.config.Network == "unix" {+ info, err := os.Stat(socketFile)+ require.NoError(t, err)+ assert.NotZero(t, info.Mode()&os.ModeSocket)+ }// Choose the appropriate signer based on the test casetestSigner := signer@@ -164,27 +189,18 @@ func TestStartServer(t *testing.T) {Timeout: 4 * time.Second,}- switch tt.config.Network {- case "unix":- client, err = gossh.Dial("unix", tt.config.Addr, sshClientConfig)- default:- if !strings.Contains(tt.config.Addr, ":") {- tt.config.Addr = ":" + tt.config.Addr- }- client, err = gossh.Dial("tcp", tt.config.Addr, sshClientConfig)+ client, err := gossh.Dial(tt.config.Network, listener.Addr().String(), sshClientConfig)+ if client != nil {+ t.Cleanup(func() { _ = client.Close() })}if tt.wantErr {- assert.Error(t, err)- if tt.errContains != "" {- assert.Contains(t, err.Error(), tt.errContains)- }+ require.ErrorContains(t, err, tt.errContains)return}require.NoError(t, err)require.NotNil(t, client)- client.Close()})}}@@ -192,15 +208,14 @@ func TestStartServer(t *testing.T) {func TestStartServerDisableSSH(t *testing.T) {t.Setenv("BESZEL_AGENT_DISABLE_SSH", "true")- agent, err := NewAgent("")- require.NoError(t, err)+ agent := createTestAgent(t)opts := ServerOptions{Network: "tcp",Addr: ":45990",}- err = agent.StartServer(opts)+ err := agent.StartServer(opts)assert.Error(t, err)assert.Contains(t, err.Error(), "SSH disabled")}diff --git a/internal/cmd/agent/agent.go b/internal/cmd/agent/agent.goindex a3b02ef14..95a2a1563 100644--- a/internal/cmd/agent/agent.go+++ b/internal/cmd/agent/agent.go@@ -1,6 +1,7 @@package mainimport (+ "context""errors""fmt""log"@@ -216,7 +217,7 @@ func main() {log.Fatal("Failed to create agent: ", err)}- if err := a.Start(serverConfig); err != nil {+ if err := a.Start(context.Background(), serverConfig); err != nil {log.Fatal("Failed to start: ", err)}}diff --git a/internal/hub/agent_connect_test.go b/internal/hub/agent_connect_test.goindex d4ec4e8b0..dea5f1149 100644--- a/internal/hub/agent_connect_test.go+++ b/internal/hub/agent_connect_test.go@@ -3,6 +3,7 @@package hubimport (+ "context""crypto/ed25519""fmt""net/http"@@ -49,6 +50,25 @@ func cleanupTestHub(hub *Hub, testApp *pbtests.TestApp) {}}+// startTestAgent returns a stop function that must run before hub cleanup.+func startTestAgent(t *testing.T, testAgent *agent.Agent, opts agent.ServerOptions) (<-chan error, func()) {+ t.Helper()+ ctx, cancel := context.WithCancel(t.Context())+ done := make(chan error, 1)+ stopped := make(chan struct{})+ var result error+ go func() {+ defer close(stopped)+ result = testAgent.Start(ctx, opts)+ done <- result+ }()+ return done, func() {+ cancel()+ <-stopped+ assert.NoError(t, result)+ }+}+// Helper function to create a test recordfunc createTestRecord(app core.App, collection string, data map[string]any) (*core.Record, error) {col, err := app.FindCachedCollectionByNameOrId(collection)@@ -748,6 +768,8 @@ func TestHandleAgentConnect(t *testing.T) {// TestAgentWebSocketIntegration tests WebSocket connection scenarios with an actual agentfunc TestAgentWebSocketIntegration(t *testing.T) {+ // Connection fixtures do not exercise the host package manager.+ t.Setenv("BESZEL_AGENT_PACKAGE_UPDATES_INTERVAL", "0")// Create hub and test apphub, testApp, err := createTestHub(t)require.NoError(t, err)@@ -895,15 +917,12 @@ func TestAgentWebSocketIntegration(t *testing.T) {t.Setenv("BESZEL_AGENT_TOKEN", tc.agentToken)// Start agent in background- done := make(chan error, 1)- go func() {- serverOptions := agent.ServerOptions{- Network: "tcp",- Addr: fmt.Sprintf("127.0.0.1:%d", portNum),- Keys: []ssh.PublicKey{tc.agentSSHKey},- }- done <- testAgent.Start(serverOptions)- }()+ done, stopAgent := startTestAgent(t, testAgent, agent.ServerOptions{+ Network: "tcp",+ Addr: fmt.Sprintf("127.0.0.1:%d", portNum),+ Keys: []ssh.PublicKey{tc.agentSSHKey},+ })+ defer stopAgent()// Wait for connection resultmaxWait := 2 * time.Second@@ -920,15 +939,15 @@ func TestAgentWebSocketIntegration(t *testing.T) {case <-timeout:// Timeout reachedif tc.expectConnection {- t.Fatalf("Expected connection to succeed but timed out - agent state: %d", connectionManager.State)+ t.Fatalf("Expected connection to succeed but timed out - agent state: %d", connectionManager.GetState())} else {- t.Logf("Connection properly rejected (timeout) - agent state: %d", connectionManager.State)+ t.Logf("Connection properly rejected (timeout) - agent state: %d", connectionManager.GetState())}connectionResult = falsecase <-ticker:- if connectionManager.State == agent.WebSocketConnected {+ if connectionManager.GetState() == agent.WebSocketConnected {if tc.expectConnection {- t.Logf("WebSocket connection successful - agent state: %d", connectionManager.State)+ t.Logf("WebSocket connection successful - agent state: %d", connectionManager.GetState())connectionResult = true} else {t.Errorf("Unexpected: Connection succeeded when it should have been rejected")@@ -992,6 +1011,8 @@ func TestAgentWebSocketIntegration(t *testing.T) {// TestMultipleSystemsWithSameUniversalToken tests that multiple systems can share the same universal tokenfunc TestMultipleSystemsWithSameUniversalToken(t *testing.T) {+ // Connection fixtures do not exercise the host package manager.+ t.Setenv("BESZEL_AGENT_PACKAGE_UPDATES_INTERVAL", "0")// Create hub and test apphub, testApp, err := createTestHub(t)require.NoError(t, err)@@ -1086,15 +1107,12 @@ func TestMultipleSystemsWithSameUniversalToken(t *testing.T) {systemsBeforeCount := len(systemsBefore)// Start agent in background- done := make(chan error, 1)- go func() {- serverOptions := agent.ServerOptions{- Network: "tcp",- Addr: fmt.Sprintf("127.0.0.1:%d", portNum),- Keys: []ssh.PublicKey{goodPubKey},- }- done <- testAgent.Start(serverOptions)- }()+ done, stopAgent := startTestAgent(t, testAgent, agent.ServerOptions{+ Network: "tcp",+ Addr: fmt.Sprintf("127.0.0.1:%d", portNum),+ Keys: []ssh.PublicKey{goodPubKey},+ })+ defer stopAgent()// Wait for connection resultmaxWait := 2 * time.Second@@ -1110,15 +1128,15 @@ func TestMultipleSystemsWithSameUniversalToken(t *testing.T) {select {case <-timeout:if tc.expectConnection {- t.Fatalf("Expected connection to succeed but timed out - agent state: %d", connectionManager.State)+ t.Fatalf("Expected connection to succeed but timed out - agent state: %d", connectionManager.GetState())} else {- t.Logf("Connection properly rejected (timeout) - agent state: %d", connectionManager.State)+ t.Logf("Connection properly rejected (timeout) - agent state: %d", connectionManager.GetState())}connectionResult = falsecase <-ticker:- if connectionManager.State == agent.WebSocketConnected {+ if connectionManager.GetState() == agent.WebSocketConnected {if tc.expectConnection {- t.Logf("WebSocket connection successful - agent state: %d", connectionManager.State)+ t.Logf("WebSocket connection successful - agent state: %d", connectionManager.GetState())connectionResult = true} else {t.Errorf("Unexpected: Connection succeeded when it should have been rejected")@@ -1191,6 +1209,8 @@ func TestMultipleSystemsWithSameUniversalToken(t *testing.T) {// (universal_tokens collection) is accepted for agent self-registration even if it is not// present in the in-memory universalTokenMap.func TestPermanentUniversalTokenFromDB(t *testing.T) {+ // Connection fixtures do not exercise the host package manager.+ t.Setenv("BESZEL_AGENT_PACKAGE_UPDATES_INTERVAL", "0")// Create hub and test apphub, testApp, err := createTestHub(t)require.NoError(t, err)@@ -1241,15 +1261,12 @@ func TestPermanentUniversalTokenFromDB(t *testing.T) {t.Setenv("BESZEL_AGENT_TOKEN", universalToken)// Start agent in background- done := make(chan error, 1)- go func() {- serverOptions := agent.ServerOptions{- Network: "tcp",- Addr: "127.0.0.1:46050",- Keys: []ssh.PublicKey{goodPubKey},- }- done <- testAgent.Start(serverOptions)- }()+ done, stopAgent := startTestAgent(t, testAgent, agent.ServerOptions{+ Network: "tcp",+ Addr: "127.0.0.1:46050",+ Keys: []ssh.PublicKey{goodPubKey},+ })+ defer stopAgent()// Wait for connection resultmaxWait := 2 * time.Second@@ -1262,9 +1279,9 @@ func TestPermanentUniversalTokenFromDB(t *testing.T) {for {select {case <-timeout:- t.Fatalf("Expected connection to succeed but timed out - agent state: %d", connectionManager.State)+ t.Fatalf("Expected connection to succeed but timed out - agent state: %d", connectionManager.GetState())case <-ticker:- if connectionManager.State == agent.WebSocketConnected {+ if connectionManager.GetState() == agent.WebSocketConnected {// Successgoto verify}