Closed pySilver closed 3 years ago
@pySilver thanks for reporting. I have not tested this package in an ASGI environment. Does this happens on Django 3.0 or are you testing on 3.1? Which is the python version are you running on?
@yakky its django 3.0.x running on python 3.8.x ;
Deployed as described here in Gunicorn section: https://www.uvicorn.org/deployment/
Sadly, the issue is not happening constantly since it's staging environment with a low traffic at the moment. But the issue pops up often in sentry :/
Looks like multiple requests shares the same model instance so race condition appears when it comes to delattr. Im not an expert in async (yet..). Maybe the only workaround would be passing request object to every handler, instead of making it available in temporary attribute.
Alternatively it should be possible to thread locals, which has an ASGI safe implementation
You mean using Local
from asgiref.local ? Django 3.0.x uses this for language activation and such things. However i think it would look rly strange here.
@pySilver I have been unable to reproduce the error, but using thread locals should fix it anyway
You can test the linked PR if it fixes your error
I've had this issue all the time. However I fixed it by moving request away from model.
@pySilver I trust that, but I couldn't fine a way to reproduce it in tests
The implemented solutions is basically to remove the request from the model object as you did
Yeah, I couldn't reproduce it with test either. It happens on race condition only when one request sets request object while other removes. It might be also related to pickling model instances to cache layer.
@pySilver I agree with your analysis, and in hindsight caching request on the model has been a bad decision :)
For some reason this error happens from time to time. Django 3.x project is running as ASGI