Procd shutdown issues/questions

I have a simple procd service that needs to cleanly close on shutdown. Currently the service intercepts a number of kill signals (SIGHUP, SIGQUIT, SIGSTOP, SIGTERM, SIGINT). When one of these signals is sent to the process, it cleans up and exits properly. For testing purposes, the service waits 10 seconds and then exits.

There is documentation on this, but it does not seem correct to my interpretations: (https://openwrt.org/docs/guide-developer/procd-init-scripts)

The term_timeout parameter seems to indicate that a SIGTERM signal is sent and procd waits for the process to terminate up to the timeout value. However, that interpretation is apparently wrong because /etc/init.d/myservice stop returns immediately even if the process has not terminated yet. I don't know what termination signal is being sent, but my service is not catching it.

My next thought was to simply block in service_stop() until the process terminates. The documentation states that stop_service is called after procd terminates the service. However, that is not what I am seeing. If I put a 20 second sleep in stop_service and then call /etc/init.d/myservice stop I can see the process remains during the 20 second sleep and then is killed after stop_service returns.

Am I misinterpreting things? What signal is procd using to terminate a service? Is there an easy way to block for a process to cleanly terminate? I suppose I could create a dummy service that shuts down after the real service and inside that service_stop block for the real service to terminate, but that is a total hack.

Thanks for any insights.

Correct. Procd waits term_timeouts seconds for the process to exit before delivering a KILL signal.

/etc/init.d/x stop is asynchronous and merely submits the service termination request to procd, it does not block until the process is stopped.

By default, SIGTERM is sent. If you service is not receiving it, make sure it is properly supervised by procd. A common mistake is to fork away upon service start, which will cause procd to lose track of the process.

stop_service() is called before the termination request is sent to procd, its purpose is to allow for custom preparatory work before initiating the kill.

2 Likes

Thank you! This is very helpful. I will update the docs.

So then upon device shutdown, the shutdown process will not wait for service shutdown to complete. Is that correct? Or is it simply that /etc/init.d/x stop does not block?

Correct, /etc/init.d/x stop does not block.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.