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 380ae0798e0f309d38d4e6e30220bf07ba02402e
Author: Robert Newson <[email protected]>
AuthorDate: Thu Sep 10 17:28:03 2026 +0100

    add optional eof validation in couch_file
---
 rel/overlay/etc/default.ini               |  4 ++++
 src/couch/priv/stats_descriptions.cfg     |  4 ++++
 src/couch/src/couch_file.erl              | 18 ++++++++++++++++++
 src/couch/test/eunit/couch_file_tests.erl | 15 ++++++++++++++-
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/rel/overlay/etc/default.ini b/rel/overlay/etc/default.ini
index 6f5e459d9..866b0c79a 100644
--- a/rel/overlay/etc/default.ini
+++ b/rel/overlay/etc/default.ini
@@ -172,6 +172,10 @@ view_index_dir = {{view_index_dir}}
 ; running clean up per clustered db.
 ;index_cleanup_delay_msec = 30000
 
+; Perform a check that couch_file's .eof field matches the true eof before
+; writing a header
+;validate_eof = true
+
 [bt_engine_cache]
 ; Memory used for btree engine cache. This is a cache for top levels of
 ; database btrees (id tree, seq tree) and a few terms from the db header. Value
diff --git a/src/couch/priv/stats_descriptions.cfg 
b/src/couch/priv/stats_descriptions.cfg
index 3497b728b..8c1d2200a 100644
--- a/src/couch/priv/stats_descriptions.cfg
+++ b/src/couch/priv/stats_descriptions.cfg
@@ -438,6 +438,10 @@
     {type, counter},
     {desc, <<"number of the attempts to read beyond set limit">>}
 ]}.
+{[couchdb, eof_mismatch], [
+    {type, counter},
+    {desc, <<"number of times couch_file eof is not the same as true eof">>}
+]}.
 {[fsync, time], [
     {type, histogram},
     {desc, <<"microseconds to call fsync">>}
diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl
index 2d0920f7e..1a69fa017 100644
--- a/src/couch/src/couch_file.erl
+++ b/src/couch/src/couch_file.erl
@@ -771,6 +771,7 @@ find_newest_header(Fd, [{Location, Size} | LocationSizes]) 
->
     end.
 
 handle_write_header(Bin, #file{fd = Fd, eof = Pos} = File) ->
+    validate_eof(File),
     BinSize = byte_size(Bin),
     case Pos rem ?SIZE_BLOCK of
         0 -> Padding = <<>>;
@@ -1026,6 +1027,23 @@ dup(#file{fd = Fd} = File) ->
             File
     end.
 
+validate_eof(#file{} = File) ->
+    case config:get_boolean("couchdb", "validate_eof", true) of
+        false ->
+            ok;
+        true ->
+            {ok, ActualPos} = file:position(File#file.fd, eof),
+            if
+                File#file.eof /= ActualPos ->
+                    couch_stats:increment_counter([couchdb, eof_mismatch]),
+                    throw_stop(
+                        {eof_mismatch, ActualPos, File#file.eof, 
File#file.filepath}, 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..c22499a09 100644
--- a/src/couch/test/eunit/couch_file_tests.erl
+++ b/src/couch/test/eunit/couch_file_tests.erl
@@ -157,7 +157,8 @@ 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)
                 ]
             }
         }
@@ -401,6 +402,18 @@ should_handle_append_bin_upgrade_clause(Fd) ->
     ?assertEqual(Pos2, Len1),
     ?assertEqual({ok, Bin}, couch_file:pread_binary(Fd, Pos2)).
 
+should_detect_eof_mismatch(_) ->
+    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),
+    ?assertMatch(
+        {error, {eof_mismatch, 4, 0, _}}, couch_file:write_header(Fd, ~"1111")
+    ),
+    catch couch_file:close(Fd).
+
 header_test_() ->
     {
         "File header read/write tests",

Reply via email to