"Error: connection already closed."
Anonymous
TRIALOP

2 years ago

I keep getting this error message when I attempt to update a user in my PostgreSQL database. I do not have such errors when users are interacting with the web application which also utilizes the database throughout use of the application. I tried wrapping the update in try exception block with 10 retries and I even close unused connections every hour

26 Replies

Anonymous
TRIALOP

2 years ago

f740c960-4ea5-4e0d-a8fc-7b3efef00d50


Anonymous
TRIALOP

2 years ago

I used to get an error SSL SYSCALL error: EOF detectedError but now it just states connection already closed


2 years ago

going to need more info, what kind of app is this?


Anonymous
TRIALOP

2 years ago

I have a Django backend application that is deployed on your platform that interacts with a postgreSQL database on your platform as well. I have Django handling the backend and React handling the frontend. It is a SPA application that offers user authorization, interaction with forms where the user enters a numerical set and that set is used to create a matrix and then deconstructed into a set of numerical sets and then displayed to the user on the frontend. Each time a user enters a set the backend/frontend updates (decrements a daily entry count). There are several pages that are injected via React router. I have not had any issues with this part of the application. However, when a standard user navigates to the payment page and makes a purchase using the Stripe API, the transactions go through and the webhook executes after a successful payment ( I have no issues there either). The webhook uses an apScheduler to setup a cron job 30 seconds after a successful payment, and it is during this time that causes the error. So the cron job executes 30 seconds after the webhook finishes execution. In this function the user is accessed via stripecheckoutid (foreign-key) and then updated in the user_table (changes from standard user to whatever membership was purchased) and updates entry fields as well. It is only during this process that I see the error. I am sorry if I am being to Verbose.


Anonymous
TRIALOP

2 years ago

What does not make sense is that I am able to update the users during their sessions in the database while interacting with the frontend (decrementing available entries). But as soon as a payment is made and I go to update the user so they have these features I get an error that connection is closed.


2 years ago

is your app using different database clients for the different parts of the app?


Anonymous
TRIALOP

2 years ago

Do you mean is the database that I have been using that is on your platform providing everything I just mentioned?


2 years ago

for the record, i dont work for railway, so it would not be my platform


Anonymous
TRIALOP

2 years ago

Sorry lol I just assumed


Anonymous
TRIALOP

2 years ago

My mistake


2 years ago

not what i mean, does your app use different database clients for the different parts of the app?


Anonymous
TRIALOP

2 years ago

No


Anonymous
TRIALOP

2 years ago

I see people complain about psycop2. Could that be the issue?


2 years ago

the issue is that your database client is trying to use closed connections


Anonymous
TRIALOP

2 years ago

I read on a thread that closing unused connections every hour worked for them. Unfortunately it is not working for me


Anonymous
TRIALOP

2 years ago

Do you have any suggestions?


2 years ago

make sure the client isnt trying to use a closed connection


Anonymous
TRIALOP

2 years ago

By the way I am new to full stack development and I appreciate your patience. I just graduated last may and built the entire application from scratch so…


Anonymous
TRIALOP

2 years ago

Have you worked with Django


2 years ago

i have not


Anonymous
TRIALOP

2 years ago

So here is the info from the Django documentation. The default value is 0 , preserving the historical behavior of closing the database connection at the end of each request. To enable persistent connections, set CONNMAXAGE to a positive integer of seconds. For unlimited persistent connections, set it to None .


Anonymous
TRIALOP

2 years ago

So maybe I should change the settings… What you do think? Or what would you suggest


2 years ago

it is clear from the information that you provided that database connections are not being fully closed, the client tries to use a closed connection and that is where the error is coming from


Anonymous
TRIALOP

2 years ago

Thanks. By the way, I appreciate you taking the time to interact with me even though you do not work for Railway


2 years ago

happy to help where I can.

while I unfortunately can't give you a direct solution, I can at minimum tell you where the problem originates from.


Anonymous
TRIALOP

2 years ago

👍


Loading...