Spaces:
Running
Running
thadillo
Claude
commited on
Commit
·
df450f1
1
Parent(s):
4e9938a
Fix SQLite database locking errors on HF Spaces
Browse filesFixes: sqlite3.OperationalError: database is locked
Changes:
- Increased connection timeout from 5s to 30s
- Enabled check_same_thread=False for multi-threading
- Added pool_pre_ping to verify connections
- Enabled SQLite WAL (Write-Ahead Logging) mode
- Set busy_timeout to 30 seconds
- Reduced synchronous mode to NORMAL for better performance
WAL mode allows concurrent reads while writing, preventing most
locking errors. Essential for HF Spaces with multiple workers.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
- app/__init__.py +23 -0
app/__init__.py
CHANGED
|
@@ -29,8 +29,31 @@ def create_app():
|
|
| 29 |
|
| 30 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
db.init_app(app)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Import models
|
| 35 |
from app.models import models
|
| 36 |
|
|
|
|
| 29 |
|
| 30 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 31 |
|
| 32 |
+
# SQLite-specific settings to reduce locking issues
|
| 33 |
+
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
|
| 34 |
+
'connect_args': {
|
| 35 |
+
'timeout': 30, # Increase timeout to 30 seconds
|
| 36 |
+
'check_same_thread': False # Allow multi-threaded access
|
| 37 |
+
},
|
| 38 |
+
'pool_pre_ping': True, # Verify connections before using
|
| 39 |
+
'pool_recycle': 3600, # Recycle connections every hour
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
db.init_app(app)
|
| 43 |
|
| 44 |
+
# Enable WAL mode for SQLite to reduce locking
|
| 45 |
+
with app.app_context():
|
| 46 |
+
from sqlalchemy import event
|
| 47 |
+
from sqlalchemy.engine import Engine
|
| 48 |
+
|
| 49 |
+
@event.listens_for(Engine, "connect")
|
| 50 |
+
def set_sqlite_pragma(dbapi_conn, connection_record):
|
| 51 |
+
cursor = dbapi_conn.cursor()
|
| 52 |
+
cursor.execute("PRAGMA journal_mode=WAL") # Write-Ahead Logging
|
| 53 |
+
cursor.execute("PRAGMA synchronous=NORMAL") # Balance safety/performance
|
| 54 |
+
cursor.execute("PRAGMA busy_timeout=30000") # 30 second timeout
|
| 55 |
+
cursor.close()
|
| 56 |
+
|
| 57 |
# Import models
|
| 58 |
from app.models import models
|
| 59 |
|