This is an automated email from the ASF dual-hosted git repository. rnewson pushed a commit to branch couch_file-perf-regression-fix in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 304485f3e6505cb0ee37d568de10d1a54278c210 Author: Robert Newson <[email protected]> AuthorDate: Thu Sep 10 17:28:03 2026 +0100 add optional eof validation in couch_file --- src/couch/src/couch_file.erl | 16 ++++++++++++++++ src/couch/test/eunit/couch_file_tests.erl | 26 +++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl index 2d0920f7e..050e5234d 100644 --- a/src/couch/src/couch_file.erl +++ b/src/couch/src/couch_file.erl @@ -625,6 +625,7 @@ eof(#file{fd = Fd}) -> file:position(Fd, eof). append_bins(#file{fd = Fd, eof = Pos} = File, Bins) -> + validate_eof(File, "append_bin_eof_check"), {BlockResps, FinalPos} = lists:mapfoldl( fun(Bin, PosAcc) -> Blocks = make_blocks(PosAcc rem ?SIZE_BLOCK, Bin), @@ -771,6 +772,7 @@ find_newest_header(Fd, [{Location, Size} | LocationSizes]) -> end. handle_write_header(Bin, #file{fd = Fd, eof = Pos} = File) -> + validate_eof(File, "header_eof_check"), BinSize = byte_size(Bin), case Pos rem ?SIZE_BLOCK of 0 -> Padding = <<>>; @@ -1026,6 +1028,20 @@ dup(#file{fd = Fd} = File) -> File end. +validate_eof(#file{} = File, Key) when is_list(Key) -> + case config:get_boolean("couch_file", Key, false) of + false -> + ok; + true -> + {ok, ActualPos} = file:position(File#file.fd, eof), + if + File#file.eof /= ActualPos -> + throw_stop({eof_mismatch, ActualPos, File#file.eof}, File); + true -> + ok + end + end. + -ifdef(TEST). -include_lib("couch/include/couch_eunit.hrl"). diff --git a/src/couch/test/eunit/couch_file_tests.erl b/src/couch/test/eunit/couch_file_tests.erl index 4887625a5..067e43255 100644 --- a/src/couch/test/eunit/couch_file_tests.erl +++ b/src/couch/test/eunit/couch_file_tests.erl @@ -21,6 +21,8 @@ setup() -> {ok, Fd} = couch_file:open(?tempfile(), [create, overwrite]), meck:new(file, [passthrough, unstick]), + config:set_boolean("couch_file", "append_bin_eof_check", true), + config:set_boolean("couch_file", "header_eof_check", true), Fd. teardown(Fd) -> @@ -157,7 +159,9 @@ read_write_test_() -> ?TDEF_FE(should_close_on_idle), ?TDEF_FE(should_crash_on_unexpected_cast), ?TDEF_FE(should_handle_pread_iolist_upgrade_clause), - ?TDEF_FE(should_handle_append_bin_upgrade_clause) + ?TDEF_FE(should_handle_append_bin_upgrade_clause), + ?TDEF_FE(should_detect_eof_mismatch_append_bin), + ?TDEF_FE(should_detect_eof_mismatch_header) ] } } @@ -401,6 +405,26 @@ should_handle_append_bin_upgrade_clause(Fd) -> ?assertEqual(Pos2, Len1), ?assertEqual({ok, Bin}, couch_file:pread_binary(Fd, Pos2)). +should_detect_eof_mismatch_append_bin(_) -> + Filepath = ?tempfile(), + {ok, Fd} = couch_file:open(Filepath, [create, overwrite]), + {ok, Io} = file:open(Filepath, [append, binary]), + file:write(Io, ~"0000"), + file:close(Io), + unlink(Fd), + ?assertEqual({error, {eof_mismatch, 4, 0}}, couch_file:append_binary(Fd, ~"1111")), + catch couch_file:close(Fd). + +should_detect_eof_mismatch_header(_) -> + Filepath = ?tempfile(), + {ok, Fd} = couch_file:open(Filepath, [create, overwrite]), + {ok, Io} = file:open(Filepath, [append, binary]), + file:write(Io, ~"0000"), + file:close(Io), + unlink(Fd), + ?assertEqual({error, {eof_mismatch, 4, 0}}, couch_file:write_header(Fd, ~"1111")), + catch couch_file:close(Fd). + header_test_() -> { "File header read/write tests",
