From 353fc58bee473cc0dc4522d5215ad60957938971 Mon Sep 17 00:00:00 2001 From: Alexander Kukushkin Date: Wed, 26 Aug 2015 15:14:44 +0200 Subject: [PATCH] Do not inherit rest api port on fork --- helpers/api.py | 7 +++++++ tests/test_patroni.py | 1 + 2 files changed, 8 insertions(+) diff --git a/helpers/api.py b/helpers/api.py index 4274f786..d9a0f527 100644 --- a/helpers/api.py +++ b/helpers/api.py @@ -1,3 +1,4 @@ +import fcntl import json import logging import psycopg2 @@ -55,6 +56,7 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread): host, port = config['listen'].split(':') HTTPServer.__init__(self, (host, int(port)), RestApiHandler) Thread.__init__(self, target=self.serve_forever) + self._set_fd_cloexec(self.socket) self.patroni = patroni self.daemon = True @@ -64,3 +66,8 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread): ret = [r for r in cursor] cursor.close() return ret + + @staticmethod + def _set_fd_cloexec(fd): + flags = fcntl.fcntl(fd, fcntl.F_GETFD) + fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC) diff --git a/tests/test_patroni.py b/tests/test_patroni.py index 7d772f68..67ac1ffd 100644 --- a/tests/test_patroni.py +++ b/tests/test_patroni.py @@ -57,6 +57,7 @@ class TestPatroni(unittest.TestCase): BaseHTTPServer.HTTPServer.__init__ = nop RestApiServer._BaseServer__is_shut_down = Mock_BaseServer__is_shut_down() RestApiServer._BaseServer__shutdown_request = True + RestApiServer.socket = 0 with open('postgres0.yml', 'r') as f: config = yaml.load(f) with patch.object(Client, 'machines') as mock_machines: