Intermittent error after upgrading to MySQL 8

New here – hello – hope I’m posting this in the right place. And hope there’s the right info here for this topic to be useful.

After upgrading to MySQL 8 and keeping everything else the same I seem to be getting an intermittent server error which seems to be something to do with a package called cookiebanner that I’ve been using without problems for a couple of years

Here’s the error log:

2025-03-08 05:48:19,903: KeyError: ‘request’
2025-03-08 05:48:19,903: File “XXX/lib/python3.8/site-packages/django/core/handlers/wsgi.py”, line 133, in call
2025-03-08 05:48:19,904: response = self.get_response(request)
2025-03-08 05:48:19,904:
2025-03-08 05:48:19,904: File “XXX/lib/python3.8/site-packages/django/core/handlers/base.py”, line 130, in get_response
2025-03-08 05:48:19,904: response = self._middleware_chain(request)
2025-03-08 05:48:19,905:
2025-03-08 05:48:19,905: File “XXX/lib/python3.8/site-packages/django/core/handlers/exception.py”, line 49, in inner
2025-03-08 05:48:19,905: response = response_for_exception(request, exc)
2025-03-08 05:48:19,905:
2025-03-08 05:48:19,905: File “XXX/lib/python3.8/site-packages/django/core/handlers/exception.py”, line 114, in response_for_exception
2025-03-08 05:48:19,905: response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
2025-03-08 05:48:19,905:
2025-03-08 05:48:19,905: File “XXX/lib/python3.8/site-packages/django/core/handlers/exception.py”, line 153, in handle_uncaught_exception
2025-03-08 05:48:19,905: return callback(request)
2025-03-08 05:48:19,906:
2025-03-08 05:48:19,906: File “XXX/lib/python3.8/site-packages/django/utils/decorators.py”, line 130, in _wrapped_view
2025-03-08 05:48:19,906: response = view_func(request, *args, **kwargs)
2025-03-08 05:48:19,906:
2025-03-08 05:48:19,906: File “XXX/lib/python3.8/site-packages/django/views/defaults.py”, line 97, in server_error
2025-03-08 05:48:19,906: return HttpResponseServerError(template.render())
2025-03-08 05:48:19,906:
2025-03-08 05:48:19,906: File “XXX/lib/python3.8/site-packages/django/template/backends/django.py”, line 61, in render
2025-03-08 05:48:19,906: return self.template.render(context)
2025-03-08 05:48:19,906:
2025-03-08 05:48:19,906: File “XXX/lib/python3.8/site-packages/django/template/base.py”, line 170, in render
2025-03-08 05:48:19,907: return self._render(context)
2025-03-08 05:48:19,907:
2025-03-08 05:48:19,907: File “XXX/lib/python3.8/site-packages/django/template/base.py”, line 162, in _render
2025-03-08 05:48:19,907: return self.nodelist.render(context)
2025-03-08 05:48:19,907:
2025-03-08 05:48:19,907: File “XXX/lib/python3.8/site-packages/django/template/base.py”, line 938, in render
2025-03-08 05:48:19,907: bit = node.render_annotated(context)
2025-03-08 05:48:19,907:
2025-03-08 05:48:19,907: File “XXX/lib/python3.8/site-packages/django/template/base.py”, line 905, in render_annotated
2025-03-08 05:48:19,907: return self.render(context)
2025-03-08 05:48:19,907:
2025-03-08 05:48:19,908: File “XXX/lib/python3.8/site-packages/django/template/library.py”, line 192, in render
2025-03-08 05:48:19,908: output = self.func(*resolved_args, **resolved_kwargs)
2025-03-08 05:48:19,908:
2025-03-08 05:48:19,908: File “XXX/lib/python3.8/site-packages/cookiebanner/templatetags/cookiebanner.py”, line 56, in cookie_accepted
2025-03-08 05:48:19,908: allowed_cookies = context[“request”].COOKIES.get(“cookiebanner”)
2025-03-08 05:48:19,908:
2025-03-08 05:48:19,908: File “XXX/lib/python3.8/site-packages/django/template/context.py”, line 83, in getitem
2025-03-08 05:48:19,909: raise KeyError(key)

According to the access log, it seems to have been from this request:

78.198.210.145 - - [08/Mar/2025:05:48:19 +0000] “GET /podcasts/helga-schubert-story-distance-fragments/ HTTP/1.1” 500 1173 “https://www.google.com/” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15” “78.198.210.145” response-time=0.020

Just tested the same URL and got a 200:

146.199.51.170 - - [08/Mar/2025:10:20:56 +0000] “GET /podcasts/helga-schubert-story-distance-fragments HTTP/1.1” 301 0 “The Fictionable Podcast” “Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0” “146.199.51.170” response-time=0.001
146.199.51.170 - - [08/Mar/2025:10:20:56 +0000] “GET /podcasts/helga-schubert-story-distance-fragments/ HTTP/1.1” 200 8051 “The Fictionable Podcast” “Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0” “146.199.51.170” response-time=0.017

It’s the tag {% cookie_accepted ‘analytics’ as cookie_analytics %} that calls cookie_accepted

cookie_accepted is from the cookiebanner package – a package I’ve been using with no problems for a couple of years. Here’s the code that defines cookie_accepted with that allowed_cookies = context[“request”].COOKIES.get(“cookiebanner”) line that seems to be throwing the exception:

@register.simple_tag(takes_context=True)
def cookie_accepted(context, cookie_group):
allowed_cookies = context[“request”].COOKIES.get(“cookiebanner”)
if not allowed_cookies:
return False
return cookie_group in unquote(allowed_cookies).split(“,”)

Here’s the requirements.txt for all the packages:

asgiref==3.5.0
dj-database-url==0.5.0
Django==3.2.12
gunicorn==20.1.0
pytz==2021.3
sqlparse==0.4.2
whitenoise==6.0.0
django-cookiebanner==0.2.5
mysqlclient==2.1.0
stripe==2.73.0
dj-stripe==2.6.1

This intermittent error seems to date back to when I tried to upgrade to Django 4.2. This required me to update to MySQL 8. The upgrade to Django 4 didn’t work, so I rolled everything back to the versions specified above.
But after rolling everything back, I started getting this intermittent error.

So am puzzled about:

  • why the context for building this template (sometimes) doesn’t include the request that the cookibanner package is expecting
  • why the same code that worked before upgrading to MySQL 8 is now throwing an error that seems to be about the cookiebanner package that I’ve been using without problems for a couple of years
  • why the same request for the same URL throws a keyerror at 5:48am and not at 10:20am

Give me a shout if I’ve missed out any crucial info, or if there’s another way or somewhere else I should be posting this kind of topic.
Any thoughts much appreciated.

Posting the code for the template tag again, with an attempt to format it a little better:

The template for this page calls a tag from the cookiebanner package. Here’s the top of the base template, with the call to cookiebanner:

<!DOCTYPE html>
<html lang="en" ontouchmove>
	<head>
		{% load static %}
		{% load cookiebanner %}
		{% block styles_content %}
		{% endblock styles_content %}
		{% cookie_accepted 'analytics' as cookie_analytics %}
		{% if cookie_analytics %}
		<!-- Global site tag (gtag.js) - Google Analytics -->
		<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXX"></script>
		<script>
			window.dataLayer = window.dataLayer || [];
			function gtag(){dataLayer.push(arguments);}
			gtag('js', new Date());
			
			gtag('config', 'XXX');
		</script>
		{% endif %}
[CONTINUES]