Skip to content

Commit

Permalink
nlsocket: merge pull request #1048 from svinota/peek_blocking_io_error
Browse files Browse the repository at this point in the history
nlsocket: fix BlockingIOError on socket peek ops

Bug-Url: #1045
Bug-Url: #1048
  • Loading branch information
svinota authored Oct 26, 2022
2 parents 0f3e967 + 94eb558 commit 642f0a4
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pyroute2/netlink/nlsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ def __init__(
self._fileno = fileno
self._sndbuf = sndbuf
self._rcvbuf = rcvbuf
self._use_peek = True
self.backlog = {0: []}
self.error_deque = collections.deque(maxlen=1000)
self.callbacks = [] # [(predicate, callback, args), ...]
Expand Down Expand Up @@ -1012,21 +1013,28 @@ def get_policy_map(self, policy=None):

def _peek_bufsize(self, socket_descriptor):
data = bytearray()
bufsize, _ = socket_descriptor.recvfrom_into(
data, 0, MSG_DONTWAIT | MSG_PEEK | MSG_TRUNC
)
try:
bufsize, _ = socket_descriptor.recvfrom_into(
data, 0, MSG_DONTWAIT | MSG_PEEK | MSG_TRUNC
)
except BlockingIOError:
self._use_peek = False
bufsize = socket_descriptor.getsockopt(SOL_SOCKET, SO_RCVBUF) // 2
return bufsize

def sendto(self, *argv, **kwarg):
return self._sendto(*argv, **kwarg)

def recv(self, *argv, **kwarg):
def recv(self, bufsize, flags=0):
if self.input_from_buffer_queue:
data_in = self.buffer_queue.get()
if isinstance(data_in, Exception):
raise data_in
return data_in
return self._sock.recv(self._peek_bufsize(self._sock), **kwarg)
return self._sock.recv(
self._peek_bufsize(self._sock) if self._use_peek else bufsize,
flags,
)

def recv_into(self, data, *argv, **kwarg):
if self.input_from_buffer_queue:
Expand Down

0 comments on commit 642f0a4

Please sign in to comment.