summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2019-01-17 13:52:22 +0000
committerGerrit Code Review <review@openstack.org>2019-01-17 13:52:22 +0000
commit3c645a77ec04a36e999bb985dbe0116588b81ef0 (patch)
tree20e2f6941ae80d87ea7dfdf64be1b5a0dbc4c372
parent4c0d4490e8dcac1f02d8b94c6937b3e258b5d594 (diff)
parent811650783d32accd2b49f907ddc520955146962c (diff)
Merge "Avoid eventlet_backdoor listing on same port"1.36.0
-rw-r--r--oslo_service/eventlet_backdoor.py21
-rw-r--r--oslo_service/tests/test_eventlet_backdoor.py8
2 files changed, 25 insertions, 4 deletions
diff --git a/oslo_service/eventlet_backdoor.py b/oslo_service/eventlet_backdoor.py
index f4ecbf4..a82d5e2 100644
--- a/oslo_service/eventlet_backdoor.py
+++ b/oslo_service/eventlet_backdoor.py
@@ -21,13 +21,13 @@ import gc
21import logging 21import logging
22import os 22import os
23import pprint 23import pprint
24import socket
25import sys 24import sys
26import traceback 25import traceback
27 26
28import eventlet.backdoor 27import eventlet.backdoor
29import greenlet 28import greenlet
30 29
30from eventlet.green import socket
31from oslo_service._i18n import _ 31from oslo_service._i18n import _
32from oslo_service import _options 32from oslo_service import _options
33 33
@@ -121,11 +121,24 @@ def _parse_port_range(port_range):
121 port_range, ex, _options.help_for_backdoor_port) 121 port_range, ex, _options.help_for_backdoor_port)
122 122
123 123
124def _listen(host, start_port, end_port, listen_func): 124def _listen_func(host, port):
125 # eventlet is setting SO_REUSEPORT by default from v0.20.
126 # But we can configure it by passing reuse_port argument
127 # from v0.22
128 try:
129 return eventlet.listen((host, port), reuse_port=False)
130 except TypeError:
131 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
132 sock.bind((host, port))
133 sock.listen(50)
134 return sock
135
136
137def _listen(host, start_port, end_port):
125 try_port = start_port 138 try_port = start_port
126 while True: 139 while True:
127 try: 140 try:
128 return listen_func((host, try_port)) 141 return _listen_func(host, try_port)
129 except socket.error as exc: 142 except socket.error as exc:
130 if (exc.errno != errno.EADDRINUSE or 143 if (exc.errno != errno.EADDRINUSE or
131 try_port >= end_port): 144 try_port >= end_port):
@@ -169,7 +182,7 @@ def _initialize_if_enabled(conf):
169 182
170 if conf.backdoor_socket is None: 183 if conf.backdoor_socket is None:
171 start_port, end_port = _parse_port_range(str(conf.backdoor_port)) 184 start_port, end_port = _parse_port_range(str(conf.backdoor_port))
172 sock = _listen('localhost', start_port, end_port, eventlet.listen) 185 sock = _listen('localhost', start_port, end_port)
173 # In the case of backdoor port being zero, a port number is assigned by 186 # In the case of backdoor port being zero, a port number is assigned by
174 # listen(). In any case, pull the port number out here. 187 # listen(). In any case, pull the port number out here.
175 where_running = sock.getsockname()[1] 188 where_running = sock.getsockname()[1]
diff --git a/oslo_service/tests/test_eventlet_backdoor.py b/oslo_service/tests/test_eventlet_backdoor.py
index 4c00c57..480761e 100644
--- a/oslo_service/tests/test_eventlet_backdoor.py
+++ b/oslo_service/tests/test_eventlet_backdoor.py
@@ -106,6 +106,14 @@ class BackdoorPortTest(base.ServiceBaseTestCase):
106 eventlet_backdoor.initialize_if_enabled, self.conf) 106 eventlet_backdoor.initialize_if_enabled, self.conf)
107 107
108 @mock.patch.object(eventlet, 'spawn') 108 @mock.patch.object(eventlet, 'spawn')
109 def test_backdoor_port_range_inuse(self, spawn_mock):
110 self.config(backdoor_port='8800:8801')
111 port = eventlet_backdoor.initialize_if_enabled(self.conf)
112 self.assertEqual(8800, port)
113 port = eventlet_backdoor.initialize_if_enabled(self.conf)
114 self.assertEqual(8801, port)
115
116 @mock.patch.object(eventlet, 'spawn')
109 @mock.patch.object(eventlet, 'listen') 117 @mock.patch.object(eventlet, 'listen')
110 def test_backdoor_port_range(self, listen_mock, spawn_mock): 118 def test_backdoor_port_range(self, listen_mock, spawn_mock):
111 self.config(backdoor_port='8800:8899') 119 self.config(backdoor_port='8800:8899')