Skip to content

Commit 8c5e602

Browse files
Moritz Clasmeierclaude
andcommitted
Clean up detached port-forward on teardown
Read ROXIE_PORT_FORWARD_PID from the environment on deployer init and kill the process during teardownCentral, so the detached port-forward started in envrc mode doesn't leak. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 12affd7 commit 8c5e602

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

internal/deployer/deployer.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"fmt"
88
"os"
99
"os/exec"
10+
"strconv"
1011
"strings"
12+
"syscall"
1113
"sync"
1214
"time"
1315

@@ -556,6 +558,12 @@ func New(log *logger.Logger) (*Deployer, error) {
556558
d.roxCACertFile = caCert
557559
}
558560

561+
if pidStr := os.Getenv("ROXIE_PORT_FORWARD_PID"); pidStr != "" {
562+
if pid, err := strconv.Atoi(pidStr); err == nil {
563+
d.portForwardPID = pid
564+
}
565+
}
566+
559567
d.kubeContext = env.GetCurrentContext()
560568

561569
clusterResourceKinds, err := d.getClusterResourceKinds()
@@ -602,6 +610,22 @@ func (d *Deployer) Cleanup() {
602610
}
603611
}
604612

613+
func (d *Deployer) stopDetachedPortForward() {
614+
if d.portForwardPID == 0 {
615+
return
616+
}
617+
proc, err := os.FindProcess(d.portForwardPID)
618+
if err != nil {
619+
return
620+
}
621+
if err := proc.Signal(syscall.SIGKILL); err != nil {
622+
d.logger.Dimf("Detached port-forward (pid %d) already gone", d.portForwardPID)
623+
return
624+
}
625+
d.logger.Dimf("Stopped detached port-forward (pid %d)", d.portForwardPID)
626+
d.portForwardPID = 0
627+
}
628+
605629
// Deploy deploys the specified components to the cluster.
606630
func (d *Deployer) Deploy(ctx context.Context, components component.Component, resources, exposure string) error {
607631
adjustedResources, adjustedExposure, adjustedPortForward := d.clusterDefaults.ApplyConvenienceDefaults(
@@ -757,6 +781,7 @@ func (d *Deployer) teardownCentral(ctx context.Context) error {
757781
}
758782

759783
d.portForward.Stop()
784+
d.stopDetachedPortForward()
760785

761786
// Add pause-reconcile annotation to not have the operator interfere during resource deletion.
762787
if d.doesResourceExist(ctx, "central", "stackrox-central-services", d.centralNamespace) {

0 commit comments

Comments
 (0)