Files
scylladb/utils/limiting_data_source.hh
Pavel Emelyanov 70b9ae04ff Merge 'encryption: fix deadlock in encrypted_data_source::get()' from Ernest Zaslavsky
When encrypted_data_source::get() caches a trailing block in _next, the next call takes it directly — bypassing input_stream::read(), which checks _eof. It then calls input_stream::read_exactly() on the already-drained stream. Unlike read(), read_up_to(), and consume(), read_exactly() does not check _eof when the buffer is empty, so it calls _fd.get() on a source that already returned EOS.

In production this manifested as stuck encrypted SSTable component downloads during tablet restore: the underlying chunked_download_source hung forever on the post-EOS get(), causing 4 tablets to never complete. The stuck files were always block-aligned sizes (8k, 12k) where _next gets populated and the source is fully consumed in the same call.

Fix by checking _input.eof() before calling read_exactly(). When the stream already reached EOF, buf2 is known to be empty, so the call is skipped entirely.

A comprehensive test is added that uses a strict_memory_source which fails on post-EOS get(), reproducing the exact code path that caused the production deadlock.

Fixes: https://scylladb.atlassian.net/browse/SCYLLADB-1128

Backport to 2025.3/4 and 2026.1 is needed since it fixes a bug that may bite us in production, to be on the safe side

Closes scylladb/scylladb#29110

* github.com:scylladb/scylladb:
  encryption: fix deadlock in encrypted_data_source::get()
  test_lib: mark `limiting_data_source_impl` as not `final`
  Fix formatting after previous patch
  Fix indentation after previous patch
  test_lib: make limiting_data_source_impl available to tests

(cherry picked from commit 3b9398dfc8)

Closes scylladb/scylladb#29198
2026-04-06 14:23:05 +03:00

37 lines
1.4 KiB
C++

/*
* Copyright (C) 2017-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <seastar/core/iostream.hh>
#include <seastar/core/temporary_buffer.hh>
class limiting_data_source_impl : public seastar::data_source_impl {
seastar::data_source _src;
seastar::noncopyable_function<size_t()> _limit_generator;;
seastar::temporary_buffer<char> _buf;
seastar::future<seastar::temporary_buffer<char>> do_get();
public:
limiting_data_source_impl(seastar::data_source&& src, seastar::noncopyable_function<size_t()>&& limit_generator);
limiting_data_source_impl(limiting_data_source_impl&&) noexcept = default;
limiting_data_source_impl& operator=(limiting_data_source_impl&&) noexcept = default;
seastar::future<seastar::temporary_buffer<char>> get() override;
seastar::future<seastar::temporary_buffer<char>> skip(uint64_t n) override;
};
/// \brief Creates an data_source from another data_source but returns its data in chunks not bigger than a given limit
///
/// \param src Source data_source from which data will be taken
/// \return resulting data_source that returns data in chunks not bigger than a given limit
seastar::data_source make_limiting_data_source(seastar::data_source&& src,
seastar::noncopyable_function<size_t()>&& limit_generator);