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

fix: retry embedded postgres port allocation#20371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
zedkipp merged 3 commits intomainfromzedkipp/embedded-pg-port
Oct 21, 2025

Conversation

@zedkipp
Copy link
Contributor

Sometimes tests would fail because the port embedded postgres tries to use is already in use. This is because there's no way to tell postgres to use an ephemeral port. This change adds retries to starting embedded postgres when the port is not explicitly defined (e.g. tests) which should rid of, or at least significantly reduce, these flakes.

coder/internal#658

@zedkippzedkipp marked this pull request as ready for reviewOctober 17, 2025 23:03
// EmbeddedPostgres.Start in case of a race condition where the port we quickly
// listen on and close in embeddedPostgresURL() is not free by the time the embedded
// postgres starts up.
maxAttempts:=1
Copy link
Member

@johnstcnjohnstcnOct 20, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is the intention here to reduce the number of flakes in CI?

If so, I'd suggest usingos.Getenv("CI") == "true" for setting this conditionally.

ref:https://docs.github.com/en/actions/reference/workflows-and-actions/variables#:~:text=Description-,CI,-Always%20set%20to

zedkipp reacted with heart emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes. I have updated this comment to more clearly indicate the motivation.

I have also adopted the CI environment variable check, TIL.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I thought about this a little more and there's actually a better way:flag.Lookup("test.v") != nil.

Copy link
ContributorAuthor

@zedkippzedkippOct 21, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

While looking up how that works under the hood, I also discoveredtesting.Testing().https://pkg.go.dev/testing#Testing. Seems to be the stdlib approach for this problem. Some context I foundhttps://redirect.github.com/golang/go/issues/52600

I've adopted the stdlib approach

cli/server.go Outdated
}
returnconnectionURL,ep.Stop,nil

return"",nil,xerrors.Errorf("failed to start built-in PostgreSQL after %d attempts. Optionally, specify an external deployment with `--postgres-url`: %w",maxAttempts,startErr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

zedkipp reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Added!

cli/server.go Outdated
)

// Since a retry is needed, we wipe the port stored here at the beginning of the loop.
_=cfg.PostgresPort().Delete()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

If a coder admin relies on embedded postgres in their production deployment, and they previously started it and depend on the port being stable, this line will change the port assignment when embedded postgres fails to start for any reason. I'd prefer that we not change the port stability behavior in this PR.

I like Cian's suggestion to limit the retry behavior to CI environments - an easy fix would be to put this line insideif (os.Getenv("CI") == "true") { ... }.

zedkipp reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Good thinking! I've adopted Cian's suggestion for this.

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.
@zedkippzedkippforce-pushed thezedkipp/embedded-pg-port branch fromb4a281d to25e8620CompareOctober 20, 2025 19:30
Copy link
Member

@johnstcnjohnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM, one suggestion for improvement which will handle running tests outside of CI as well.

// EmbeddedPostgres.Start in case of a race condition where the port we quickly
// listen on and close in embeddedPostgresURL() is not free by the time the embedded
// postgres starts up.
maxAttempts:=1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I thought about this a little more and there's actually a better way:flag.Lookup("test.v") != nil.

@zedkippzedkipp merged commite73f9d3 intomainOct 21, 2025
141 of 156 checks passed
@zedkippzedkipp deleted the zedkipp/embedded-pg-port branchOctober 21, 2025 18:52
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsOct 21, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@johnstcnjohnstcnjohnstcn approved these changes

@hugodutkahugodutkahugodutka approved these changes

Assignees

@zedkippzedkipp

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@zedkipp@johnstcn@hugodutka

[8]ページ先頭

©2009-2025 Movatter.jp