From 75bf97d155fef72d6d69133a12420af9ca190d1d Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Sat, 14 Nov 2026 12:31:27 +0000 Subject: [PATCH 1/8] test: wait up to a minute for the Autobahn server TestAutobahn gave the wstest container 5s to accept a handshake after docker run. Since the daily jobs moved to ubuntu-26.04 (Docker 29.4.2), the first container start on a fresh runner takes 2.8s to 12s, then wstest needs about 1s more to listen. On 24.04 (Docker 28.0.4) the first start took under 0.5s. The test failed in 13 of 27 daily test jobs since 2026-10-30. waitWS now allows a minute, sleeps 100ms between dials instead of dialing in a busy loop, and returns the last dial error. --- autobahn_test.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/autobahn_test.go b/autobahn_test.go index 20b89609..c656db15 100644 --- a/autobahn_test.go +++ b/autobahn_test.go @@ -98,20 +98,25 @@ func TestAutobahn(t *testing.T) { checkWSTestIndex(t, "./ci/out/autobahn-report/index.json") } +// waitWS dials url until the handshake succeeds. On a fresh ubuntu-26.04 +// GitHub runner, the first container start alone took up to 12s, so the +// limit is a minute. func waitWS(ctx context.Context, url string) error { - ctx, cancel := context.WithTimeout(ctx, time.Second*5) + ctx, cancel := context.WithTimeout(ctx, time.Minute) defer cancel() - for ctx.Err() == nil { + for { c, _, err := websocket.Dial(ctx, url, nil) - if err != nil { - continue + if err == nil { + c.Close(websocket.StatusNormalClosure, "") + return nil + } + select { + case <-ctx.Done(): + return fmt.Errorf("wstest server did not accept a WebSocket handshake: %w", err) + case <-time.After(100 * time.Millisecond): } - c.Close(websocket.StatusNormalClosure, "") - return nil } - - return ctx.Err() } func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func() error, err error) { From 21b820e56e4fe5c846aa6fae25cea5296e226330 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Sat, 14 Nov 2026 12:39:15 +0000 Subject: [PATCH 2/8] test: say the slow start follows docker pull --- autobahn_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/autobahn_test.go b/autobahn_test.go index c656db15..b4a20e9b 100644 --- a/autobahn_test.go +++ b/autobahn_test.go @@ -98,9 +98,9 @@ func TestAutobahn(t *testing.T) { checkWSTestIndex(t, "./ci/out/autobahn-report/index.json") } -// waitWS dials url until the handshake succeeds. On a fresh ubuntu-26.04 -// GitHub runner, the first container start alone took up to 12s, so the -// limit is a minute. +// waitWS dials url until the handshake succeeds. On ubuntu-26.04 GitHub +// runners, a container started right after docker pull took up to 12s to +// start, so the limit is a minute. func waitWS(ctx context.Context, url string) error { ctx, cancel := context.WithTimeout(ctx, time.Minute) defer cancel() From 57050ee96278704a1abadf454dbe35d058a4cc3e Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Sat, 14 Nov 2026 13:21:06 +0000 Subject: [PATCH 3/8] test: remove the Autobahn container and stop waiting when it exits Killing the docker run client leaves the container running, so each TestAutobahn run left a wstest container behind. closeFn now removes the container by name. wstestServer now returns once the server accepts handshakes, and stops waiting when docker run exits, so a server that fails to start fails the test right away. That lets the wait allow 5 minutes for a slow start. --- autobahn_test.go | 60 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/autobahn_test.go b/autobahn_test.go index b4a20e9b..f53279a1 100644 --- a/autobahn_test.go +++ b/autobahn_test.go @@ -68,9 +68,6 @@ func TestAutobahn(t *testing.T) { assert.Success(t, closeFn()) }() - err = waitWS(ctx, wstestURL) - assert.Success(t, err) - cases, err := wstestCaseCount(ctx, wstestURL) assert.Success(t, err) @@ -98,11 +95,12 @@ func TestAutobahn(t *testing.T) { checkWSTestIndex(t, "./ci/out/autobahn-report/index.json") } -// waitWS dials url until the handshake succeeds. On ubuntu-26.04 GitHub -// runners, a container started right after docker pull took up to 12s to -// start, so the limit is a minute. -func waitWS(ctx context.Context, url string) error { - ctx, cancel := context.WithTimeout(ctx, time.Minute) +// waitWS dials url until the handshake succeeds or exited is closed. On +// ubuntu-26.04 GitHub runners, a container started right after docker pull +// took up to 12s to start. A server that fails to start exits, so the 5 minute +// limit only ends the wait for a server that hangs. +func waitWS(ctx context.Context, url string, exited <-chan struct{}) error { + ctx, cancel := context.WithTimeout(ctx, 5*time.Minute) defer cancel() for { @@ -112,13 +110,17 @@ func waitWS(ctx context.Context, url string) error { return nil } select { + case <-exited: + return errors.New("wstest exited before accepting a WebSocket handshake") case <-ctx.Done(): - return fmt.Errorf("wstest server did not accept a WebSocket handshake: %w", err) + return fmt.Errorf("wstest did not accept a WebSocket handshake: %w", err) case <-time.After(100 * time.Millisecond): } } } +// wstestServer starts the Autobahn fuzzing server and returns once it accepts +// WebSocket handshakes. closeFn removes the container. func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func() error, err error) { defer errd.Wrap(&err, "failed to start autobahn wstest server") @@ -171,8 +173,11 @@ func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func( return "", nil, err } + // Killing the docker client does not stop the container, so closeFn + // removes it by name. + container := "websocket-autobahn-" + serverPort var args []string - args = append(args, "run", "-i", "--rm", + args = append(args, "run", "-i", "--rm", "--name", container, "-v", fmt.Sprintf("%s:%[1]s", specFile), "-v", fmt.Sprintf("%s/ci:/ci", wd), fmt.Sprintf("-p=%s:%s", serverAddr, serverPort), @@ -198,18 +203,33 @@ func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func( return "", nil, fmt.Errorf("failed to start wstest: %w", err) } - return url, func() error { - err = wstest.Process.Kill() - if err != nil { - return fmt.Errorf("failed to kill wstest: %w", err) + exited := make(chan struct{}) + var exitErr error + go func() { + exitErr = wstest.Wait() + close(exited) + }() + + closeFn = func() error { + out, rmErr := exec.Command("docker", "rm", "--force", container).CombinedOutput() + _ = wstest.Process.Kill() + <-exited + if rmErr != nil { + return fmt.Errorf("failed to remove wstest container: %w: %s", rmErr, out) } - err = wstest.Wait() - var ee *exec.ExitError - if errors.As(err, &ee) && ee.ExitCode() == -1 { - return nil + return nil + } + + err = waitWS(ctx, url, exited) + if err != nil { + select { + case <-exited: + err = fmt.Errorf("%w: %v", err, exitErr) + default: } - return err - }, nil + return "", nil, errors.Join(err, closeFn()) + } + return url, closeFn, nil } func wstestCaseCount(ctx context.Context, url string) (cases int, err error) { From 936f2062c819460e1b880f91c8b59c8f9f54d004 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Sat, 14 Nov 2026 14:43:07 +0000 Subject: [PATCH 4/8] test: wait 2 minutes for the Autobahn server, using wg.Go The image is pulled before docker run, so the wait covers only the container start and wstest's startup, which took at most 13.5s on the runners measured. 2 minutes is enough for that. docker run exiting now cancels the wait's context with the exit error as its cause, so waitWS needs no separate channel. --- autobahn_test.go | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/autobahn_test.go b/autobahn_test.go index f53279a1..b308d709 100644 --- a/autobahn_test.go +++ b/autobahn_test.go @@ -13,6 +13,7 @@ import ( "os/exec" "strconv" "strings" + "sync" "testing" "time" @@ -95,12 +96,11 @@ func TestAutobahn(t *testing.T) { checkWSTestIndex(t, "./ci/out/autobahn-report/index.json") } -// waitWS dials url until the handshake succeeds or exited is closed. On -// ubuntu-26.04 GitHub runners, a container started right after docker pull -// took up to 12s to start. A server that fails to start exits, so the 5 minute -// limit only ends the wait for a server that hangs. -func waitWS(ctx context.Context, url string, exited <-chan struct{}) error { - ctx, cancel := context.WithTimeout(ctx, 5*time.Minute) +// waitWS dials url until the handshake succeeds or ctx is done. The image is +// already pulled, so the wait covers the container start and wstest's own +// startup, which took up to 13.5s on ubuntu-26.04 GitHub runners. +func waitWS(ctx context.Context, url string) error { + ctx, cancel := context.WithTimeout(ctx, 2*time.Minute) defer cancel() for { @@ -110,10 +110,8 @@ func waitWS(ctx context.Context, url string, exited <-chan struct{}) error { return nil } select { - case <-exited: - return errors.New("wstest exited before accepting a WebSocket handshake") case <-ctx.Done(): - return fmt.Errorf("wstest did not accept a WebSocket handshake: %w", err) + return fmt.Errorf("wstest did not accept a WebSocket handshake: %w (last dial error: %w)", context.Cause(ctx), err) case <-time.After(100 * time.Millisecond): } } @@ -203,30 +201,30 @@ func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func( return "", nil, fmt.Errorf("failed to start wstest: %w", err) } - exited := make(chan struct{}) - var exitErr error - go func() { - exitErr = wstest.Wait() - close(exited) - }() + // A server that fails to start exits, and the wait ends then instead of at + // its time limit. + waitCtx, exited := context.WithCancelCause(ctx) + var wg sync.WaitGroup + wg.Go(func() { + err := wstest.Wait() + if err == nil { + err = errors.New("exit status 0") + } + exited(fmt.Errorf("wstest exited: %w", err)) + }) closeFn = func() error { - out, rmErr := exec.Command("docker", "rm", "--force", container).CombinedOutput() + out, err := exec.Command("docker", "rm", "--force", container).CombinedOutput() _ = wstest.Process.Kill() - <-exited - if rmErr != nil { - return fmt.Errorf("failed to remove wstest container: %w: %s", rmErr, out) + wg.Wait() + if err != nil { + return fmt.Errorf("failed to remove wstest container: %w: %s", err, out) } return nil } - err = waitWS(ctx, url, exited) + err = waitWS(waitCtx, url) if err != nil { - select { - case <-exited: - err = fmt.Errorf("%w: %v", err, exitErr) - default: - } return "", nil, errors.Join(err, closeFn()) } return url, closeFn, nil From 674243590569de8487324257010e6dc19e50dfeb Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 17 Nov 2026 13:09:55 +0000 Subject: [PATCH 5/8] test: remove the Autobahn container's volumes and name it randomly docker rm --force keeps the image's anonymous /config and /reports volumes, which --rm would remove, so every run left two volumes. closeFn now passes --volumes. The container name came from the port, and unusedListenAddr releases the port before docker binds it, so another run could get the same name, and a failing run's closeFn would remove the other run's container. The name is now random. The 2 minute limit moves from waitWS to wstestServer, next to the measurement that explains it, and its cause names the limit in the error instead of a second "context deadline exceeded". --- autobahn_test.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/autobahn_test.go b/autobahn_test.go index b308d709..4e47b443 100644 --- a/autobahn_test.go +++ b/autobahn_test.go @@ -21,6 +21,7 @@ import ( "github.com/coder/websocket/internal/errd" "github.com/coder/websocket/internal/test/assert" "github.com/coder/websocket/internal/test/wstest" + "github.com/coder/websocket/internal/test/xrand" "github.com/coder/websocket/internal/util" ) @@ -96,13 +97,8 @@ func TestAutobahn(t *testing.T) { checkWSTestIndex(t, "./ci/out/autobahn-report/index.json") } -// waitWS dials url until the handshake succeeds or ctx is done. The image is -// already pulled, so the wait covers the container start and wstest's own -// startup, which took up to 13.5s on ubuntu-26.04 GitHub runners. +// waitWS dials url every 100ms until the handshake succeeds or ctx is done. func waitWS(ctx context.Context, url string) error { - ctx, cancel := context.WithTimeout(ctx, 2*time.Minute) - defer cancel() - for { c, _, err := websocket.Dial(ctx, url, nil) if err == nil { @@ -172,8 +168,9 @@ func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func( } // Killing the docker client does not stop the container, so closeFn - // removes it by name. - container := "websocket-autobahn-" + serverPort + // removes it by this name. The name is random because another run can get + // the same port. + container := fmt.Sprintf("websocket-autobahn-%x", xrand.Bytes(8)) var args []string args = append(args, "run", "-i", "--rm", "--name", container, "-v", fmt.Sprintf("%s:%[1]s", specFile), @@ -201,9 +198,13 @@ func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func( return "", nil, fmt.Errorf("failed to start wstest: %w", err) } - // A server that fails to start exits, and the wait ends then instead of at - // its time limit. + // The image is already pulled, so the wait covers the container start and + // wstest's startup, which took up to 13.5s on ubuntu-26.04 GitHub runners. + // A server that fails to start exits, which ends the wait early. + const startTimeout = 2 * time.Minute waitCtx, exited := context.WithCancelCause(ctx) + waitCtx, cancelWait := context.WithTimeoutCause(waitCtx, startTimeout, fmt.Errorf("timed out after %v", startTimeout)) + defer cancelWait() var wg sync.WaitGroup wg.Go(func() { err := wstest.Wait() @@ -214,7 +215,9 @@ func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func( }) closeFn = func() error { - out, err := exec.Command("docker", "rm", "--force", container).CombinedOutput() + // The --volumes flag also removes the image's anonymous volumes, as --rm + // does. + out, err := exec.Command("docker", "rm", "--force", "--volumes", container).CombinedOutput() _ = wstest.Process.Kill() wg.Wait() if err != nil { From 830e385a36c2833764f63529fa530bc92a2956dd Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 17 Nov 2026 16:19:45 +0200 Subject: [PATCH 6/8] Apply suggestion from @mafredri --- autobahn_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/autobahn_test.go b/autobahn_test.go index 4e47b443..ad84c7d2 100644 --- a/autobahn_test.go +++ b/autobahn_test.go @@ -215,8 +215,6 @@ func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func( }) closeFn = func() error { - // The --volumes flag also removes the image's anonymous volumes, as --rm - // does. out, err := exec.Command("docker", "rm", "--force", "--volumes", container).CombinedOutput() _ = wstest.Process.Kill() wg.Wait() From c4d424e07d86d9bb9a24dff6536b596dfd13bf99 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 17 Nov 2026 16:21:04 +0200 Subject: [PATCH 7/8] Apply suggestion from @mafredri --- autobahn_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/autobahn_test.go b/autobahn_test.go index ad84c7d2..88c0b304 100644 --- a/autobahn_test.go +++ b/autobahn_test.go @@ -168,8 +168,7 @@ func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func( } // Killing the docker client does not stop the container, so closeFn - // removes it by this name. The name is random because another run can get - // the same port. + // removes it by this name. Uses a random name to avoid collisions. container := fmt.Sprintf("websocket-autobahn-%x", xrand.Bytes(8)) var args []string args = append(args, "run", "-i", "--rm", "--name", container, From 4140d35af9fa021628e5859030be768334c7fb29 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 17 Nov 2026 16:25:20 +0200 Subject: [PATCH 8/8] Apply suggestion from @mafredri --- autobahn_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/autobahn_test.go b/autobahn_test.go index 88c0b304..ceca3263 100644 --- a/autobahn_test.go +++ b/autobahn_test.go @@ -198,9 +198,9 @@ func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func( } // The image is already pulled, so the wait covers the container start and - // wstest's startup, which took up to 13.5s on ubuntu-26.04 GitHub runners. - // A server that fails to start exits, which ends the wait early. - const startTimeout = 2 * time.Minute + // wstest's startup. This can take up to 15s on Ubuntu 26.04 GitHub runners. + // The selected timeout leaves enough room to avoid flakes. + const startTimeout = 1 * time.Minute waitCtx, exited := context.WithCancelCause(ctx) waitCtx, cancelWait := context.WithTimeoutCause(waitCtx, startTimeout, fmt.Errorf("timed out after %v", startTimeout)) defer cancelWait()