service icon indicating copy to clipboard operation
service copied to clipboard

macos: LaunchDaemon Restart implementation should perhaps use kickstart

Open emidoots opened this issue 3 years ago • 1 comments

Right now the macOS Restart implementation is done in terms of stopping and starting the service:

https://github.com/kardianos/service/blob/3596dfaf340d19b3f4d3c1029517a623367f65d5/service_darwin.go#L258-L265

Start and Stop actually load and unload the launch daemon plist, which means that if the program crashes for any reason (e.g. another goroutine panics or something) then the daemon would be unloaded and would not start again on future system restarts for example.

I encountered this doing something a bit weird: having my service Restart itself. This works on Windows/Linux, but not macOS due to how Restart is implemented (this was easy enough to workaround by just having my process exit though.)

I believe a better implementation may be to use kickstart with the -k flag as described here: https://serverfault.com/a/947469

emidoots avatar Dec 27 '22 02:12 emidoots

Hello. I've tripped over this as well.

Another problem with this implementation is that user probably expects that in case service is currently stopped, Start() and Restart() will have identical behavior (true for systemd, for example). This implementation fails in this case, because Stopping() a service that is not running results in error, and Start() is not called.

BR, ympek

ympek avatar May 31 '23 20:05 ympek