Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CORE-2814]: utils/file_io: fix double closing of ss::file in write_fully #18303

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/v/utils/file_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "bytes/iobuf.h"
#include "bytes/iostream.h"

#include <seastar/core/coroutine.hh>
#include <seastar/core/file.hh>
#include <seastar/core/fstream.hh>
#include <seastar/core/temporary_buffer.hh>
Expand Down Expand Up @@ -57,17 +58,11 @@ ss::future<> write_fully(const std::filesystem::path& p, iobuf buf) {
| ss::open_flags::truncate;
/// Closes file on failure, otherwise file is expected to be closed in the
/// success case where the ss::output_stream calls close()
return ss::with_file_close_on_failure(
ss::open_file_dma(p.string(), flags),
[buf = std::move(buf)](ss::file f) mutable {
return ss::make_file_output_stream(std::move(f), buf_size)
.then([buf = std::move(buf)](ss::output_stream<char> out) mutable {
return ss::do_with(
std::move(out),
[buf = std::move(buf)](ss::output_stream<char>& out) mutable {
return write_iobuf_to_output_stream(std::move(buf), out)
.then([&out]() mutable { return out.close(); });
});
});
auto out = co_await ss::with_file_close_on_failure(
ss::open_file_dma(p.string(), flags), [](ss::file f) {
return ss::make_file_output_stream(std::move(f), buf_size);
});
co_await write_iobuf_to_output_stream(std::move(buf), out).finally([&out] {
pgellert marked this conversation as resolved.
Show resolved Hide resolved
return out.close();
});
Copy link
Member

@dotnwat dotnwat May 10, 2024

Choose a reason for hiding this comment

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

looks safe to me.

nit: I don't know if there is an idiomatic use of with_file_close_on_failure, but it seems like it would be:

auto f = co_await open();
co_await with_file_close_on_failure(
  // all the things that could fail and we don't want hand crafted error handling.
  make_file_output_stream.then(write_io_to_output_stream)...
);
co_await f.close();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ye, you just need to be sure that no path inside the lambda closes the file.
For the pr i copied how the function is used in the unit tests under seastar

}