Skip to content

Request.objects.all().delete() is dangerous #711

Open
@moonsikpark

Description

@moonsikpark

django-silk/silk/models.py

Lines 167 to 170 in 1cb4623

# Make sure we can delete everything if needed by settings
if target_count <= 0:
cls.objects.all().delete()
return

If you call all() on a table with many rows, it will retrieve all the data at once, which will likely result in out-of-memory (OOM) errors.

As a workaround, make sure to set the SILKY_MAX_RECORDED_REQUESTS_CHECK_PERCENT very low (0.01 for example so it runs every 10_000 requests) and the SILKY_MAX_RECORDED_REQUESTS high (100_000 or more).

This way, the chances of running into this issue are much smaller (but still possible, depending on your load).

An even better workaround is to set SILKY_MAX_RECORDED_REQUESTS_CHECK_PERCENT to 0 and run the silky.Request.garbage_collect(force=True) method in a scheduled task through celery.

Originally posted by @okke-formsma in #265 (comment)

I previously used Celery to call this function to avoid deadlock, as suggested in the referenced comment. However, due to OOM errors, our scheduler failed to clear the database, leading to excessive database usage.

I recommend setting a limit and deleting the rows recursively instead. I will submit a pull request to address this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions