Forums

Flask app stops running

Hello,

I'm trying to find out why my Flask app stops working after a while. When I click the "Reload" button, my app works. But after a while I get this.

Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

2025-03-10 17:27:41 Mon Mar 10 17:27:41 2025 - received message 0 from emperor
2025-03-10 17:27:41 SIGINT/SIGTERM received...killing workers...
2025-03-10 17:27:42 worker 1 buried after 1 seconds
2025-03-10 17:27:42 goodbye to uWSGI.
2025-03-10 17:27:42 VACUUM: unix socket /var/sockets/site.pythonanywhere.com/socket removed.
2025-03-10 17:27:48 *** Starting uWSGI 2.0.20 (64bit) on [Mon Mar 10 17:27:47 2025] ***
2025-03-10 17:27:48 compiled with version: 9.4.0 on 22 July 2022 18:35:26
2025-03-10 17:27:48 os: Linux-6.5.0-1022-aws #22~22.04.1-Ubuntu SMP Fri Jun 14 16:31:00 UTC 2024
2025-03-10 17:27:48 nodename: blue-liveweb32
2025-03-10 17:27:48 machine: x86_64
2025-03-10 17:27:48 clock source: unix
2025-03-10 17:27:48 pcre jit disabled
2025-03-10 17:27:48 detected number of CPU cores: 4
2025-03-10 17:27:48 current working directory: /home/site
2025-03-10 17:27:48 detected binary path: /usr/local/bin/uwsgi
2025-03-10 17:27:48 *** dumping internal routing table ***
2025-03-10 17:27:48 [rule: 0] subject: path_info regexp: \.svgz$ action: addheader:Content-Encoding:gzip
2025-03-10 17:27:48 *** end of the internal routing table ***
2025-03-10 17:27:48 chdir() to /home/site/
2025-03-10 17:27:48 your processes number limit is 256
2025-03-10 17:27:48 your memory page size is 4096 bytes
2025-03-10 17:27:48 detected max file descriptor number: 123456
2025-03-10 17:27:48 building mime-types dictionary from file /etc/mime.types...
2025-03-10 17:27:48 567 entry found
2025-03-10 17:27:48 lock engine: pthread robust mutexes
2025-03-10 17:27:48 thunder lock: disabled (you can enable it with --thunder-lock)
2025-03-10 17:27:48 uwsgi socket 0 bound to UNIX address /var/sockets/site.pythonanywhere.com/socket fd 3
2025-03-10 17:27:48 Python version: 3.10.5 (main, Jul 22 2022, 17:09:35) [GCC 9.4.0]
2025-03-10 17:27:48 *** Python threads support is disabled. You can enable it with --enable-threads ***
2025-03-10 17:27:48 Python main interpreter initialized at 0x55788d2fce90
2025-03-10 17:27:48 your server socket listen backlog is limited to 100 connections
2025-03-10 17:27:48 your mercy for graceful operations on workers is 60 seconds
2025-03-10 17:27:48 setting request body buffering size to 65536 bytes
2025-03-10 17:27:48 mapped 334256 bytes (326 KB) for 1 cores
2025-03-10 17:27:48 *** Operational MODE: single process ***
2025-03-10 17:27:48 initialized 38 metrics
2025-03-10 17:27:48 WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x55788d2fce90 pid: 1 (default app)
2025-03-10 17:27:48 *** uWSGI is running in multiple interpreter mode ***
2025-03-10 17:27:48 gracefully (RE)spawned uWSGI master process (pid: 1)
2025-03-10 17:27:48 spawned uWSGI worker 1 (pid: 2, cores: 1)
2025-03-10 17:27:48 metrics collector thread started
2025-03-10 17:27:48 spawned 2 offload threads for uWSGI worker 1
2025-03-10 17:27:50 announcing my loyalty to the Emperor...

Thanks,

Tony

[edit by admin: formatting]

An internal server error will normally log the details of the problem to the website's error log rather than the server log (which is the one you pasted in above). Could you take a look there? The most recent error will be the one at the bottom of the file.

The website error logs show this, and I made a change to the db.py file. Waiting to see if that keeps the Flask app running.

mysql.connector.errors.OperationalError: MySQL Connection not available.
2025-03-11 12:52:52,457: Exception on / [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/tonynsx/grocery_list/app.py", line 8, in index
    menu = db.showAll()
  File "/home/tonynsx/grocery_list/db.py", line 11, in showAll

How are you managing your database connections -- is it with Flask-SQLAlchemy? If so, see this help page.

Sorry for the late reply. Its a MySQL database. Still having this issue. For example, if I insert something, the website shows correctly. No errors. But then without doing anything, the website doesn't open and shows internal server error.

How are you managing your database connections in db.py?