Skip to content

Commit d9b16fe

Browse files
committed
Clean up standby control socket earlier
1 parent fca9c3c commit d9b16fe

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

lib/instances/standby.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ func (m *manager) shutdownHypervisor(ctx context.Context, inst *Instance) error
369369
shutdownErr = hv.Shutdown(ctx)
370370
}
371371

372+
// Teardown is committed; prevent new control-socket clients while the
373+
// hypervisor exits. The deferred remove remains as a fallback for early
374+
// returns above.
375+
_ = os.Remove(inst.SocketPath)
376+
372377
// Wait for process to exit
373378
if inst.HypervisorPID != nil {
374379
pid := *inst.HypervisorPID

lib/instances/standby_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,47 @@
11
package instances
22

33
import (
4+
"net"
45
"os"
56
"path/filepath"
67
"testing"
78

9+
"github.com/kernel/hypeman/lib/hypervisor"
810
"github.com/stretchr/testify/assert"
911
"github.com/stretchr/testify/require"
1012
)
1113

14+
func TestShutdownHypervisorRemovesControlSocket(t *testing.T) {
15+
tmpDir, err := os.MkdirTemp("/tmp", "hypeman-standby-socket-")
16+
require.NoError(t, err)
17+
t.Cleanup(func() {
18+
_ = os.RemoveAll(tmpDir)
19+
})
20+
socketPath := filepath.Join(tmpDir, "noop.sock")
21+
listener, err := net.Listen("unix", socketPath)
22+
require.NoError(t, err)
23+
require.NoError(t, listener.Close())
24+
25+
lifecycleNoopHypervisorStates.Store(socketPath, hypervisor.StateRunning)
26+
t.Cleanup(func() {
27+
lifecycleNoopHypervisorStates.Delete(socketPath)
28+
})
29+
30+
m := &manager{}
31+
inst := &Instance{
32+
StoredMetadata: StoredMetadata{
33+
Id: "standby-socket-cleanup",
34+
SocketPath: socketPath,
35+
HypervisorType: lifecycleNoopHypervisorType,
36+
},
37+
}
38+
39+
require.NoError(t, m.shutdownHypervisor(t.Context(), inst))
40+
41+
_, err = os.Stat(socketPath)
42+
require.True(t, os.IsNotExist(err), "shutdown should remove the hypervisor control socket")
43+
}
44+
1245
func TestDiscardPromotedRetainedSnapshotTargetAfterSnapshotError(t *testing.T) {
1346
t.Parallel()
1447

0 commit comments

Comments
 (0)