Skip to content

Commit

Permalink
stream: Add assertion against dangling read/write coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
dermesser committed Sep 29, 2024
1 parent 109cc64 commit d75fd7b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
12 changes: 12 additions & 0 deletions test/stream_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ TEST(TtyTest, closeWhileReading) {
run_loop(setup);
}

TEST(PipeTest, danglingReadDies) {
auto f = [](const Loop &loop) -> uvco::Promise<std::optional<std::string>> {
auto [read, write] = pipe(loop);
auto _ = write.write("Hello");
return read.read();
};
auto setup = [&f](const Loop &loop) -> uvco::Promise<void> {
co_await f(loop);
};
EXPECT_DEATH(run_loop(setup), R"(stream must outlive reader coroutine)");
}

TEST(PipeTest, pipePingPong) {
auto setup = [&](const Loop &loop) -> uvco::Promise<void> {
auto [read, write] = pipe(loop);
Expand Down
6 changes: 6 additions & 0 deletions uvco/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ StreamBase::~StreamBase() {
// descriptors.
closeHandle(stream_.release());
}
BOOST_ASSERT_MSG(
!reader_,
"StreamBase::~StreamBase(): stream must outlive reader coroutines.");
BOOST_ASSERT_MSG(
!writer_,
"StreamBase::~StreamBase(): stream must outlive writer coroutines.");
}

TtyStream TtyStream::tty(const Loop &loop, int fd) {
Expand Down
4 changes: 3 additions & 1 deletion uvco/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class StreamBase {
/// Read available data (up to `buffer.size()` bytes) from stream. Returns
/// the number of bytes read, or 0 on EOF or closed handle (`close()`).
///
/// Only one read() coroutine may be active at a time.
/// Only one read() coroutine may be active at a time. The stream must outlive
/// the coroutine, i.e. live until `co_await stream.read(...)` returns a
/// result.
///
/// Throws `UvcoException` on error.
Promise<size_t> read(std::span<char> buffer);
Expand Down

0 comments on commit d75fd7b

Please sign in to comment.