Handle race condition on pgmq extension init#1695
Conversation
a74b83b to
f46c81e
Compare
This call can fail when multiple producers start in parallel. Try to recover from this gracefully if possible.
f46c81e to
db73768
Compare
|
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? |
|
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. |
|
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. |
|
Yes, retrying would be an alternative |
This call can fail when multiple producers start in parallel. Try to recover from this gracefully if possible.