On 12/1/26 16:37, Patrick Steinhardt wrote:
> On Tue, Dec 01, 2026 at 02:17:48PM +0530, Kaartic Sivaraam wrote:
>> On 12/1/26 12:52, Patrick Steinhardt wrote:
>>>> + >>>> +acquire_expensive_lock_ () { >>>> + local owner waited= tmp="$expensive_lock_.$$" misses=0 >>>> + mkdir -p "$TEST_RESULTS_DIR" || >>>> + { expensive_lock_unavailable_; return 0; } >>>> + # The lock is a hard link to a file that already holds our PID, so >>>> + # it never exists without its owner recorded in it. Remove any >>>> + # $tmp left by a dead process with our PID first: it may still be >>>> + # linked to that process's lock, which writing to it would empty. >>>> + rm -f "$tmp"
>>> >>> Nit: it is expected that in most cases this won't do anything, so in the >>> expected common case we'll see an error message. Do we want to silence >>> this? >>>
>> >> I guess that was mostly done to keep things consistent as we pretty much >> silence everything in this function. So, I tend to prefer keeping the '-f' >> on this one too.
> > Oh, the "-f" should be kept indeed. But shouldn't it be `2>/dev/null`? > > Ah, no, rm(1) doesn't actually complain in case the target path does not > exist. I thought it did. One week off and I've already forgotten > everything. >
I guess you're just warming up from the time off ;-)
>>> I feel like re-checking the lock owner is kind of pointless here. The >>> check doesn't fix the race, but it suggests it to the reader. And that >>> is overall fine, because the intent now is to fail in a way that we >>> prefer to run the test instead of blocking forever. >>> >>> But it raises the question why we even bother to check again. To me it >>> only causes puzzlement. >>>
>> >> The re-check is present in order to prevent a specific race scenario pointed >> out by Junio: >>
>>> * T1 holds $expensive_lock_, finishes its last EXPENSIVE test, >>> calls release_expensive_lock_ (rm -rf "$expensive_lock_"), and >>> exits. >>> >>> * Waiter T2 read owner=$(cat "$expensive_lock_/pid" 2>/dev/null) >>> (obtaining T1's PID) just before T1 removed the directory. >>> >>> * Waiter T3 runs mkdir "$expensive_lock_", succeeds, writes T3's >>> PID to pid, and begins its EXPENSIVE test. >>> >>> * T2 now checks test -n "$owner" && ! kill -0 "$owner" 2>/dev/null >>> against T1's exited PID, finds that T1 is gone, runs rm -rf >>> "$expensive_lock_", which deletes T3's active lock. It loops, >>> succeeds on mkdir "$expensive_lock_", and runs its EXPENSIVE test >>> concurrently with T3.
>> >> >> It talks about the variant that created directories but it applies for the >> current linking variant also. >> >> If we could improve the comment above the relevant code in someway, do let >> me know. It tries to convey it but I unfortunately couldn't get what detail >> it is missing.
> > But the race isn't fully fixed, is it? You have simply narrowed the race > window, but the race itself still exists between `test $(cat > "$expensive_lock_" = "$owner")` and the `rm -f`, doesn't it? >
Ah. That is true indeed. Though, without the re-check, makes overlaps after a SIGKILL more common: in a small stress test they went from about 3 per run to about 15. So, I kind of lean towards keeping it since it doesn't cost us much.
-- Sivaraam