Merge pull request #14131 from rabbitmq/mergify/bp/v4.1.x/pr-14108

CQ: Retry opening file when flushing buffers to avoid "DELETE PENDING" issues on Windows (backport #14108)
This commit is contained in:
Michael Klishin 2025-06-25 19:15:37 +04:00 committed by GitHub
commit 30f12e2664
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 1 deletions

View File

@ -194,6 +194,25 @@ maybe_flush_buffer(State = #qs{ write_buffer_size = WriteBufferSize }) ->
false -> State
end.
open_eventually(File, Modes) ->
open_eventually(File, Modes, 3).
open_eventually(_, _, 0) ->
{error, eacces};
open_eventually(File, Modes, N) ->
case file:open(File, Modes) of
OK = {ok, _} ->
OK;
%% When the current write file was recently deleted it
%% is possible on Windows to get an {error,eacces}.
%% Sometimes Windows sets the files to "DELETE PENDING"
%% state and delays deletion a bit. So we wait 10ms and
%% try again up to 3 times.
{error, eacces} ->
timer:sleep(10),
open_eventually(File, Modes, N - 1)
end.
flush_buffer(State = #qs{ write_buffer_size = 0 }, _) ->
State;
flush_buffer(State0 = #qs{ write_buffer = WriteBuffer }, FsyncFun) ->
@ -204,7 +223,7 @@ flush_buffer(State0 = #qs{ write_buffer = WriteBuffer }, FsyncFun) ->
Writes = flush_buffer_build(WriteList, CheckCRC32, SegmentEntryCount),
%% Then we do the writes for each segment.
State = lists:foldl(fun({Segment, LocBytes}, FoldState) ->
{ok, Fd} = file:open(segment_file(Segment, FoldState), [read, write, raw, binary]),
{ok, Fd} = open_eventually(segment_file(Segment, FoldState), [read, write, raw, binary]),
case file:position(Fd, eof) of
{ok, 0} ->
%% We write the file header if it does not exist.