Regular crashes during normal operation #180
Labels
No labels
UserStory
bug
duplicate
enhancement
help wanted
hw task
invalid
question
refactoring
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
mattzz/halloween#180
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
After 20-30 min of scaring the pumpkin application crashes with the following error message:
One suggestion from chatgpt is:
Here's the entire log file of the young couple
My theory is that the slower responses of the server when several hosts work in parallel and the Wifi is burdened by the visitors and that leads to threading problems with SQLite.
The default of requests.get is no timeout - which may worsen the issue. A timeout definitely needs to be added.
I will also test if it works to create a separate connection to the file. It may be better however to use a PostgreSQL db.
I'll first add logging and fire up a few hosts in parallel and see how long those requests take without any changes.
A question is if we need a database at all. With those little data we have we could also keep them in a thread that maintains a data structure in memory and offers access functions. A postgreSQL db might be a problem for Raspberry Pi Zeros...
I will build up an experiment: Add a memoryDB module and use that as database instead in database.py...
This is what ChatGPT suggests as memoryDB:
'memorydb.py
import threading
class MemoryDB:
Hm. Glad we found that it is clearly db related.
My understanding is that there is no global context in this kind of web applications, so it's less about persistence of data and more about accessing a global context, shared via db. So in order to share a global context, db usage seemed to be way. However, maybe there are other ways to provide global context.
Regarding in memory storage: The db is already in memory, as far as I remember. The sqlite file is being served from a ram disk to prevent flash corruption due to frequent writes.
If there was a way to make sqlite thread safe, that would be the route I would chose. And refactoring open/close calls. Postgres/Mariadb etc are way too heavy solutions for this problem, imho.
On sqlite side of things there are a few improvements possible:
I have done a bit of research about flask and the database usage and I think I found an issue with how the data connection is used by the endless task. The database connection is meant to be used by a request and to be destroyed afterwards. However, we now have a number of endless tasks which access the database and write to it. And they open the connection, write to it here and there and never close it. This seems wrong.
Regarding file system and sd card accesses: The database is stored in /run/shm which is an in-memory location which is lost after power down. So there are no SD card write accesses by database accesses.
Let's identify accesses to the db which deviate from the normal approach of opening the connection for an instant request responses:
ChatGPT recommends the following:
In Flask request handlers
def get_db(): if 'db' not in g: g.db = sqlite3.connect(DATABASE) return g.dbEach request runs in its own thread, so g.db is its per-thread connection. Flask tears it down afterward.
In background threads
def background_worker(): conn = sqlite3.connect(DATABASE) while True: # Do long or repeating work ...Each worker opens its own connection once and reuses it until it’s done.
Requests get their own thread and their own db connection and we only have to make sure that non-request-triggered background tasks have their own connection and don't create interfering requests.