Use kqueue to detect hangs on the other side of the connector or the condition of the socket

I read man 2 kqueue, but did not find out, how I can receive a notification about a socket hang or an exceptional state of a socket without registering it with EVFILT_READor EVFILT_WRITE. In addition, it is not entirely clear how kqueue signals exceptional socket states.

Thanks for your reply in advance.

+3
source share
1 answer

The trick that can be used to receive EOL events when ignoring all READ events is to deliver a ridiculously high NOTE_LOWAT, thereby suppressing all READ events.

, PPL REPL:

Python 2.6.5 (r265:79063, Jul 17 2010, 22:57:01) 
[GCC 4.2.1 20070719  [FreeBSD]] on freebsd8
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> import socket
>>> import sys
>>> a, b = socket.socketpair()
>>> kq = select.kqueue()
>>> kq.control([select.kevent(a, select.KQ_FILTER_READ, select.KQ_EV_ADD, select.KQ_NOTE_LOWAT, sys.maxint)], 0)
[]
>>> b.send('abc')
3
>>> kq.control(None, 10) # Interrupt after some time.
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>> b.close()
>>> kq.control(None, 10) # Immediate return.
[<select.kevent ident=3 filter=-1 flags=0x8000 fflags=0x0 data=0x3 udata=0x0>]
>>> 

, FreeBSD , man 2 select:

- .

+1

Source: https://habr.com/ru/post/1757754/


All Articles