pgapp icon indicating copy to clipboard operation
pgapp copied to clipboard

pgapp_worker does not check the source of handle_info({'EXIT',

Open aktungmak opened this issue 6 years ago • 3 comments

Problem

pgapp_worker expects to have one child epgsql_sock which is the connection to the database. The function handle_info({'EXIT', From, Reason}, State) deals with the case when this child dies for some reason. In this case we cast reconnect and a new child epgsql_sock is created.

When using pgapp:with_transaction, the code inside the provided function is run by the pgapp_worker process. If the code inside the transaction function receives the message {'EXIT', From, Reason} (for example a spawn_linked process finishes normally) pgapp_worker will interpret this as a crash of its child epgsql_sock and it will trigger a cast to reconnect, even though the original epqgsql_sock is alive and well. This process will cause the number of open epgsql_socks to increase forever until the database limit is reached, at which point some of the open connections will be forcefully closed.

Solution

The function handle_info({'EXIT', From, Reason}, State) should ensure that the From PID is the same as its child epgsql_sock. If it is not, then it should not attempt to reconnect.

aktungmak avatar Dec 13 '18 16:12 aktungmak

I have a fix for this issue in progress, I will submit a PR tomorrow if all is well.

aktungmak avatar Dec 13 '18 16:12 aktungmak

PR #27 seems to fix this, I will double check with a full test in the rest of the application code to make sure there is nothing I have missed.

aktungmak avatar Dec 14 '18 09:12 aktungmak

I have tested and all looks good to me! Feel free to merge #27 at your convenience.

aktungmak avatar Dec 14 '18 11:12 aktungmak