mirror of
https://github.com/openvswitch/ovs
synced 2025-09-02 15:25:22 +00:00
ovs python: ovs.stream.open_block() returns success even if the remote is unreachable
The python function ovs.socket_util.check_connection_completion() uses select() (provided by python) to monitor the socket file descriptor. The select() returns 1 when the file descriptor becomes ready. For error cases like - 111 (Connection refused) and 113 (No route to host) (POLLERR), ovs.poller._SelectSelect.poll() expects the exceptfds list to be set by select(). But that is not the case. As per the select() man page, writefds list will be set for POLLERR. Please see "Correspondence between select() and poll() notifications" section of select(2) man page. Because of this behavior, ovs.socket_util.check_connection_completion() returns success even if the remote is unreachable or not listening on the port. This patch fixes this issue by using poll() to check the connection status similar to the C implementation of check_connection_completion(). A new function 'get_system_poll() is added in ovs/poller.py which returns the select.poll() object. If select.poll is monkey patched by eventlet/gevent, it gets the original select.poll() and returns it. The test cases added in this patch fails without the fix. Suggested-by: Ben Pfaff <blp@ovn.org> Signed-off-by: Numan Siddique <nusiddiq@redhat.com> Signed-off-by: Ben Pfaff <blp@ovn.org> Acked-by: Mark Michelson <mmichels@redhat.com>
This commit is contained in:
committed by
Ben Pfaff
parent
44a629b5b8
commit
c1aa16d191
@@ -31,14 +31,22 @@ except ImportError:
|
||||
SSL = None
|
||||
|
||||
try:
|
||||
import eventlet.patcher
|
||||
from eventlet import patcher as eventlet_patcher
|
||||
|
||||
def _using_eventlet_green_select():
|
||||
return eventlet.patcher.is_monkey_patched(select)
|
||||
return eventlet_patcher.is_monkey_patched(select)
|
||||
except:
|
||||
eventlet_patcher = None
|
||||
|
||||
def _using_eventlet_green_select():
|
||||
return False
|
||||
|
||||
try:
|
||||
from gevent import monkey as gevent_monkey
|
||||
except:
|
||||
gevent_monkey = None
|
||||
|
||||
|
||||
vlog = ovs.vlog.Vlog("poller")
|
||||
|
||||
POLLIN = 0x001
|
||||
@@ -257,3 +265,26 @@ class Poller(object):
|
||||
def __reset(self):
|
||||
self.poll = SelectPoll()
|
||||
self.timeout = -1
|
||||
|
||||
|
||||
def get_system_poll():
|
||||
"""Returns the original select.poll() object. If select.poll is
|
||||
monkey patched by eventlet or gevent library, it gets the original
|
||||
select.poll and returns an object of it using the
|
||||
eventlet.patcher.original/gevent.monkey.get_original functions.
|
||||
|
||||
As a last resort, if there is any exception it returns the
|
||||
SelectPoll() object.
|
||||
"""
|
||||
try:
|
||||
if _using_eventlet_green_select():
|
||||
_system_poll = eventlet_patcher.original("select").poll
|
||||
elif gevent_monkey and gevent_monkey.is_object_patched(
|
||||
'select', 'poll'):
|
||||
_system_poll = gevent_monkey.get_original('select', 'poll')
|
||||
else:
|
||||
_system_poll = select.poll
|
||||
except:
|
||||
_system_poll = SelectPoll
|
||||
|
||||
return _system_poll()
|
||||
|
@@ -162,8 +162,8 @@ def make_unix_socket(style, nonblock, bind_path, connect_path, short=False):
|
||||
|
||||
|
||||
def check_connection_completion(sock):
|
||||
p = ovs.poller.SelectPoll()
|
||||
if sys.platform == "win32":
|
||||
p = ovs.poller.SelectPoll()
|
||||
event = winutils.get_new_event(None, False, True, None)
|
||||
# Receive notification of readiness for writing, of completed
|
||||
# connection or multipoint join operation, and of socket closure.
|
||||
@@ -173,6 +173,7 @@ def check_connection_completion(sock):
|
||||
win32file.FD_CLOSE)
|
||||
p.register(event, ovs.poller.POLLOUT)
|
||||
else:
|
||||
p = ovs.poller.get_system_poll()
|
||||
p.register(sock, ovs.poller.POLLOUT)
|
||||
pfds = p.poll(0)
|
||||
if len(pfds) == 1:
|
||||
@@ -180,7 +181,7 @@ def check_connection_completion(sock):
|
||||
if revents & ovs.poller.POLLERR:
|
||||
try:
|
||||
# The following should raise an exception.
|
||||
socket.send("\0", socket.MSG_DONTWAIT)
|
||||
sock.send("\0".encode(), socket.MSG_DONTWAIT)
|
||||
|
||||
# (Here's where we end up if it didn't.)
|
||||
# XXX rate-limit
|
||||
|
@@ -191,8 +191,15 @@ class Stream(object):
|
||||
if error:
|
||||
return error, None
|
||||
else:
|
||||
status = ovs.socket_util.check_connection_completion(sock)
|
||||
return 0, cls(sock, name, status)
|
||||
err = ovs.socket_util.check_connection_completion(sock)
|
||||
if err == errno.EAGAIN or err == errno.EINPROGRESS:
|
||||
status = errno.EAGAIN
|
||||
err = 0
|
||||
elif err == 0:
|
||||
status = 0
|
||||
else:
|
||||
status = err
|
||||
return err, cls(sock, name, status)
|
||||
|
||||
@staticmethod
|
||||
def _open(suffix, dscp):
|
||||
|
Reference in New Issue
Block a user