Fix port in use error on certificate replacement (#2185)

When switching certificates there is a race condition with a concurrent API request. If there is one active during the replacement period then the replacement will error out with a port in use error and Patroni gets stuck in a state without an active API server.

Fix is to call server_close after shutdown which will wait for already running requests to complete before returning.

Close #2184
This commit is contained in:
Ants Aasma
2022-01-26 13:52:25 +01:00
committed by GitHub
parent 3e1076a574
commit 0980838cb3
2 changed files with 4 additions and 1 deletions
+2
View File
@@ -768,6 +768,8 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
reloading_config = self.__listen is not None # changing config in runtime
if reloading_config:
self.shutdown()
# Rely on ThreadingMixIn.server_close() to have all requests terminate before we continue
self.server_close()
self.__listen = listen
self.__ssl_options = ssl_options
+2 -1
View File
@@ -549,7 +549,8 @@ class TestRestApiServer(unittest.TestCase):
self.assertRaises(ValueError, MockRestApiServer, None, '', bad_config)
self.assertRaises(ValueError, self.srv.reload_config, bad_config)
self.assertRaises(ValueError, self.srv.reload_config, {})
with patch.object(socket.socket, 'setsockopt', Mock(side_effect=socket.error)):
with patch.object(socket.socket, 'setsockopt', Mock(side_effect=socket.error)), \
patch.object(MockRestApiServer, 'server_close', Mock()):
self.srv.reload_config({'listen': ':8008'})
@patch.object(MockPatroni, 'dcs')