Skip to content

Commit 4e836f0

Browse files
committed
net/tls: Wait for data_{source,sink}::close()
Fixes #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.
1 parent cba633d commit 4e836f0

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/net/tls.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module;
2424
#endif
2525

2626
#include <any>
27+
#include <coroutine>
2728
#include <filesystem>
2829
#include <stdexcept>
2930
#include <system_error>
@@ -1604,17 +1605,18 @@ class session : public enable_lw_shared_from_this<session> {
16041605
future<> close_after_shutdown() {
16051606
_eof = true;
16061607
try {
1607-
(void)_in.close().handle_exception([](std::exception_ptr) {}); // should wake any waiters
1608+
co_await _in.close(); // should wake any waiters
16081609
} catch (...) {
16091610
}
16101611
try {
1611-
(void)_out.close().handle_exception([](std::exception_ptr) {});
1612+
co_await _out.close();
16121613
} catch (...) {
16131614
}
1615+
16141616
// make sure to wait for handshake attempt to leave semaphores. Must be in same order as
16151617
// handshake aqcuire, because in worst case, we get here while a reader is attempting
16161618
// re-handshake.
1617-
return with_semaphore(_in_sem, 1, [this] {
1619+
co_await with_semaphore(_in_sem, 1, [this] {
16181620
return with_semaphore(_out_sem, 1, [] {});
16191621
});
16201622
}

0 commit comments

Comments
 (0)