Skip to content

Commit

Permalink
Merge pull request #535 from daviddavis/issue6045
Browse files Browse the repository at this point in the history
Fix "connection already closed" error in content app
  • Loading branch information
daviddavis committed Feb 11, 2020
2 parents f728fff + 5aabb20 commit 700366e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/plugin_api/6045.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A new method '_reset_db_connection' has been added to content.Handler. It should be called before accessing the db to ensure that the db connection is alive.
13 changes: 12 additions & 1 deletion pulpcore/content/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from aiohttp.web_exceptions import HTTPForbidden, HTTPFound, HTTPNotFound
from django.conf import settings
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.db import IntegrityError, transaction
from django.db import connection, IntegrityError, transaction
from pulpcore.app.models import (
Artifact,
BaseDistribution,
Expand Down Expand Up @@ -84,6 +84,13 @@ class Handler:

distribution_model = None

@staticmethod
def _reset_db_connection():
"""
Reset database connection if it's unusable or obselete to avoid "connection already closed".
"""
connection.close_if_unusable_or_obsolete()

async def list_distributions(self, request):
"""
The handler for an HTML listing all distributions
Expand All @@ -94,6 +101,8 @@ async def list_distributions(self, request):
Returns:
:class:`aiohttp.web.HTTPOk`: The response back to the client.
"""
self._reset_db_connection()

if self.distribution_model is None:
distributions = BaseDistribution.objects.only("base_path").all()
else:
Expand All @@ -112,6 +121,8 @@ async def stream_content(self, request):
:class:`aiohttp.web.StreamResponse` or :class:`aiohttp.web.FileResponse`: The response
back to the client.
"""
self._reset_db_connection()

path = request.match_info['path']
return await self._match_and_stream(path, request)

Expand Down

0 comments on commit 700366e

Please sign in to comment.