Skip to content

Commit

Permalink
append_challenged_posix_file_impl: allow destructing file with no que…
Browse files Browse the repository at this point in the history
…ued work

There is no problem destructing the file if nothing is queued on it.
posix_file_impl::~posix_file_impl will auto close its file descriptor.

Refs scylladb/scylladb#7285

Test: unit(dev), file_io(debug)
Signed-off-by: Benny Halevy <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
bhalevy authored and avikivity committed Oct 11, 2020
1 parent ebcb3ae commit 35c255d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/core/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,10 @@ append_challenged_posix_file_impl::append_challenged_posix_file_impl(int fd, ope
append_challenged_posix_file_impl::~append_challenged_posix_file_impl() {
// If the file has not been closed we risk having running tasks
// that will try to access freed memory.
assert(_closing_state == state::closed);
//
// It is safe to destory it if nothing is queued.
// Note that posix_file_impl::~posix_file_impl auto-closes the file descriptor.
assert(_q.empty() && _logical_size == _committed_size);
}

bool
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/file_io_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,38 @@ SEASTAR_TEST_CASE(test_nowait_flag_correctness) {
}
});
}

SEASTAR_TEST_CASE(test_destruct_just_constructed_append_challenged_file) {
return tmp_dir::do_with_thread([] (tmp_dir& t) {
sstring filename = (t.get_path() / "testfile.tmp").native();
auto oflags = open_flags::rw | open_flags::create;
auto f = open_file_dma(filename, oflags).get0();
});
}

SEASTAR_TEST_CASE(test_destruct_append_challenged_file_after_write) {
return tmp_dir::do_with_thread([] (tmp_dir& t) {
sstring filename = (t.get_path() / "testfile.tmp").native();
auto buf = allocate_aligned_buffer<unsigned char>(4096, 4096);
std::fill(buf.get(), buf.get() + 4096, 0);

auto f = open_file_dma(filename, open_flags::rw | open_flags::create).get0();
f.dma_write(0, buf.get(), 4096).get();
});
}

SEASTAR_TEST_CASE(test_destruct_append_challenged_file_after_read) {
return tmp_dir::do_with_thread([] (tmp_dir& t) {
sstring filename = (t.get_path() / "testfile.tmp").native();
auto buf = allocate_aligned_buffer<unsigned char>(4096, 4096);
std::fill(buf.get(), buf.get() + 4096, 0);

auto f = open_file_dma(filename, open_flags::rw | open_flags::create).get0();
f.dma_write(0, buf.get(), 4096).get();
f.flush().get0();
f.close().get();

f = open_file_dma(filename, open_flags::rw).get0();
f.dma_read(0, buf.get(), 4096).get();
});
}

0 comments on commit 35c255d

Please sign in to comment.