Fix setup SQL connect panic and add startup retries#3827
Fix setup SQL connect panic and add startup retries#3827ljluestc wants to merge 1 commit intosemaphoreui:developfrom
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bf9be5d5ab
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if lastErr != nil { | ||
| panic(fmt.Errorf("cannot connect to database after %d attempts: %w", connectMaxAttempts, lastErr)) |
There was a problem hiding this comment.
Check current error state before panicking after retries
The retry loop breaks on a successful connect()+Ping, but lastErr is never cleared, so any earlier transient failure causes this panic path to execute even after a later successful attempt. In a normal startup race (e.g., DB ready on attempt 2+), setup/runtime will still fail after the loop despite having a valid connection. This regresses the intended retry behavior; the post-loop failure check should be based on the final err (or reset lastErr on success).
Useful? React with 👍 / 👎.
What problem does this PR solve?
Issue Number: close #1653
When running Semaphore with MySQL in Docker Compose, startup can fail during early database initialization windows.
Two concrete failures were reported:
What changed and how does it work?
This PR hardens SQL setup/startup behavior:
Safe setup close ordering (
cli/cmd/setup.go)Nil-safe SQL close (
db/sql/SqlDb.go)SqlDbConnection.Close()now safely no-ops when SQL handles are not initialized.d.sql = nil).SQL connect retry/backoff for container startup race (
db/sql/SqlDb.go)Connect()now retries ping/connect with bounded attempts and backoff.Regression test (
db/sql/SqlDb_test.go)Why this approach?
Check List
Tests
go -C /home/calelin/dev/semaphore test ./db/sqlgo -C /home/calelin/dev/semaphore test ./cli/setupDocumentation
Release note
Improve MySQL startup reliability in Docker Compose by adding retry/backoff during SQL connect and fixing a setup panic caused by closing uninitialized SQL connections.