Skip to content

Commit 9e8ff44

Browse files
committed
fix: ensure embedded-postgres state is wiped between retries
Retries were previously added when starting embedded postgres to mitigate port allocation conflicts (we can't use an ephemeral port). Retries alone seemingly did not fix the test flakes. A new failure mode appeared on the retries: timing out connecting to the database. When a port discovery error occurrs, embedded-postgres does not create the database. If the data directory exists on the next attempt, embedded-postgres will assume the database has already been created. This seems to cause the timeout error. Wipe all state between retries to ensure attempts execute the same logic that creates the database.
1 parent a272843 commit 9e8ff44

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

cli/server.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,14 @@ func startBuiltinPostgres(ctx context.Context, cfg config.Root, logger slog.Logg
21582158

21592159
var startErr error
21602160
for attempt := 0; attempt < maxAttempts; attempt++ {
2161+
if retryPortDiscovery && attempt > 0 {
2162+
// Clean up the data and runtime directories and the port file from the
2163+
// previous failed attempt to ensure a clean slate for the next attempt.
2164+
_ = os.RemoveAll(filepath.Join(cfg.PostgresPath(), "data"))
2165+
_ = os.RemoveAll(filepath.Join(cfg.PostgresPath(), "runtime"))
2166+
_ = cfg.PostgresPort().Delete()
2167+
}
2168+
21612169
// Ensure a password and port have been generated.
21622170
connectionURL, err := embeddedPostgresURL(cfg)
21632171
if err != nil {
@@ -2204,11 +2212,6 @@ func startBuiltinPostgres(ctx context.Context, cfg config.Root, logger slog.Logg
22042212
slog.F("port", pgPort),
22052213
slog.Error(startErr),
22062214
)
2207-
2208-
if retryPortDiscovery {
2209-
// Since a retry is needed, we wipe the port stored here at the beginning of the loop.
2210-
_ = cfg.PostgresPort().Delete()
2211-
}
22122215
}
22132216

22142217
return "", nil, xerrors.Errorf("failed to start built-in PostgreSQL after %d attempts. "+

0 commit comments

Comments
 (0)