Skip to content

Commit

Permalink
Revalidate but do not update cache when readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Jan 30, 2024
1 parent cb3e5b9 commit 4ea8870
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
16 changes: 11 additions & 5 deletions ext/bootsnap/bootsnap.c
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,12 @@ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler, VALUE args
goto fail_errno;
}
valid_cache = cache_key_equal_slow_path(&current_key, &cached_key, input_data);
if (valid_cache) { // TODO: readonly
if (update_cache_key(&current_key, cache_fd, &errno_provenance)) {
exception_message = path_v;
goto fail_errno;
if (valid_cache) {
if (!readonly) {
if (update_cache_key(&current_key, cache_fd, &errno_provenance)) {
exception_message = path_v;
goto fail_errno;
}
}
bs_instrumentation(sym_revalidated, path_v);
}
Expand Down Expand Up @@ -902,6 +904,10 @@ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler, VALUE args
static VALUE
bs_precompile(char * path, VALUE path_v, char * cache_path, VALUE handler)
{
if (readonly) {
return Qfalse;
}

struct bs_cache_key cached_key, current_key;
int cache_fd = -1, current_fd = -1;
int res, valid_cache = 0, exception_tag = 0;
Expand Down Expand Up @@ -936,7 +942,7 @@ bs_precompile(char * path, VALUE path_v, char * cache_path, VALUE handler)
goto fail;
}
valid_cache = cache_key_equal_slow_path(&current_key, &cached_key, input_data);
if (valid_cache) { // TODO: readonly */
if (valid_cache) {
if (update_cache_key(&current_key, cache_fd, &errno_provenance)) {
goto fail;
}
Expand Down
37 changes: 29 additions & 8 deletions test/compile_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CompileCacheTest < Minitest::Test
def teardown
super
Bootsnap::CompileCache::Native.readonly = false
Bootsnap.instrumentation = nil
end

def test_compile_option_crc32
Expand Down Expand Up @@ -158,6 +159,34 @@ def test_dont_store_cache_after_a_stale_when_readonly
load(path)
end

def test_dont_revalidate_when_readonly
path = Help.set_file("a.rb", "a = a = 3", 100)
load(path)

entries = Dir["#{Bootsnap::CompileCache::ISeq.cache_dir}/**/*"].select { |f| File.file?(f) }
assert_equal 1, entries.size
cache_entry = entries.first
old_cache_content = File.binread(cache_entry)

Bootsnap::CompileCache::Native.readonly = true

output = RubyVM::InstructionSequence.compile_file(path)
Bootsnap::CompileCache::ISeq.expects(:input_to_storage).never
Bootsnap::CompileCache::ISeq.expects(:storage_to_output).once.returns(output)
Bootsnap::CompileCache::ISeq.expects(:input_to_output).never

FileUtils.touch(path, mtime: File.mtime(path) + 50)

calls = []
Bootsnap.instrumentation = ->(event, path) { calls << [event, path] }
load(path)

assert_equal [[:revalidated, "a.rb"]], calls

new_cache_content = File.binread(cache_entry)
assert_equal old_cache_content, new_cache_content, "Cache entry was mutated"
end

def test_invalid_cache_file
path = Help.set_file("a.rb", "a = a = 3", 100)
cp = Help.cache_path("#{@tmp_dir}-iseq", path)
Expand All @@ -177,8 +206,6 @@ def test_instrumentation_hit
load(file_path)

assert_equal [], calls
ensure
Bootsnap.instrumentation = nil
end

def test_instrumentation_miss
Expand All @@ -190,8 +217,6 @@ def test_instrumentation_miss
load(file_path)

assert_equal [[:miss, "a.rb"]], calls
ensure
Bootsnap.instrumentation = nil
end

def test_instrumentation_revalidate
Expand All @@ -205,8 +230,6 @@ def test_instrumentation_revalidate
load(file_path)

assert_equal [[:revalidated, "a.rb"]], calls
ensure
Bootsnap.instrumentation = nil
end

def test_instrumentation_stale
Expand All @@ -220,7 +243,5 @@ def test_instrumentation_stale
load(file_path)

assert_equal [[:stale, "a.rb"]], calls
ensure
Bootsnap.instrumentation = nil
end
end

0 comments on commit 4ea8870

Please sign in to comment.