From a2cb707338c4f0cacf25656410f010ba2f855227 Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Fri, 22 Nov 2024 14:33:52 +0200 Subject: [PATCH] net/tls: Extract session::close_after_shutdown() --- src/net/tls.cc | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/net/tls.cc b/src/net/tls.cc index bc8f79f0d98..54e95abe2e5 100644 --- a/src/net/tls.cc +++ b/src/net/tls.cc @@ -1594,26 +1594,31 @@ class session : public enable_lw_shared_from_this { auto me = shared_from_this(); // running in background. try to bye-handshake us nicely, but after 10s we forcefully close. engine().run_in_background(with_timeout(timer<>::clock::now() + std::chrono::seconds(10), shutdown()).finally([this] { - _eof = true; - try { - (void)_in.close().handle_exception([](std::exception_ptr) {}); // should wake any waiters - } catch (...) { - } - try { - (void)_out.close().handle_exception([](std::exception_ptr) {}); - } 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] { - return with_semaphore(_out_sem, 1, [] {}); - }); + return close_after_shutdown(); }).then_wrapped([me = std::move(me)](future<> f) { // must keep object alive until here. f.ignore_ready_future(); })); } } + + future<> close_after_shutdown() { + _eof = true; + try { + (void)_in.close().handle_exception([](std::exception_ptr) {}); // should wake any waiters + } catch (...) { + } + try { + (void)_out.close().handle_exception([](std::exception_ptr) {}); + } 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] { + return with_semaphore(_out_sem, 1, [] {}); + }); + } + // helper for sink future<> flush() noexcept { return with_semaphore(_out_sem, 1, [this] {