Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: use defer for mutex unlock in UpdatePortVisibility
Wrap GetTunnelPort and DeleteTunnelPort calls in anonymous functions
that use defer for the mutex unlock, making them robust against future
edits that add early returns between Lock and Unlock.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
  • Loading branch information
williammartin and Copilot committed May 13, 2026
commit d5c4951434da4eb9467937953eec9a1fabfb378c
17 changes: 10 additions & 7 deletions internal/codespaces/portforwarder/port_forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,11 @@ func (fwd *CodespacesPortForwarder) ListPorts(ctx context.Context) (ports []*tun

// UpdatePortVisibility changes the visibility (private, org, public) of the specified port.
func (fwd *CodespacesPortForwarder) UpdatePortVisibility(ctx context.Context, remotePort int, visibility string) error {
fwd.connection.ManagerMu.Lock()
tunnelPort, err := fwd.connection.TunnelManager.GetTunnelPort(ctx, fwd.connection.Tunnel, remotePort, fwd.connection.Options)
fwd.connection.ManagerMu.Unlock()
tunnelPort, err := func() (*tunnels.TunnelPort, error) {
fwd.connection.ManagerMu.Lock()
defer fwd.connection.ManagerMu.Unlock()
return fwd.connection.TunnelManager.GetTunnelPort(ctx, fwd.connection.Tunnel, remotePort, fwd.connection.Options)
}()
if err != nil {
Comment thread
williammartin marked this conversation as resolved.
return fmt.Errorf("error getting tunnel port: %w", err)
}
Expand All @@ -287,10 +289,11 @@ func (fwd *CodespacesPortForwarder) UpdatePortVisibility(ctx context.Context, re
if err != nil {
return fmt.Errorf("error converting port: %w", err)
}
fwd.connection.ManagerMu.Lock()
err = fwd.connection.TunnelManager.DeleteTunnelPort(ctx, fwd.connection.Tunnel, port, fwd.connection.Options)
fwd.connection.ManagerMu.Unlock()
if err != nil {
if err := func() error {
fwd.connection.ManagerMu.Lock()
defer fwd.connection.ManagerMu.Unlock()
return fwd.connection.TunnelManager.DeleteTunnelPort(ctx, fwd.connection.Tunnel, port, fwd.connection.Options)
}(); err != nil {
return fmt.Errorf("error deleting tunnel port: %w", err)
}

Expand Down
8 changes: 7 additions & 1 deletion internal/ghcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"os"
"os/exec"
"os/signal"
"path/filepath"
"slices"
"strconv"
Expand Down Expand Up @@ -134,7 +135,12 @@ func Main() exitCode {
}
}

ctx := context.Background()
// Intercept SIGINT/SIGTERM so that defers (e.g. telemetry flush) run
// before the process exits. Without this, the Go runtime's default
// signal handler calls os.Exit immediately, skipping all defers.
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

updateCtx, updateCancel := context.WithCancel(ctx)
defer updateCancel()
updateMessageChan := make(chan *update.ReleaseInfo)
Expand Down
Loading