From c35018430fb833d638527a0c728c0aeb9cc9e627 Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Mon, 30 Nov 2026 14:40:36 +0000 Subject: [PATCH 1/2] Fix flaky TestOpenDoesNotWaitForLauncherChild TempDir cleanup The test opens twice, and each open starts a sleeper that writes the same file when it ends. The cleanup waited only until that file existed, so it went ahead after the first sleeper, and the second sleeper could write the file again while TempDir was being removed: "unlinkat ...: directory not empty". Give each open its own TempDir and file and wait for both. The sleeper now writes a temporary file and renames it into place, so the file appears only after it is closed and the cleanup cannot race an open handle on Windows. Co-authored-by: Isaac --- libs/browser/browser_test.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/libs/browser/browser_test.go b/libs/browser/browser_test.go index 7a709e8e71a..d01b444eaaa 100644 --- a/libs/browser/browser_test.go +++ b/libs/browser/browser_test.go @@ -105,7 +105,12 @@ func TestOpenerSleeper(t *testing.T) { t.Skip("runs only as the child of the browser command") } time.Sleep(openerSleepTime) - _ = os.WriteFile(file, nil, 0o600) + // Write a temporary file and rename it, so that the file appears only once it is closed, and the cleanup that waits + // for it can remove the directory (Windows does not remove a directory with an open file in it). + tmp := file + ".tmp" + if os.WriteFile(tmp, nil, 0o600) == nil { + _ = os.Rename(tmp, file) + } } // runOpener calls open with this test binary as the browser command, and returns what the command wrote to stdout and @@ -183,12 +188,10 @@ func TestOpenCommandOutput(t *testing.T) { // TestOpenDoesNotWaitForLauncherChild checks that the default opener returns when its launcher returns, although the // browser the launcher started is still running. The launcher is a stub that runs this test binary the way pkg/browser's // runCmd runs xdg-open: with the package's Stdout and Stderr, and waiting only for the command. The sleeper is the -// browser, and it writes sleepFile when it ends. Neither open may wait for it. +// browser, and it writes its sleepFile when it ends. Neither open may wait for it. Each open gets its own sleepFile, so +// that the cleanup waits for both sleepers: with a shared file, the second sleeper could write it while TempDir is removed. func TestOpenDoesNotWaitForLauncherChild(t *testing.T) { t.Setenv(openerChildEnv, "1") - sleepFile := filepath.Join(t.TempDir(), "sleeper-done") - t.Setenv(openerBackgroundEnv, sleepFile) - waitForSleeper(t, sleepFile) replaceDefaultLauncher(t, func(url string) error { cmd := exec.Command(os.Args[0], url) cmd.Stdout = browserpkg.Stdout @@ -198,6 +201,9 @@ func TestOpenDoesNotWaitForLauncherChild(t *testing.T) { ctx := env.Set(t.Context(), "BROWSER", "") for range 2 { + sleepFile := filepath.Join(t.TempDir(), "sleeper-done") + t.Setenv(openerBackgroundEnv, sleepFile) + waitForSleeper(t, sleepFile) require.NoError(t, Open(ctx, "https://example.com/login")) _, err := os.Stat(sleepFile) assert.ErrorIs(t, err, fs.ErrNotExist, "the open waited for the browser that the launcher started") From 6512a9615b7feef6cb2bd0151d2a6aaed2971e27 Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Mon, 30 Nov 2026 16:21:29 +0000 Subject: [PATCH 2/2] Use libs/atomicfile for the sleeper's marker file Co-authored-by: Isaac --- libs/browser/browser_test.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/libs/browser/browser_test.go b/libs/browser/browser_test.go index d01b444eaaa..a450eb71454 100644 --- a/libs/browser/browser_test.go +++ b/libs/browser/browser_test.go @@ -14,6 +14,7 @@ import ( browserpkg "github.com/pkg/browser" + "github.com/databricks/cli/libs/atomicfile" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/env" "github.com/stretchr/testify/assert" @@ -105,12 +106,9 @@ func TestOpenerSleeper(t *testing.T) { t.Skip("runs only as the child of the browser command") } time.Sleep(openerSleepTime) - // Write a temporary file and rename it, so that the file appears only once it is closed, and the cleanup that waits - // for it can remove the directory (Windows does not remove a directory with an open file in it). - tmp := file + ".tmp" - if os.WriteFile(tmp, nil, 0o600) == nil { - _ = os.Rename(tmp, file) - } + // Write atomically, so that the file appears only once it is closed, and the cleanup that waits for it can remove the + // directory (Windows does not remove a directory with an open file in it). + _ = atomicfile.Write(file, nil, 0o600) } // runOpener calls open with this test binary as the browser command, and returns what the command wrote to stdout and