From 0ca33d1cf9428d5c23822be8a019a16ce226a3d5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 4 Jan 2026 22:26:25 +0000 Subject: [PATCH] fix(native): make MySQL connection check immediate on first attempt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The waitForMySQL function was timing out before making any connection attempt when using short timeouts (500ms). The ticker waits 500ms before the first tick, at which point a 500ms timeout would already be expired. This fix adds an immediate connection attempt before entering the ticker loop, ensuring that at least one connection attempt is made even with short timeouts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- internal/sqltest/native/mysql.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/internal/sqltest/native/mysql.go b/internal/sqltest/native/mysql.go index 82881fdfb7..69482bace6 100644 --- a/internal/sqltest/native/mysql.go +++ b/internal/sqltest/native/mysql.go @@ -166,6 +166,11 @@ func waitForMySQL(ctx context.Context, uri string, timeout time.Duration) error ticker := time.NewTicker(500 * time.Millisecond) defer ticker.Stop() + // Make an immediate first attempt before waiting for the ticker + if err := tryMySQLConnection(ctx, uri); err == nil { + return nil + } + var lastErr error for { select { @@ -175,23 +180,24 @@ func waitForMySQL(ctx context.Context, uri string, timeout time.Duration) error if time.Now().After(deadline) { return fmt.Errorf("timeout waiting for MySQL (last error: %v)", lastErr) } - db, err := sql.Open("mysql", uri) - if err != nil { - lastErr = err - slog.Debug("native/mysql", "open-attempt", err) - continue - } - // Use a short timeout for ping to avoid hanging - pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second) - err = db.PingContext(pingCtx) - cancel() - if err != nil { + if err := tryMySQLConnection(ctx, uri); err != nil { lastErr = err - db.Close() continue } - db.Close() return nil } } } + +func tryMySQLConnection(ctx context.Context, uri string) error { + db, err := sql.Open("mysql", uri) + if err != nil { + slog.Debug("native/mysql", "open-attempt", err) + return err + } + defer db.Close() + // Use a short timeout for ping to avoid hanging + pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + return db.PingContext(pingCtx) +}