Forums

Postgres Server Errors?

Once a week or so (sometimes more often) I will get an error:

Traceback (most recent call last): 1 File "/home/Venturafranklin/.virtualenvs/thetatauCMT/lib/python3.6/site-packages/django/db/backends/utils.py" line 84 in _execute return self.cursor.execute(sql, params) OperationalError: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.

Similar to: https://www.pythonanywhere.com/forums/topic/11724/ But that didn't seem to go anywhere.

Searching online seems to say something with the postgres configuration/connection, but the connection works most of the time. I don't seem to be able to access the logs, are you able to pull them? Anything glaringly obvious there?

It is not all the same URL, nor request type. Here are time stamps of error notification.

```

2020-09-01 11:09 AM

2020-08-31 12:08 PM

2020-08-28 12:44 PM

2020-08-24 12:56 PM

2020-08-24 12:15 PM

2020-08-24 9:45 AM

2020-08-06 12:22 PM

2020-08-05 8:46 AM

2020-08-05 8:46 AM

```

Thank you for your help!

Maybe try to close your django database connection before executing the query with db.connection.close() to refresh your connection.

This is across my whole site, so there is no db connection to close.

Is it possible to go to /var/log/postgresql on the postgres server and share with me the logs?

There's nothing obvious in the Postgres logs, no -- here's everything they have for the timestamps Aug 31 22:54 - Sep 1 20:08:

LOG:  could not receive data from client: Connection reset by peer
LOG:  could not receive data from client: Connection reset by peer
LOG:  could not receive data from client: Connection reset by peer
LOG:  could not receive data from client: Connection reset by peer
WARNING:  there is already a transaction in progress
ERROR:  SAVEPOINT can only be used in transaction blocks
STATEMENT:  SAVEPOINT "XXXXXXXXXXXX
ERROR:  ROLLBACK TO SAVEPOINT can only be used in transaction blocks
STATEMENT:  ROLLBACK TO SAVEPOINT "YYYYYYYYYYY"
WARNING:  there is no transaction in progress
WARNING:  there is already a transaction in progress
WARNING:  there is already a transaction in progress
LOG:  could not receive data from client: Connection reset by peer
LOG:  could not receive data from client: Connection reset by peer
LOG:  could not receive data from client: Connection reset by peer

(I've masked the IDs of the savepoints for security, probably unnecessarily, but it's best to be sure.)

The main Postgres process has been running since May 28, so it certainly doesn't look like it's crashing. Is there anything at all in common between the requests that trigger this -- for example, a particular model that you're doing lookups on?

It is several different URLs but they are all related to the User model. Different queries, but the same model.

Are any of them making changes to the user model?

All but one are just queries. The one was to a different model and happened yesterday.

This error seems to mostly be a result of accidentally sharing a connection between a number of processes. However, Django opens a new connection for every request, so that shouldn't be the issue. However, if you open a connection in your Django code that is not part of a request, that could possibly cause something like this.

No, I'm not doing any extra db access. Just using regular Django for everything.

The code is here https://github.com/VenturaFranklin/thetatauCMT

I can't rule out the libraries I use, but that's not a common thing to do.

Since that appears to be a maintained repo. Perhaps you can ask the owners of the repo whether there are issues with running the software in a forked process server?

That's my repo, and I literally only deploy it to pythonanywhere or run it locally for development.

hmm. do you by any chance do use any threads or subprocesses?

No, neither threads nor subprocesses.

It looks like the issue starts with this:

Traceback (most recent call last):
  File "/home/Venturafranklin/.virtualenvs/testCMT/local/lib/python3.6/site-packages/rollbar/__init__.py", line 933, in _add_locals_data
    if arginfo.locals and _check_add_locals(cur_frame, frame_num, num_frames):
  File "/home/Venturafranklin/.virtualenvs/testCMT/local/lib/python3.6/site-packages/rollbar/__init__.py", line 1036, in _check_add_locals
    ('root' in SETTINGS and (frame.get('filename') or '').lower().startswith((SETTINGS['root'] or '').lower()))))
AttributeError: 'Path' object has no attribute 'lower'

Perhaps you can investigate what rollbar is doing to see whether there's something there. Alternatively, try not using rollbar to see whether anything changes in order to isolate the problem.

I fixed that error. I'll see if it is the cause of the original error.

Rollbar is trying to send me the full stack trace or context data so I will see if now I get more information about the "server closed the connection unexpectedly" error or if the error with Rollbar was the cause of the error itself.

Good to see that you made it work.