Skip to content

Report errors on segment close instead of silently discarding them#592

Merged
kjnilsson merged 3 commits intomainfrom
report-errors-on-segment-close
Mar 17, 2026
Merged

Report errors on segment close instead of silently discarding them#592
kjnilsson merged 3 commits intomainfrom
report-errors-on-segment-close

Conversation

@kjnilsson
Copy link
Copy Markdown
Contributor

ra_log_segment:close/1 in append mode now propagates errors from sync and file:close instead of ignoring them. The segment writer asserts close succeeds when flushing mem tables and defers pivot file deletion until after the successor segment is opened.

@kjnilsson kjnilsson force-pushed the report-errors-on-segment-close branch from 5f43d98 to 83d1a7f Compare March 16, 2026 11:12
@kjnilsson kjnilsson force-pushed the report-errors-on-segment-close branch 2 times, most recently from ee99a47 to b9309d7 Compare March 16, 2026 15:38
@kjnilsson kjnilsson requested a review from Copilot March 17, 2026 11:14
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR changes segment lifecycle handling so that ra_log_segment:close/1 in append mode propagates failures (from sync and file:close/1) instead of always returning ok, and updates the segment writer to rely on/assume successful closes in key flush paths.

Changes:

  • Make ra_log_segment:close/1 (append mode) return sync/close errors to callers rather than silently discarding them.
  • Update segment writer flush logic to assert segment close succeeds, and defer pivot segment deletion until after the successor segment is opened.
  • Minor call-site adjustments to ignore close/1 return values where appropriate (mostly read-mode handles).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/ra_log_segment_writer.erl Defers pivot deletion until after successor is opened; asserts successful close when flushing mem tables.
src/ra_log_segment.erl Changes close/1 contract to propagate errors in append mode; updates some internal close call sites and types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +796 to +800
-spec close(state()) -> ok | {error, file:posix()}.
close(#state{cfg = #cfg{fd = Fd,
mode = append,
file_advise = FileAdvise}} = State) ->
% close needs to be defensive and idempotent so we ignore the return
% values here
_ = sync(State),
_ = case is_full(State) of
true ->
file:advise(Fd, 0, 0, FileAdvise);
false ->
ok
end,
_ = file:close(Fd),
ok;
file_advise = FileAdvise}} = State0) ->
case sync(State0) of
Comment on lines +810 to +811
%TODO: if the sync completed ok do we care about errors during
% file:close/1?
{noreply, State0};
Succ ->
_ = prim_file:delete(Pivot),
_ = ra_log_segment:close(Succ),
Comment on lines +796 to +800
-spec close(state()) -> ok | {error, file:posix()}.
close(#state{cfg = #cfg{fd = Fd,
mode = append,
file_advise = FileAdvise}} = State) ->
% close needs to be defensive and idempotent so we ignore the return
% values here
_ = sync(State),
_ = case is_full(State) of
true ->
file:advise(Fd, 0, 0, FileAdvise);
false ->
ok
end,
_ = file:close(Fd),
ok;
file_advise = FileAdvise}} = State0) ->
case sync(State0) of
ra_log_segment:close/1 in append mode now propagates errors from
sync and file:close instead of ignoring them. The segment writer
asserts close succeeds when flushing mem tables and defers pivot
file deletion until after the successor segment is opened.
@kjnilsson kjnilsson force-pushed the report-errors-on-segment-close branch from b9309d7 to f035238 Compare March 17, 2026 11:30
@michaelklishin michaelklishin added this to the 3.0.2 milestone Mar 17, 2026
@kjnilsson kjnilsson merged commit 1a2e9b5 into main Mar 17, 2026
6 checks passed
@dumbbell-rabbitmq dumbbell-rabbitmq deleted the report-errors-on-segment-close branch March 23, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants