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
37 changes: 12 additions & 25 deletions cmd/port-selector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,17 @@ func runWithName(name string) error {
// ALWAYS return the same port for (directory, name) - port is stable per directory
if existing := store.FindByDirectoryAndName(cwd, name); existing != nil {
debug.Printf("main", "found existing allocation for name %s: port %d (locked=%v)", name, existing.Port, existing.Locked)

// Warn if the port is busy (occupied by another process)
if !port.IsPortFree(existing.Port) {
procInfo := port.GetPortProcess(existing.Port)
if procInfo != nil && procInfo.Name != "" {
fmt.Fprintf(os.Stderr, "warning: port %d is busy (%s); use --forget to get a new port\n", existing.Port, procInfo.Name)
} else {
fmt.Fprintf(os.Stderr, "warning: port %d is busy; use --forget to get a new port\n", existing.Port)
}
}

// Update last_used timestamp for the specific port being issued
if !store.UpdateLastUsedByPort(existing.Port) {
debug.Printf("main", "warning: UpdateLastUsedByPort failed for port %d", existing.Port)
Expand Down Expand Up @@ -798,18 +809,7 @@ func runList() error {
maxDirLen := 0

for i, alloc := range allAllocs {
directory := alloc.Directory

// Check if port is busy and has Docker info
if !port.IsPortFree(alloc.Port) {
if procInfo := port.GetPortProcess(alloc.Port); procInfo != nil {
if procInfo.ContainerID != "" && procInfo.Cwd != "" && procInfo.Cwd != "/" {
directory = procInfo.Cwd
}
}
}

shortDir := pathutil.ShortenHomePath(directory)
shortDir := pathutil.ShortenHomePath(alloc.Directory)
allDirectories[i] = shortDir

if len(shortDir) > maxDirLen {
Expand All @@ -828,7 +828,6 @@ func runList() error {
username := "-"
pid := "-"
process := "-"
directory := alloc.Directory

// Determine SOURCE and use saved external info for external allocations
source := "free"
Expand Down Expand Up @@ -874,20 +873,12 @@ func runList() error {
} else if procInfo.ContainerID != "" {
// Docker container detected via fallback
process = "docker-proxy"
if procInfo.Cwd != "" && procInfo.Cwd != "/" {
directory = procInfo.Cwd
}
} else {
// Have user but no PID and no Docker - mark incomplete only if no saved name
if alloc.ProcessName == "" {
hasIncompleteInfo = true
}
}

// Use live Docker directory if available and better than saved
if procInfo.ContainerID != "" && procInfo.Cwd != "" && procInfo.Cwd != "/" {
directory = procInfo.Cwd
}
}
}

Expand All @@ -903,10 +894,6 @@ func runList() error {

// Get pre-calculated directory string and truncate if needed
shortDir := allDirectories[i]
// If directory was updated by Docker check, re-shorten it
if directory != alloc.Directory {
shortDir = pathutil.ShortenHomePath(directory)
}
// Cap at 40 characters maximum
if len(shortDir) > maxDirWidth {
shortDir = truncateDirectoryPath(shortDir, maxDirWidth)
Expand Down
12 changes: 9 additions & 3 deletions cmd/port-selector/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bytes"
"fmt"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -1227,8 +1226,6 @@ func TestPortSelector_ReturnsSamePortEvenWhenBusy(t *testing.T) {
t.Logf("Initial port: %s", initialPort)

// Step 2: Simulate user's service running on that port
portNum := 0
fmt.Sscanf(initialPort, "%d", &portNum)
ln, err := net.Listen("tcp", ":"+initialPort)
if err != nil {
t.Skipf("could not occupy port %s for test: %v", initialPort, err)
Expand All @@ -1254,6 +1251,15 @@ func TestPortSelector_ReturnsSamePortEvenWhenBusy(t *testing.T) {
t.Errorf("BUG REPRODUCED: expected same port %s, got different port %s", initialPort, secondPort)
t.Errorf("Port should be stable for the same directory, even when busy")
}

// Step 5: Verify warning is printed to stderr when port is busy
stderrStr := stderr2.String()
if !strings.Contains(stderrStr, "warning: port") || !strings.Contains(stderrStr, "is busy") {
t.Errorf("expected 'warning: port ... is busy' in stderr, got: %q", stderrStr)
}
if !strings.Contains(stderrStr, "--forget") {
t.Errorf("expected '--forget' hint in stderr warning, got: %q", stderrStr)
}
}

func TestPortSelector_PortStabilityAcrossMultipleCalls(t *testing.T) {
Expand Down
7 changes: 0 additions & 7 deletions internal/allocations/allocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2543,10 +2543,6 @@ func TestSetAllocation_PreservesLockedPorts(t *testing.T) {

// Tests for issue #77: FindByDirectoryAndNameWithPriority





// Tests for UnlockOtherLockedPorts

func TestUnlockOtherLockedPorts(t *testing.T) {
Expand Down Expand Up @@ -2630,9 +2626,6 @@ func TestUnlockOtherLockedPorts_EmptyStore(t *testing.T) {
}
}




func TestRefreshExternalAllocations_KeepsActive(t *testing.T) {
store := NewStore()
now := time.Now().UTC()
Expand Down
Loading