-
Notifications
You must be signed in to change notification settings - Fork 64
Race condition in pool.on('connect',...) ? #97
Comments
Event listeners are called synchronously by |
@charmander I glanced through the source, and given that the db call is async, I didn't see any guarantee that a call like I have in my example would complete prior to a client issuing a query. Did I miss something? |
@claytongulick A client can only run one query at a time, so pg keeps a queue of queries for each client. If the query is first to enter the queue, it will be the first to run. |
Thanks for clarifying that @charmander -- I came here with the same concern as @claytongulick and found this issue, but what you're saying makes sense. |
it's true that you can guarantee the query to be executed first, but, only if the connect callback is synchronous, if your callback uses await/promises before issuing the query, the order won't be guaranteed I'm currently looking for a workaround for this also, I think sometimes (eg, for |
well there is the undocumented so I resorted to monkey patching the connect method for now |
@mathroc What is it that you need to do every time a connection is acquired? |
it's a websocket application, I call |
A (fairly) common question I see around is how to set options like search_path with node-pg. The answer I see most commonly given is to use something like
pool.on('connect', (client) => {client.query('set search_path...');});
Glancing through the code though, it doesn't look like doing this will actually guarantee that the search_path will be set before a client issues a query, given that it's an async call.
Are we running the risk that queries can be executed prior to setup? This could be a potential security issue as well if using RLS and something like 'SET my.user_id = 123' - a query could potentially be executed prior to the SET command completing?
The text was updated successfully, but these errors were encountered: