'Early hang up on socket.send
In other question Python - How to wait on both queue and a socket on same time sockets are used to organize waiting for event in a queue. But what I have faced with is that socket.send hangs up after 10s to 100s 1-byte send calls. Simplified version of the queue is below.
import os
import socket
import sys
import queue
class PollableQueue(queue.Queue):
def __init__(self):
maxsize = 1000
super().__init__(maxsize=maxsize)
# Create a pair of connected sockets
if os.name == 'posix':
self._putsocket, self._getsocket = socket.socketpair()
else:
# Workaround for non-POSIX systems which can't wait events with select (require fileno())
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('127.0.0.1', 0))
server.listen(1)
self._putsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._putsocket.connect(server.getsockname())
self._getsocket, _ = server.accept()
server.close()
curBufSize = self._putsocket.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
print('_putsocket buf size %d ' % curBufSize)
self._putsocket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, maxsize + 100)
def fileno(self):
return self._getsocket.fileno()
def put(self, item):
super().put(item, block=False)
print('p', end='')
sys.stdout.flush()
self._putsocket.send(b'x') # Problem send
print(' .p', end='')
sys.stdout.flush()
if __name__ == '__main__':
q = PollableQueue()
i = 0
while True:
q.put('aaa')
i += 1
print(' %d' % i, end='')
sys.stdout.flush()
If I set maxsize = 0 and doesn't call setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, ...), I get output "... p .p 267p .p 268p .p 269p .p 270p .p 271p .p 272p .p 273p .p 274p .p 275p .p 276p .p 277p .p 278p". So 278 times we were able to put byte into the socket and then hanged up.
But if I want to be able to put 1000 elements into the queue while consumer is busy, I set maxsize = 1000 and call setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1100), I get hang up after 6 sends!
We can play with different sizes of the buffer, but this doesn't solve the problem. This reproduces on Linux jetson-nx 4.9.253-tegra #26 SMP PREEMPT Thu Jan 6 00:20:07 PST 2022 aarch64 aarch64 aarch64 GNU/Linux
(Jetson) and on Linux version 4.15.0-142-generic (buildd@lgw01-amd64-036) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #146-Ubuntu SMP Tue Apr 13 01:11:19 UTC 2021 (a server) and doesn't - on Windows 7.
Initial socket buffer size is 212992, looks a bit too much, on Windows it is like 8 KB.
Am I doing something wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
