0
votes

I create epoll and register some non-blocking sockets which try connect to closed ports on localhost. Why epoll tells me, that i can write to this socket (it give event for one of created socket with eventmask contain EPOLLOUT)? But this socket doesn't open and if i try send something to it i get an error Connection refused.

Another question - what does mean even EPOLLHUP? I thought that this is event for refused connection. But how in this case event can have simultaneously EPOLLHUP and EPOLLOUT events?

Sample code on Python:

import socket
import select

poll = select.epoll()
fd_to_sock = {}
for i in range(1, 3):
    s = socket.socket()
    s.setblocking(0)
    s.connect_ex(('localhost', i))
    poll.register(s, select.EPOLLOUT)
    fd_to_sock[s.fileno()] = s

print(poll.poll(0.1))
# prints '[(4, 28), (5, 28)]'
2

2 Answers

1
votes

All that poll guarantees is that your application won't block after calling corresponding function. So you are getting what you've paid for - you can now rest assured writing to this socket won't block - and it didn't block, did it?

Poll never guarantees that corresponding operation will succeed.

1
votes

poll/select/epoll return when the file descriptor is "ready" but that just means that the operation will not block (not that you will necessarily be able to write to it successfully).

Likewise for EPOLLIN: for example, it will return ready when a socket is closed; in that case, you won't actually be able to read data from it.

EPOLLHUP means that there was a "hang up" on the connection. That would really only occur once you actually had a connection. Also, the documentation (http://linux.die.net/man/2/epoll_ctl) says that you don't need to include it anyway:

EPOLLHUP Hang up happened on the associated file descriptor. epoll_wait(2) will always wait for this event; it is not necessary to set it in events.