EVFILT_WRITE returning twice

103 views Asked by At

I'm trying to send a buffer to my socket client when the file descriptor is available for writing.

EV_SET is set to: EVFILT_WRITE, EV_ADD | EV_DISABLE | EV_CLEAR

then when changed to EVFILT_WRITE, EV_ENABLE then EVFILT_WRITE get triggered once which is great!

but if i use the function write or send when i get EVFILT_WRITE like this:

if (e->filter == EVFILT_WRITE)
send(socket, buff, strlen(buff), 0);

then i get again another EVFILT_WRITE event. It seems like the send function trigger another EVFILT_WRITE event. is that expected behaviour? I thought EVFILT_WRITE triggers only when the file descriptor is available for writing.

I searched for the issue, but it looks like nobody mention that. Can someone please confirm if that is expected behaviour and why?

1

There are 1 answers

2
arrowd On

This is how I understand it:

Since you used EV_CLEAR, the kevent facility starts to return state transitions, not the current state. So, whenever you touch the socket descriptor with kevent() or send() calls, you get an EVFILT_WRITE event back.

Another way to look at this:

When send() is called the descriptor becomes unavailable for writing for a moment and then again becomes available, which is why you get an event.

I'll try to loop in some knowledgeable people to this question.