libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] Using MHD_stop_daemon with signal() or elsewhere?


From: Gavin Henry
Subject: Re: [libmicrohttpd] Using MHD_stop_daemon with signal() or elsewhere?
Date: Tue, 25 Jan 2022 14:43:39 +0000

> On 24.01.2022 22:16, Gavin Henry wrote:
> >> Using graceful closure is one of the vital thing for correct
> >> implementation of any application.
> >
> > Understood.
> >
> >> Of course you *must* call MHD_stop_daemon() as it stops MHD threads,
> >> free memory, closes sockets, and does other cleanups.
> >
> > Thanks. I understand closing the sockets, but the OS/kernel will do
> > all the rest no?
>
> Actually OS can close the sockets as well, but it will do that in
> abortive way (RST will be sent).

True.

> You may rely on OS that resources most probably be freed if application
> was closed because of problems (see MHD_PANIC()), but you must not
> expect that OS will do a proper cleanup after you every time.
> First of all, automatic cleanup is not completely guaranteed. OS may
> miss something. It's much better to do it correctly.
> Secondly, if you close your app without proper cleanup, you would have
> no control on the order of cleanup/closure/free. OS may deallocate
> resources that are still in use, which may result in memory or even file
> corruption.
> And the last but not least, not cleaning at exit is a resource leak. It
> would be harder to detect other leaks, when you have a large leak "by
> design".

Thanks for your advice Evgeny. Very much appreciated.

> > I need mhd always on until the application quits. What happens after
> > that the OS/kernel will sort.
>
> If you call MHD_stop_daemon() before closing you application, MHD will
> be closed a few nanosecond earlier. Do you think it will cause any kind
> of problems for your application?

Nah, I'm just trying to figure out the best way to call it as it will
only stop when
the application stops. That's usually handled in a signal handler.

I'll look at other projects that use MHD.

> >
> >> Not calling this function is like allocating memory without deallocating.
> >
> > I don't think that's the same at all. MHD doesn't perform badly nor
> > does my application leak memory if I don't call MHD_stop_daemon(). It
> > restarts just fine.
>
> Actually it is exactly the same. Not calling MHD_stop_daemon() would
> cause no deallocation of already allocated memory, no closure of already
> open socket etc.
> Another analogy: you may power off your PC by just pulling the power
> plug, without shutting down of the OS. It may work and PC may reboot
> just fine. But there is not guarantee that PC will reboot correctly and
> you will not lose something important.

I need to stay away from SO forums :-)

> >> If your application is designed to work as systemd daemon, you must
> >> handle SIGTERM signal to properly close/free all resources before final
> >> exit.
> > Is there some guidance on that systemd requirement? Will Google.
>
> Yes, please search. This mailing list is for discussion of MHD.

Understood.



--
Kind Regards,

Gavin Henry.
https://sentrypeer.org



reply via email to

[Prev in Thread] Current Thread [Next in Thread]