Skip to content

Commit

Permalink
net/tls: Wait for data_{source,sink}::close()
Browse files Browse the repository at this point in the history
Fixes scylladb#799

data_{source,sink}::close() return a future. If it is not ready on
close() return, then the current tls session close() may result in use
after free.

Converting close_after_shutdown() to a coroutine and sequentially
co_awaiting on close() addresses this issue. The waiting is done
sequentially, as this is shutdown path anyway.
  • Loading branch information
p12tic committed Nov 24, 2024
1 parent cba633d commit 4e836f0
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/net/tls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module;
#endif

#include <any>
#include <coroutine>
#include <filesystem>
#include <stdexcept>
#include <system_error>
Expand Down Expand Up @@ -1604,17 +1605,18 @@ class session : public enable_lw_shared_from_this<session> {
future<> close_after_shutdown() {
_eof = true;
try {
(void)_in.close().handle_exception([](std::exception_ptr) {}); // should wake any waiters
co_await _in.close(); // should wake any waiters
} catch (...) {
}
try {
(void)_out.close().handle_exception([](std::exception_ptr) {});
co_await _out.close();
} catch (...) {
}

// make sure to wait for handshake attempt to leave semaphores. Must be in same order as
// handshake aqcuire, because in worst case, we get here while a reader is attempting
// re-handshake.
return with_semaphore(_in_sem, 1, [this] {
co_await with_semaphore(_in_sem, 1, [this] {
return with_semaphore(_out_sem, 1, [] {});
});
}
Expand Down

0 comments on commit 4e836f0

Please sign in to comment.