0%

Django日志提示The request's session was deleted before the request completed

最近发现一个Django写的系统中在某些时候请求会返回400错误,查了下日志,发现在Bad Request日志上面还有这样的错误:

The request’s session was deleted before the request completed. The user may have logged out in a concurrent request, for example.

很奇怪这是个啥意思,于是查了下这句话,发现这个错误仅在django.contrib.sessions.middleware.SessionMiddleware中存在:

1
2
3
4
5
6
7
8
9
10
11
# Save the session data and refresh the client cookie.
# Skip session save for 500 responses, refs #3881.
if response.status_code != 500:
try:
request.session.save()
except UpdateError:
raise SuspiciousOperation(
"The request's session was deleted before the "
"request completed. The user may have logged "
"out in a concurrent request, for example."
)

看起来是在更新session的时候出错,然后就报了这么个错误。我们的session用的是cached_db模式,memcached一般不怎么出问题,那可能就是那个时间数据库只读无法写入导致了这个错误。查了下数据库重启的记录还是真是,那个时间正好无法写入。

想起来我们曾经做过个配置,把SESSION_SAVE_EVERY_REQUEST配置给打开了,以便于用户使用时不会被突然登出。这个配置也导致了每次发起请求后其实都会有一次request.session.save()的操作,也就对应了一次数据库的写入。看来这个问题要么不去管他,要么就是换个更靠谱的session存储方式,要么就是直接把这个配置给关掉了~

如果我的文字帮到了您,那么可不可以请我喝罐可乐?