Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/instances/standby.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ func (m *manager) shutdownHypervisor(ctx context.Context, inst *Instance) error
shutdownErr = hv.Shutdown(ctx)
}

// Teardown is committed; prevent new control-socket clients while the
// hypervisor exits. The deferred remove remains as a fallback for early
// returns above.
_ = os.Remove(inst.SocketPath)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admission undercount during shutdown wait

Medium Severity

Removing inst.SocketPath before WaitForProcessExit / forceKillHypervisorPID can leave on-disk metadata with a live HypervisorPID while the socket file is gone. The admission reconciler treats a missing socket as inactive, so capacity can be undercounted until standby saves metadata and clears the PID, even though the VMM process still holds CPU and memory.

Fix in Cursor Fix in Web

Triggered by learned rule: Admission capacity accounting must not silently undercount on errors

Reviewed by Cursor Bugbot for commit d9b16fe. Configure here.

// Wait for process to exit
if inst.HypervisorPID != nil {
pid := *inst.HypervisorPID
Expand Down
33 changes: 33 additions & 0 deletions lib/instances/standby_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
package instances

import (
"net"
"os"
"path/filepath"
"testing"

"github.com/kernel/hypeman/lib/hypervisor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestShutdownHypervisorRemovesControlSocket(t *testing.T) {
tmpDir, err := os.MkdirTemp("/tmp", "hypeman-standby-socket-")
require.NoError(t, err)
t.Cleanup(func() {
_ = os.RemoveAll(tmpDir)
})
socketPath := filepath.Join(tmpDir, "noop.sock")
listener, err := net.Listen("unix", socketPath)
require.NoError(t, err)
require.NoError(t, listener.Close())

lifecycleNoopHypervisorStates.Store(socketPath, hypervisor.StateRunning)
t.Cleanup(func() {
lifecycleNoopHypervisorStates.Delete(socketPath)
})

m := &manager{}
inst := &Instance{
StoredMetadata: StoredMetadata{
Id: "standby-socket-cleanup",
SocketPath: socketPath,
HypervisorType: lifecycleNoopHypervisorType,
},
}

require.NoError(t, m.shutdownHypervisor(t.Context(), inst))

_, err = os.Stat(socketPath)
require.True(t, os.IsNotExist(err), "shutdown should remove the hypervisor control socket")
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regression test misses early removal

Low Severity

TestShutdownHypervisorRemovesControlSocket closes the Unix listener before calling shutdownHypervisor, so the control socket file is often already absent and the case omits HypervisorPID, skipping the post-shutdown wait path. The assertion would still pass from the existing deferred os.Remove alone, so it does not guard the new pre-wait removal in standby.go.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit d9b16fe. Configure here.


func TestDiscardPromotedRetainedSnapshotTargetAfterSnapshotError(t *testing.T) {
t.Parallel()

Expand Down
Loading