The pattern is already established elsewhere in adit.core.tasks and adit.mass_transfer.tasks (linked to procrastinate-org/procrastinate#1106). check_disk_space queries DicomFolder.objects.all() and currently doesn't close the connection at the end, leaking a Django per-thread connection into the asgiref sync_to_async worker thread.
In production it accumulates over time; in tests it contributes to the test-DB-teardown warning addressed by openradx/adit-radis-shared#197.
Proposed fix
from django import db
@app.periodic(cron="0 7 * * *")
@app.task
def check_disk_space(*args, **kwargs):
try:
# ... existing body ...
finally:
db.close_old_connections()
Same try/finally pattern as the other ADIT tasks that link to procrastinate#1106.
Audit
A grep across adit/*/tasks.py to catch any other periodic or task functions using ORM without db.close_old_connections() would be worthwhile while addressing this one.
The pattern is already established elsewhere in
adit.core.tasksandadit.mass_transfer.tasks(linked to procrastinate-org/procrastinate#1106).check_disk_spacequeriesDicomFolder.objects.all()and currently doesn't close the connection at the end, leaking a Django per-thread connection into the asgirefsync_to_asyncworker thread.In production it accumulates over time; in tests it contributes to the test-DB-teardown warning addressed by openradx/adit-radis-shared#197.
Proposed fix
Same try/finally pattern as the other ADIT tasks that link to procrastinate#1106.
Audit
A grep across
adit/*/tasks.pyto catch any other periodic or task functions using ORM withoutdb.close_old_connections()would be worthwhile while addressing this one.