]> git.corax.cc Git - toolbox/commitdiff
include/inst: Make subsequent calls to inst_running() return false
authorMatthias Kruk <m@m10k.eu>
Thu, 22 Jul 2021 23:21:48 +0000 (08:21 +0900)
committerMatthias Kruk <m@m10k.eu>
Thu, 22 Jul 2021 23:36:15 +0000 (08:36 +0900)
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

index 3be890b1b75638e7857ffbf76033699dca997efb..677bb36015807bfe921c9941be4be3f7f2e88d5d 100644 (file)
@@ -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
 }