[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [libmicrohttpd] Dropping shutdown() on the listening socket from MHD
From: |
Christian Grothoff |
Subject: |
Re: [libmicrohttpd] Dropping shutdown() on the listening socket from MHD_stop_daemon()? |
Date: |
Tue, 22 Sep 2015 19:23:28 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.7.0 |
Hi Lennart!
Nice to see you here. The reason why we call 'shutdown' on the listen
FD was (IIRC) that this is one way to signal a thread that is
select()ing on the FD (on Linux-only). This way, we don't have to use
an extra pipe (or event-FD). So if MHD runs in thread-pool mode with 4
threads and without a signaling pipe, this shutdown() on the listen
socket is how we terminate the threads.
Now, this does not work on some platforms (FreeBSD, no clue about
W32/zOS/etc.), so we do also _allow_ the use of a pipe, and there is an
option to "force" its use on Linux: MHD_USE_PIPE_FOR_SHUTDOWN.
If that option is set, there is no real _need_ for MHD to call shutdown
on the listen socket, and that would enable this use with systemd.
We could also introduce a more explicit flag (i.e.
MHD_NO_LISTEN_SHUTDOWN), but I think just modifying the code to check
for MHD_USE_PIPE_FOR_SHUTDOWN and then documenting (and thereby
guaranteeing) that this will disable shutdown() on the listen socket is
the simplest option.
I've implemented this in SVN 36369, diff attached.
Note that you can already force MHD_USE_PIPE_FOR_SHUTDOWN with 'old'
code by (somehow) making configure not #define HAVE_LISTEN_SHUTDOWN,
i.e. by appending #undef HAVE_LISTEN_SHUTDOWN to MHD_config.h after
configure runs. But that's an ugly hack for *static* builds of the lib.
There is another good method to do this with existing code, which is to
call MHD_quiesce_daemon() before MHD_stop_daemon().
MHD_quiesce_daemon() returns you the listen FD and _removes_ it from
MHD's control, so it will not call shutdown() on it anymore. Using
MHD_quiesce_daemon() also requires you to set MHD_USE_PIPE_FOR_SHUTDOWN.
So with old code you can do:
mhd = MHD_start_daemon (..., MHD_USE_PIPE_FOR_SHUTDOWN, ...)
// normal app
#if MHD_VERSION <= 0x00094300
close (MHD_quiesce_daemon(mhd)); // closing was now your responsibility!
#endif
MHD_stop_daemon (mhd); // will no longer shutdown().
So with the patch, you can save the line in the middle...
Happy hacking!
Christian
On 09/22/2015 06:03 PM, Lennart Poettering wrote:
> Heya,
>
> we are using libmicrohttpd for two mini daemons in systemd, and we use
> it with socket activation, so that the daemons do not have to run
> unless requested. There's a certain problem though: libmicrohttpd when
> shutting down invokes shutdown() on the listening fd. This has the
> effect that the kernel really shuts it down and it will not accept new
> connections anymore. When using socket activation we really would like
> it to continue to listen, so that we can start it again should one day
> come another connection.
>
> It's OK to invoke shutdown() on connection sockets, but I can't see a
> reason why you would want to invoke it on the listening socket. Hence,
> could this invocation of shutdown() be removed from MHD_stop_daemon()?
> Is there any reason it is in there in the first place?
>
> Thanks,
>
> Lennart
>
signature.asc
Description: OpenPGP digital signature