Skip to content

Commit

Permalink
win-sshproxy.tid created before thread id is available
Browse files Browse the repository at this point in the history
this commit fixes a potential race condition that prevented the tests to succeed
when running in a github workflow.
Basically the thread id was not actually available before
writing it on the file, resulting in a thread id equals to 0 written in it.
So, when the tests were trying to retrieve the thread id to use it to send
the WM_QUIT signal, they failed.

This patch adds a check on the thread id before writing
it on the file. Now, if the thread id is 0, it keeps calling winquit to
retrieve it. If, after 10 secs, there is no success it returns an error.

Signed-off-by: lstocchi <[email protected]>
  • Loading branch information
lstocchi committed Nov 28, 2024
1 parent ec2ed7d commit d7f6125
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
27 changes: 26 additions & 1 deletion cmd/win-sshproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"strings"
"syscall"
"time"
"unsafe"

"github.com/containers/gvisor-tap-vsock/pkg/sshclient"
Expand Down Expand Up @@ -173,11 +174,35 @@ func saveThreadId() (uint32, error) {
return 0, err
}
defer file.Close()
tid := winquit.GetCurrentMessageLoopThreadId()

tid, err := getThreadId()
if err != nil {
return 0, err
}

fmt.Fprintf(file, "%d:%d\n", os.Getpid(), tid)
return tid, nil
}

func getThreadId() (uint32, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

var tid uint32
for {
select {
case <-ctx.Done():
return 0, fmt.Errorf("failed to get thread ID")
default:
tid = winquit.GetCurrentMessageLoopThreadId()
if tid != 0 {
return tid, nil
}
time.Sleep(100 * time.Millisecond)
}
}
}

// Creates an "error" style pop-up window
func alert(caption string) int {
// Error box style
Expand Down
8 changes: 5 additions & 3 deletions test-win-sshproxy/basic_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package e2e
Expand Down Expand Up @@ -25,15 +26,16 @@ var _ = Describe("connectivity", func() {
err := startProxy()
Expect(err).ShouldNot(HaveOccurred())

var pid uint32
var pid, tid uint32
for i := 0; i < 20; i++ {
pid, _, err = readTid()
if err == nil {
pid, tid, err = readTid()
if err == nil && tid != 0 {
break
}
time.Sleep(100 * time.Millisecond)
}

Expect(tid).ShouldNot(Equal(0))
Expect(err).ShouldNot(HaveOccurred())
proc, err := os.FindProcess(int(pid))
Expect(err).ShouldNot(HaveOccurred())
Expand Down

0 comments on commit d7f6125

Please sign in to comment.