From d7f61250313ac5e29ff6acf5661ddd1b698b0bf0 Mon Sep 17 00:00:00 2001 From: lstocchi Date: Thu, 28 Nov 2024 18:03:08 +0100 Subject: [PATCH] win-sshproxy.tid created before thread id is available 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 --- cmd/win-sshproxy/main.go | 27 ++++++++++++++++++++++++++- test-win-sshproxy/basic_test.go | 8 +++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/cmd/win-sshproxy/main.go b/cmd/win-sshproxy/main.go index 73d1fdd75..afdae6211 100644 --- a/cmd/win-sshproxy/main.go +++ b/cmd/win-sshproxy/main.go @@ -11,6 +11,7 @@ import ( "path/filepath" "strings" "syscall" + "time" "unsafe" "github.com/containers/gvisor-tap-vsock/pkg/sshclient" @@ -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 diff --git a/test-win-sshproxy/basic_test.go b/test-win-sshproxy/basic_test.go index e9541c58d..894bd3753 100644 --- a/test-win-sshproxy/basic_test.go +++ b/test-win-sshproxy/basic_test.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package e2e @@ -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())