[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Proper socket shutdown.



On Sat, Oct 30, 2010 at 11:55 AM, Kelly Brock <Kerby@xxxxxxxxxxx> wrote:
> Hi Nick,
 [...]
>        Just a note before the details.  I would suggest adding a little
> section to the manual covering the proper method of handshaked shutdown.
> It's just a minor thing but figuring out the watermark method is non-obvious
> till you give it some further thought.  (Well, it was to me at the time,
> then again it was 2am. :))

Sure!  Want to take a crack at writing it?  The source is at
http://github.com/nmathewson/libevent-book .  It would probably be
neat to add a series of "Cookbook" chapters.

Instead of doing the watermark trick, I usually have a callback that
checks whether evbuffer_get_length(bufferevent_get_output(bev)) is 0,
does a socket shutdown if it is, but the watermark way is probably...
better?

>        Anywhere, here is a quick and dirty testbed for the problem.
>
> http://pastebin.com/QF4N2DEw
>
>        If you leave "USE_IOCP" set to 0 everything shuts down correctly.
> If you turn that flag on though, the EOF's are never seen.  I just got this
> test case functional so forgive the dirty leaky implementation if you will,
> but please do check that I'm not just making some other stupid error as I'm
> still getting used to 2.0 changes.

Reading the code, just a few (minor) comments that shouldn't affect
anything really:
 * "SD_SEND" is windowsism.  Elsewhere it's either "SHUT_WR," or just "1".
 * When you're writing event handlers, there is no guarantee that
you'll only hit the READING or WRITING flag with the EOF event, or
that you'll only hit expected events, or that you won't hit events in
unexpected combinations  So instead of
    switch(what) {
      case BEV_EVENT_EOF | BEV_EVENT_WRITING:
        ...
    }
 I'd recommend:
     if (what & BEV_EVENT_EOF) {...
     }
     if (what & BEV_EVENT_ERROR) {...
     }

In particular, we should make sure that closing a socket without doing
a proper shutdown does at least trigger *some* event.



As for the main bug here... I'm seeing hints on MSDN that shutdown()
may not work properly in tandem with IOCP sockets, and that
WSASendDisconnect or DisconectEx might be a better choice for those.

-- 
Nick
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.