[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [libmicrohttpd] Using MHD_stop_daemon with signal() or elsewhere?
From: |
Evgeny Grin |
Subject: |
Re: [libmicrohttpd] Using MHD_stop_daemon with signal() or elsewhere? |
Date: |
Tue, 25 Jan 2022 10:26:41 +0300 |
User-agent: |
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 |
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).
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".
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?
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.
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.
--
Evgeny
OpenPGP_signature
Description: OpenPGP digital signature