Comment 8 for bug 1844773

Revision history for this message
Dmitrii Shcherbakov (dmitriis) wrote :

For POSIX I would try using named semaphores on a per-agent basis.

http://man7.org/linux/man-pages/man7/sem_overview.7.html
"Persistence
POSIX named semaphores have kernel persistence: if not removed by sem_unlink(3), a semaphore will exist until the system is shut down."

So, on agent start/restart:

* sem_open;
* sem_trywait -> if EAGAIN is not returned, run the start hook.

I believe if a process exits or crashes without calling sem_post, the semaphore will stay locked forever which would be suitable with sem_trywait. Likewise, we will never call sem_unlink so it will stay there until reboot.

For windows agents there is similar functionality in winapi:
https://docs.microsoft.com/en-us/windows/win32/sync/semaphore-objects?redirectedfrom=MSDN
"Closing the handle does not affect the semaphore count; therefore, be sure to call ReleaseSemaphore before closing the handle or before the process terminates."

Since "stop" is mentioned in the feature request, the semantics that are currently documented are such that application files (packages) and config are to be removed on "stop".

https://discourse.jujucharms.com/t/charm-hooks/1040#heading--stop
"Stop the application
Remove any files/configuration created during the application lifecycle
Prepare any backup(s) of the application that are required for restore purposes."

In other words: stop == uninstall.

So I think it's worth a discussion when it comes to running "stop" before reboot because some applications remove packages and local state when this event is called. Maybe we need a different event for this.