Fix Nginx cache handling for caller-owned locks

This commit is contained in:
Ilia Ross
2026-09-18 18:13:47 +02:00
parent afb0929dd9
commit a8a1b70015
2 changed files with 26 additions and 1 deletions

View File

@@ -562,13 +562,14 @@ while (1) {
}
elsif (&lock_file($f)) {
push(@lock_all_config_files_cache, $f);
# Drop stale lines only after acquiring this lock ourselves.
&unflush_file_lines($f);
}
else {
&error("Failed to lock Nginx config file $f");
}
$locked{$f} = 1;
$new_files++;
&unflush_file_lines($f);
}
last if (!$new_files);

View File

@@ -142,8 +142,32 @@ subtest 'locks acquired by a caller remain owned by it' => sub {
main::unlock_file($conf);
};
subtest 'pending edits under a caller lock are retained' => sub {
write_text($conf, "http {\n}\n");
main::flush_config_cache();
main::unflush_file_lines($conf);
main::lock_file($conf);
my $lines = main::read_file_lines($conf);
splice(@$lines, 1, 0, ' # pending edit');
main::lock_all_config_files();
is(main::read_file_lines($conf), $lines,
'pre-existing writable cache is retained');
main::flush_file_lines($conf);
main::unlock_all_config_files();
main::unlock_file($conf);
open(my $fh, '<', $conf) or die "$conf: $!";
my $saved = do { local $/; <$fh> };
close($fh) or die "$conf: $!";
like($saved, qr/^ # pending edit$/m, 'pending edit is saved');
};
subtest 'included files are refreshed after waiting for their locks' => sub {
my $extra = "$tmp/extra.conf";
write_text($conf, "http {\n include $included;\n}\n");
write_text($included, servers());
main::flush_config_cache();
main::unflush_file_lines($conf);
main::unflush_file_lines($included);
write_text($extra, "server {\n server_name gamma.invalid;\n}\n");
my $lock = \&main::lock_file;
my $changed = 0;