Skip to contentSkip to conversation

io: Allow socket error to be retrieved on disconnect

PATCH Stotland 2 participants 4 messages Oct 13, 2020 – Oct 15, 2020 1 message not in the archive

lore
Share & tools
lore

Stotland

#1 lore raw
In reply to a message not in the archive

Hi Denis,

On Tue, 2020-10-13 at 13:18 -0500, Denis Kenzior wrote:

Hi Inga,

On 10/12/20 5:13 PM, Inga Stotland wrote:

When encountering an error condition, first invoke disconnect handler

if it is set, and only then clear/remove the watch.

This allows the client to retrieve the the actual code for the socket

error (via getsockopt()). Otherwise, if the socket is closed

prior to invoking disconnect handler, the error code is lost.

---

  ell/io.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
ell/io.c +0 -0

index 1fa5f0b..169dfb2 100644

--- a/ell/io.c

+++ b/ell/io.c

@@ -102,8 +102,9 @@ static void io_callback(int fd, uint32_t events, void *user_data)

        if (unlikely(events & (EPOLLERR | EPOLLHUP))) {

                l_util_debug(io->debug_handler, io->debug_data,

                                                "disconnect event <%p>", io);

- watch_remove(io->fd, !io->close_on_destroy);

+

                io_disconnected(io);

+ watch_remove(io->fd, !io->close_on_destroy);

So you want to obtain ECONNREFUSED from the disconnect handler and not the read

handler like glib?

Another potential issue is that EPOLLHUP might come in (when TCP socket is EOFed

for example) and there might still be data left in the socket buffer. So

strictly speaking the read watcher can continue existing, but the write watcher

is no longer settable. I think we sort of ignored this case since we have not

had to deal with connection oriented sockets...

It is my understanding that in glib, a typical write watch is associated with EPOLLOUT | EPOLLERR
and a typical read watch is associated with EPOLLOUT | EPOLLERR |EPOLLHUP

I am not sure we have to follow glib to the letter :), esp since ELL has read/write and disconnect handlers.

Currently, read and write handlers are invoked only if there's no error condition encountered.
I assumed that this is a design choice in ELL to simplify client's life: no need to check for error condition
every time read/write handler is invoked.

Also, as the code is implemented currently, an error condition triggers the io closure procedure and we never get to read/write handlers. As you mentioned, we should at least allow EPOLLIN to be processed...

So the choices as I can see so far:

1) If EPOLLIN or EPOLLOUT are raised, allow calling read/write handlers even is an error condition is encountered. This puts a burden on client implementation to check for potential errors and may not be very compatible with the ell-based code that is already out there.

2) Keep the logic for read/write handlers, but allow error retrieval in disconnect handler if it is set (basically, don't close the socket until disconnect handler is called).

3) Any suggestions?

BTW, the actual case I am looking at from the user perspective is a situation when the pollfalgs are EPOLLOUT|EPOLLHUP|EPOLLERR When using GLib, this triggers the write watch with the handler checking for the error condition.

                return;

        }

@@ -123,8 +124,8 @@ static void io_callback(int fd, uint32_t events, void *user_data)

                        if (watch_modify(io->fd, io->events, false) == -EBADF) {

So this condition is hit when fd was close()-d by the read/write callback...

                                io->close_on_destroy = false;

- watch_clear(io->fd);

                                io_disconnected(io);

+ watch_clear(io->fd);

Since in patch 1 your intent is not to invoke the disconnect callback when the

client invokes io_destroy, should we treat this part the same way?

Note that since the fd is already closed, I'm not sure invoking io_disconnected

prior to watch_clear makes sense anyway. The fd is gone and you'll just get a

-EBADFD. Better to have io_get_fd return -1.

Agree

                                return;

                        }

                }

@@ -146,8 +147,8 @@ static void io_callback(int fd, uint32_t events, void *user_data)

                        if (watch_modify(io->fd, io->events, false) == -EBADF) {

                                io->close_on_destroy = false;

- watch_clear(io->fd);

                                io_disconnected(io);

+ watch_clear(io->fd);

As above

                                return;

                        }

                }

Regards,

-Denis

#2 lore raw

Hi Inga,

<snip>

Quoted 9 lines
Quoted 8 lines

So you want to obtain ECONNREFUSED from the disconnect handler and not the read handler like glib?

Another potential issue is that EPOLLHUP might come in (when TCP socket is EOFed for example) and there might still be data left in the socket buffer. So strictly speaking the read watcher can continue existing, but the write watcher is no longer settable. I think we sort of ignored this case since we have not had to deal with connection oriented sockets...

Quoted 4 lines

It is my understanding that in glib, a typical  write watch is associated with EPOLLOUT | EPOLLERR
 and a typical read watch is associated with EPOLLOUT | EPOLLERR |EPOLLHUP

Right. Probably to catch the HUP.

Quoted 2 lines

I am not sure we have to follow glib to the letter :), esp since ELL has read/write and disconnect handlers.

That's fine with me. Just pointing out the possibility that this can happen. I think by custom we try to drain all data from a socket in a read_watch callback.
But this might not always be possible.

Quoted 6 lines

Currently, read and write handlers are invoked only if there's no error condition encountered.
 I assumed that this is a design choice in ELL to simplify client's life: no
need to check for error condition
every time read/write handler is invoked.

Right.

Quoted 4 lines

Also, as the code is implemented currently, an error condition triggers the io closure procedure and we never get to read/write handlers.
As you mentioned, we should at least allow EPOLLIN to be processed...

Right. I think in theory it is possible to have both EPOLLIN and EPOLLHUP set.

Quoted 8 lines

So the choices as I can see so far:

1) If EPOLLIN or EPOLLOUT are raised, allow calling read/write handlers even is an error condition is encountered. This puts a burden on client implementation to check for potential errors and may not be very compatible with the ell-based code that is already out there.

Quoted 4 lines

2) Keep the logic for read/write handlers, but allow error retrieval in disconnect handler if it is set (basically, don't close the socket until disconnect handler is called).
3) Any suggestions?

We could do a combination of 1 & 2: detect EPOLLIN|EPOLLHUP and call the read callback first. Not sure whether any other combination of EPOLLHUP might happen, but they must be rare. Then call the disconnect handler and let the client deal with obtaining error conditions if they care to.

Quoted 6 lines

BTW, the actual case I am looking at from the user perspective is a situation when the pollfalgs are EPOLLOUT|EPOLLHUP|EPOLLERR
When using GLib, this triggers the write watch with the handler checking for the error condition.

Curious. How does that even make sense? EPOLLHUP should mean the other side closed the socket, so writing to it is rather useless... I would think EPOLLOUT shouldn't even be set in this case.

Regards,
-Denis

#3 lore raw

Hi Denis,
On Tue, 2020-10-13 at 17:25 -0500, Denis Kenzior wrote:
<snip>

So the choices as I can see so far:

1) If EPOLLIN or EPOLLOUT are raised, allow calling read/write handlers even is

an error condition is encountered. This puts a burden on client

implementation to check for potential errors and may not be very compatible with

the ell-based code that is already out there.

2) Keep the logic for read/write handlers, but allow error retrieval in

disconnect handler if it is set (basically, don't close the socket until

disconnect handler is called).

3) Any suggestions?

We could do a combination of 1 & 2: detect EPOLLIN|EPOLLHUP and call the read

callback first. Not sure whether any other combination of EPOLLHUP might

happen, but they must be rare. Then call the disconnect handler and let the

client deal with obtaining error conditions if they care to.

Sounds good.

Checking EPOLLIN first and , if set, calling read handler unconditionally is easy enough.

There is one hairy issue with calling disconnect prior to removing or clearing a watch:
Since ELL cannot prevent a client from calling l_io_destroy from a disconnect handler, this may cause an unpredictable behavior in subsequent call to watch remove/clear.
If we preserve fd # locally prior to calling disconnect, this still does not address the issue 100%: the same clinet that called l_io_destroy from the disconnect handler may call l_io_new and a new fd may end up with the # that was just "released". And then we will call watch_remove()...

The only way I see around this is to add a strong warning comment to l_io_destroy() that states that calling l_io_destroy directly from a disconnect handler should be done only by using idle_oneshot construct, otherwise, the behavior is not guaranteed. What are your thoughts on this?

BTW, the actual case I am looking at from the user perspective is a situation

when the pollfalgs are EPOLLOUT|EPOLLHUP|EPOLLERR

When using GLib, this triggers the write watch with the handler checking for the

error condition.

Curious. How does that even make sense? EPOLLHUP should mean the other side

closed the socket, so writing to it is rather useless... I would think EPOLLOUT

shouldn't even be set in this case.

I encountered this particular combination with a number of bluez tests (e.g., creating a connection with invalid parameters).

Probably has to do with sequence in which the epoll flags are set in those cases...

Regards,

Inga

#4 lore raw

Hi Inga,

Quoted 24 lines · Inga

Hi Denis,
On Tue, 2020-10-13 at 17:25 -0500, Denis Kenzior wrote:
<snip>

Quoted 14 lines
Quoted 9 lines

So the choices as I can see so far:

1) If EPOLLIN or EPOLLOUT are raised, allow calling read/write handlers even is an error condition is encountered. This puts a burden on client implementation to check for potential errors and may not be very compatible with the ell-based code that is already out there.

Quoted 4 lines

2) Keep the logic for read/write handlers, but allow error retrieval in disconnect handler if it is set (basically, don't close the socket until disconnect handler is called).
3) Any suggestions?

Quoted 4 lines

We could do a combination of 1 & 2: detect EPOLLIN|EPOLLHUP and call the read callback first. Not sure whether any other combination of EPOLLHUP might happen, but they must be rare. Then call the disconnect handler and let the client deal with obtaining error conditions if they care to.

Sounds good.

Quoted 8 lines

Checking EPOLLIN first and , if set, calling read handler unconditionally is easy enough.

There is one hairy issue with calling disconnect prior to removing or clearing a watch:
Since ELL cannot prevent a client from calling l_io_destroy from a disconnect handler, this may cause an unpredictable behavior  in subsequent call to watch remove/clear.

We've resorted to using a flag marking the object as destroyed for cases like this. See iwd.git/src/watchlist.h for an example. It is then used to unwind the callback stack and clean up gracefully. I think we tried to avoid doing this in io.c, but maybe we can't...

Quoted 5 lines

If we preserve fd # locally prior to calling disconnect, this still does not address the issue 100%: the same clinet that called l_io_destroy from the disconnect handler may call l_io_new and a new fd may end up with the # that was just "released".
And then we will call watch_remove()...

Yes, but there's nothing we can do about that.

Quoted 6 lines

The only way I see around this is to add a strong warning comment to l_io_destroy() that states that calling l_io_destroy directly from a disconnect handler should be done only by using idle_oneshot construct, otherwise, the behavior is not guaranteed. What are your thoughts on this?

I think we can work around the l_io_destroy issue, so I rather not resort to requiring l_idle_add. But, l_idle_add is a valid strategy. I think it is a good idea to make the user aware of this strategy in the documentation.

Regards,
-Denis