From 7c40b02004a8d4bb5c7aa14fc32b32318d24fd5a Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Fri, 23 Jul 2021 08:21:48 +0900 Subject: [PATCH] include/inst: Make subsequent calls to inst_running() return false When an instance is instructed to stop, a semaphore is increased to signal this condition. The inst_running() function uses sem_trywait() on this semaphore, returning false if the semaphore could be passed, thus telling the caller that the instance should be stopped. However, since sem_trywait() decreases the semaphore, subsequent calls to inst_running() will return true again. This commit changes inst_running() to increase the semaphore in case it was decreased by sem_trywait(). However, there remains a small window of opportunity where the function may return different values to different callers if multiple processes from the same instance call the function at the same time. --- include/inst.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/inst.sh b/include/inst.sh index 3be890b..677bb36 100644 --- a/include/inst.sh +++ b/include/inst.sh @@ -147,6 +147,12 @@ inst_running() { return 0 fi + # The next invocation of inst_running() will return true if we don't + # increase the semaphore here, since sem_trywait() decreased it. + if ! sem_post "$__inst_sem"; then + log_warn "Could not post semaphore $__inst_sem" + fi + return 1 } -- 2.47.3