[bug] The agent may be stopped and started while the configuration is applied
In some cases it may happen that the agent is stopped and started again while the configuration is being applied.
If this happens, the configuration or config status of the device may remain in an inconsistent state (either the configuration may not be fully applied or it may not report that it has been applied to the OpenWISP server).
I am not able to replicate right now the steps which cause the agent to stop and start itself automatically.
Something like this was happening for the "reload" which we are already handling.
If we can confirm this issue, we could introduce a mechanism which delays the stop of the agent until the configuration has been applied, like we are doing in the reload:
https://github.com/openwisp/openwisp-config/blob/6ffbe537d2a4ccf1359da9235319e3a62e9df294/openwisp-config/files/openwisp.init#L89-L102
We can turn that code into a function and reuse in stop_service too.