Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When the authentication middleware was added in pulpcore 3.15, it became the first place in the content app that made an attempt to use the database. As a result, it is a convinient place to handle InterfaceError and OperationalError which are raised when the database connection has been closed. When this occurs, Handler._reset_db_connection() is called to clean up the database connection and the middleware tries to use the database again. If the database connection is closed later in the handling of the request by the content app, the user will still get a 500 error. However, the next request will be handled properly. This patch also adds a call to Handler._reset_db_connection() inside the heartbeat method. backports #9276 fixes: #9598 https://pulp.plan.io/issues/9598 (cherry picked from commit 3faa649)
- Loading branch information
Showing
6 changed files
with
96 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fixed bug where the content app would stop working after a brief loss of connection to the database. | ||
(backported from #9276) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import asyncio | ||
from unittest import skip | ||
from unittest.mock import patch, call | ||
|
||
from django.db.utils import InterfaceError, OperationalError | ||
from django.test import TestCase | ||
|
||
from pulpcore.content import _heartbeat | ||
|
||
|
||
class ContentHeartbeatTestCase(TestCase): | ||
@skip("Skipping while resolving https://github.com/rochacbruno/dynaconf/issues/689") | ||
@patch("pulpcore.app.models.ContentAppStatus.objects.get_or_create") | ||
@patch("pulpcore.content.handler.Handler._reset_db_connection") | ||
def test_db_connection_interface_error(self, mock_reset_db, mock_get_or_create): | ||
""" | ||
Test that if an InterfaceError or OperationalError is raised, | ||
Handler._reset_db_connection() is called | ||
""" | ||
|
||
class MockException(Exception): | ||
pass | ||
|
||
mock_get_or_create.side_effect = [InterfaceError(), OperationalError(), MockException()] | ||
|
||
loop = asyncio.get_event_loop() | ||
with self.settings(CONTENT_APP_TTL=1): | ||
try: | ||
loop.run_until_complete(_heartbeat()) | ||
except MockException: | ||
pass | ||
loop.close() | ||
|
||
mock_get_or_create.assert_called() | ||
mock_reset_db.assert_has_calls([call(), call()]) |