Skip to content

Handle race condition on pgmq extension init#1695

Merged
GermanCoding merged 4 commits into
masterfrom
fix-pgmq-init
May 29, 2026
Merged

Handle race condition on pgmq extension init#1695
GermanCoding merged 4 commits into
masterfrom
fix-pgmq-init

Conversation

@GermanCoding

Copy link
Copy Markdown
Contributor

This call can fail when multiple producers start in parallel. Try to recover from this gracefully if possible.

@Alex-Kum Alex-Kum force-pushed the fix-pgmq-init branch 2 times, most recently from a74b83b to f46c81e Compare May 28, 2026 09:14
@Alex-Kum Alex-Kum marked this pull request as ready for review May 28, 2026 09:17
GermanCoding and others added 2 commits May 28, 2026 11:19
This call can fail when multiple producers start in parallel. Try to
recover from this gracefully if possible.
@rngcntr

rngcntr commented May 28, 2026

Copy link
Copy Markdown
Collaborator

So is it OK if two producers start (or run?) in parallel?

If yes: Why does PG throw an error in the first place if the case is expected? Why are we logging a warning if everything is fine?
If no: Why are we only logging a warning? :D

@GermanCoding

Copy link
Copy Markdown
Contributor Author

Yes, two producers should be able to run in parallel. InitAsync() is supposed to load the postgres pgmq extension, if it wasn't loaded already. Unfortunately, the code is not thread-safe: If multiple callers attempt to initialize the extension at the same time, postgres throws exceptions as this loading is apparently not atomic. These exceptions are "fine", in the sense that the extension is probably loaded normally and we can continue. We log a warning because we cannot be 100% sure: The postgres extension error could be caused by something else that actually prevents the extension from working (but we never observed this).

IMHO the better fix would be to upstream this into the npgmq library eventually, but since we do not have contact to the author this is slightly easier for us.

@rngcntr

rngcntr commented May 28, 2026

Copy link
Copy Markdown
Collaborator

That sounds to me as if the exception indicates that loading the pgmq extension is currently in progress. Would a retry logic be a possible solution? If we hit this case, just try again a bit later and the extension should be loaded so we can proceed without an error and be sure that everything is as expected.

As it currently is, the exception handling would allow Motor.NET to proceed even if the pgmq extension is still loading but not yet ready.

@GermanCoding

Copy link
Copy Markdown
Contributor Author

Yes, retrying would be an alternative

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: .NET has Polly 😄

@GermanCoding GermanCoding added this pull request to the merge queue May 29, 2026
Merged via the queue into master with commit 512dde1 May 29, 2026
8 checks passed
@GermanCoding GermanCoding deleted the fix-pgmq-init branch May 30, 2026 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants