Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb4a281d

Browse files
committed
fix: retry embedded postgres port allocation
Sometimes tests would fail because the port embedded postgres tries to useis already in use. This is because there's no way to tell postgres to usean ephemeral port. This change adds retries to starting embedded postgreswhen the port is not explicitly defined (e.g. tests), which should rid, orat least significantly reduce, these flakes.
1 parentaec3e9e commitb4a281d

File tree

1 file changed

+67
-38
lines changed

1 file changed

+67
-38
lines changed

‎cli/server.go‎

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,50 +2134,79 @@ func startBuiltinPostgres(ctx context.Context, cfg config.Root, logger slog.Logg
21342134
return"",nil,xerrors.New("The built-in PostgreSQL cannot run as the root user. Create a non-root user and run again!")
21352135
}
21362136

2137-
// Ensure a password and port have been generated!
2138-
connectionURL,err:=embeddedPostgresURL(cfg)
2139-
iferr!=nil {
2140-
return"",nil,err
2141-
}
2142-
pgPassword,err:=cfg.PostgresPassword().Read()
2143-
iferr!=nil {
2144-
return"",nil,xerrors.Errorf("read postgres password: %w",err)
2145-
}
2146-
pgPortRaw,err:=cfg.PostgresPort().Read()
2147-
iferr!=nil {
2148-
return"",nil,xerrors.Errorf("read postgres port: %w",err)
2149-
}
2150-
pgPort,err:=strconv.ParseUint(pgPortRaw,10,16)
2151-
iferr!=nil {
2152-
return"",nil,xerrors.Errorf("parse postgres port: %w",err)
2153-
}
2154-
21552137
cachePath:=filepath.Join(cfg.PostgresPath(),"cache")
21562138
ifcustomCacheDir!="" {
21572139
cachePath=filepath.Join(customCacheDir,"postgres")
21582140
}
21592141
stdlibLogger:=slog.Stdlib(ctx,logger.Named("postgres"),slog.LevelDebug)
2160-
ep:=embeddedpostgres.NewDatabase(
2161-
embeddedpostgres.DefaultConfig().
2162-
Version(embeddedpostgres.V13).
2163-
BinariesPath(filepath.Join(cfg.PostgresPath(),"bin")).
2164-
// Default BinaryRepositoryURL repo1.maven.org is flaky.
2165-
BinaryRepositoryURL("https://repo.maven.apache.org/maven2").
2166-
DataPath(filepath.Join(cfg.PostgresPath(),"data")).
2167-
RuntimePath(filepath.Join(cfg.PostgresPath(),"runtime")).
2168-
CachePath(cachePath).
2169-
Username("coder").
2170-
Password(pgPassword).
2171-
Database("coder").
2172-
Encoding("UTF8").
2173-
Port(uint32(pgPort)).
2174-
Logger(stdlibLogger.Writer()),
2175-
)
2176-
err=ep.Start()
2177-
iferr!=nil {
2178-
return"",nil,xerrors.Errorf("Failed to start built-in PostgreSQL. Optionally, specify an external deployment with `--postgres-url`: %w",err)
2142+
2143+
// If the port is not defined, find an available port dynamically.
2144+
// There is no way to tell Postgres to use an ephemeral port, so we need to retry
2145+
// EmbeddedPostgres.Start in case of a race condition where the port we quickly
2146+
// listen on and close in embeddedPostgresURL() is not free by the time the embedded
2147+
// postgres starts up.
2148+
maxAttempts:=1
2149+
_,err=cfg.PostgresPort().Read()
2150+
ifxerrors.Is(err,os.ErrNotExist) {
2151+
// This maximum is somewhat arbitrarily chosen. It should cover most cases
2152+
// where port conflicts occur in CI.
2153+
maxAttempts=3
2154+
}
2155+
2156+
varstartErrerror
2157+
forattempt:=0;attempt<maxAttempts;attempt++ {
2158+
// Ensure a password and port have been generated.
2159+
connectionURL,err:=embeddedPostgresURL(cfg)
2160+
iferr!=nil {
2161+
return"",nil,err
2162+
}
2163+
pgPassword,err:=cfg.PostgresPassword().Read()
2164+
iferr!=nil {
2165+
return"",nil,xerrors.Errorf("read postgres password: %w",err)
2166+
}
2167+
pgPortRaw,err:=cfg.PostgresPort().Read()
2168+
iferr!=nil {
2169+
return"",nil,xerrors.Errorf("read postgres port: %w",err)
2170+
}
2171+
pgPort,err:=strconv.ParseUint(pgPortRaw,10,16)
2172+
iferr!=nil {
2173+
return"",nil,xerrors.Errorf("parse postgres port: %w",err)
2174+
}
2175+
2176+
ep:=embeddedpostgres.NewDatabase(
2177+
embeddedpostgres.DefaultConfig().
2178+
Version(embeddedpostgres.V13).
2179+
BinariesPath(filepath.Join(cfg.PostgresPath(),"bin")).
2180+
// Default BinaryRepositoryURL repo1.maven.org is flaky.
2181+
BinaryRepositoryURL("https://repo.maven.apache.org/maven2").
2182+
DataPath(filepath.Join(cfg.PostgresPath(),"data")).
2183+
RuntimePath(filepath.Join(cfg.PostgresPath(),"runtime")).
2184+
CachePath(cachePath).
2185+
Username("coder").
2186+
Password(pgPassword).
2187+
Database("coder").
2188+
Encoding("UTF8").
2189+
Port(uint32(pgPort)).
2190+
Logger(stdlibLogger.Writer()),
2191+
)
2192+
2193+
startErr=ep.Start()
2194+
ifstartErr==nil {
2195+
returnconnectionURL,ep.Stop,nil
2196+
}
2197+
2198+
logger.Warn(ctx,"failed to start embedded postgres",
2199+
slog.F("attempt",attempt+1),
2200+
slog.F("max_attempts",maxAttempts),
2201+
slog.F("port",pgPort),
2202+
slog.Error(startErr),
2203+
)
2204+
2205+
// Since a retry is needed, we wipe the port stored here at the beginning of the loop.
2206+
_=cfg.PostgresPort().Delete()
21792207
}
2180-
returnconnectionURL,ep.Stop,nil
2208+
2209+
return"",nil,xerrors.Errorf("failed to start built-in PostgreSQL after %d attempts. Optionally, specify an external deployment with `--postgres-url`: %w",maxAttempts,startErr)
21812210
}
21822211

21832212
funcConfigureHTTPClient(ctx context.Context,clientCertFile,clientKeyFilestring,tlsClientCAFilestring) (context.Context,*http.Client,error) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp