Skip to content

Commit d76dbab

Browse files
fix: port-forward error logs failed to port forward (#9728) (#9759)
* fix: port-forward error logs `failed to port forward` Reflect changes in `os.Process` introduced in Go 1.23 * use `reflect` to make it future proof(?) * Add safeguards to call FieldByName * Add basic test to getHandleFromProcess * Fix copyright year Co-authored-by: SeongChan Lee <[email protected]>
1 parent 62fba6a commit d76dbab

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

pkg/skaffold/kubectl/exec_windows.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package kubectl
1919
import (
2020
"context"
2121
"fmt"
22+
"os"
2223
"os/exec"
24+
"reflect"
2325
"unsafe"
2426

2527
"golang.org/x/sys/windows"
@@ -62,11 +64,10 @@ func (c *Cmd) Start() error {
6264
return fmt.Errorf("could not start the command: %w", err)
6365
}
6466

65-
// Use `unsafe` to extract the process handle.
66-
processHandle := (*struct {
67-
Pid int
68-
handle windows.Handle
69-
})(unsafe.Pointer(c.Process)).handle
67+
processHandle, err := getHandleFromProcess(c.Process)
68+
if err != nil {
69+
return fmt.Errorf("could not get handle from process: %w", err)
70+
}
7071

7172
if err := windows.AssignProcessToJobObject(handle, processHandle); err != nil {
7273
return fmt.Errorf("could not assign job object: %w", err)
@@ -81,6 +82,24 @@ func (c *Cmd) Start() error {
8182
return nil
8283
}
8384

85+
func getHandleFromProcess(p *os.Process) (windows.Handle, error) {
86+
// `os.Process` don't expose `handle uintptr` field.
87+
v := reflect.ValueOf(p)
88+
i := reflect.Indirect(v)
89+
90+
k := i.Kind()
91+
if k != reflect.Struct {
92+
return windows.InvalidHandle, fmt.Errorf("unexpected kind of os.Process. probably a bug: %s", k)
93+
}
94+
95+
f := i.FieldByName("handle")
96+
if f.IsZero() {
97+
return windows.InvalidHandle, fmt.Errorf("could not get 'handle' field from os.Process. probably a bug")
98+
}
99+
100+
return windows.Handle(f.Uint()), nil
101+
}
102+
84103
// Run starts the specified command in a job object and waits for it to complete
85104
func (c *Cmd) Run() error {
86105
if err := c.Start(); err != nil {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2025 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kubectl
18+
19+
import (
20+
"context"
21+
"runtime"
22+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
"golang.org/x/sys/windows"
26+
)
27+
28+
func TestGetHandleFromProcess(t *testing.T) {
29+
if runtime.GOOS != "windows" {
30+
t.Skip("getHandleFromProcess only works on Windows")
31+
}
32+
33+
ctx := context.TODO()
34+
c := CommandContext(ctx, "kubectl", "--help")
35+
err := c.Cmd.Start()
36+
assert.Nil(t, err, "could not start command")
37+
38+
h, err := getHandleFromProcess(c.Process)
39+
assert.NotEqual(t, h, windows.InvalidHandle, "handle is invalid")
40+
assert.Nil(t, err, "could not get handle from process")
41+
42+
err = c.Cmd.Wait()
43+
assert.Nil(t, err, "could not wait command")
44+
}

0 commit comments

Comments
 (0)