switch-to-configuration-ng: don't block when the lockfile is already locked (#382797)

authored by Ramses and committed by GitHub 9abef1ce 83377f82

+19 -2
+1 -1
nixos/modules/system/activation/switch-to-configuration.pl
··· 95 96 make_path("/run/nixos", { mode => oct(755) }); 97 open(my $stc_lock, '>>', '/run/nixos/switch-to-configuration.lock') or die "Could not open lock - $!"; 98 - flock($stc_lock, LOCK_EX) or die "Could not acquire lock - $!"; 99 openlog("nixos", "", LOG_USER); 100 101 # run pre-switch checks
··· 95 96 make_path("/run/nixos", { mode => oct(755) }); 97 open(my $stc_lock, '>>', '/run/nixos/switch-to-configuration.lock') or die "Could not open lock - $!"; 98 + flock($stc_lock, LOCK_EX|LOCK_NB) or die "Could not acquire lock - $!"; 99 openlog("nixos", "", LOG_USER); 100 101 # run pre-switch checks
+17
nixos/tests/switch-test.nix
··· 732 out = switch_to_specialisation("${machine}", "modifiedSystemConf") 733 assert_contains(out, "starting the following units: ${dbusService}\n") 734 735 with subtest("fstab mounts"): 736 switch_to_specialisation("${machine}", "") 737 # add a mountpoint
··· 732 out = switch_to_specialisation("${machine}", "modifiedSystemConf") 733 assert_contains(out, "starting the following units: ${dbusService}\n") 734 735 + with subtest("aborts on already locked lock file"): 736 + (exitcode, _) = machine.execute( 737 + 'flock -x --nb /run/nixos/switch-to-configuration.lock -c "${otherSystem}/bin/switch-to-configuration test"', 738 + timeout=5 739 + ) 740 + # See man timeout, exit codes above 124 come from the timeout command 741 + # We want to make sure that stc actually exited with an error code, 742 + # if instead we hit the timeout, then it means that stc hangs, which is 743 + # what we don't want 744 + # TODO: We cannot match on the exact exit code since it's not consistent between 745 + # stc and stc-ng, since errno/last_os_error is not a very stable interface, 746 + # we should probably get rid of that in stc-ng once we got rid of the 747 + # perl implementation 748 + assert exitcode < 124, \ 749 + "switch-to-configuration did not abort as expected, " + \ 750 + f"probably it timed out instead (exit code: {exitcode}), 124 means timeout" 751 + 752 with subtest("fstab mounts"): 753 switch_to_specialisation("${machine}", "") 754 # add a mountpoint
+1 -1
pkgs/by-name/sw/switch-to-configuration-ng/src/src/main.rs
··· 1037 die(); 1038 }; 1039 1040 - let Ok(_lock) = Flock::lock(lock, FlockArg::LockExclusive) else { 1041 eprintln!("Could not acquire lock"); 1042 die(); 1043 };
··· 1037 die(); 1038 }; 1039 1040 + let Ok(_lock) = Flock::lock(lock, FlockArg::LockExclusiveNonblock) else { 1041 eprintln!("Could not acquire lock"); 1042 die(); 1043 };