Skip to content

Commit 9d00617

Browse files
committed
fix(async): complete pending operations during shutdown
1 parent 0ef2ae7 commit 9d00617

4 files changed

Lines changed: 50 additions & 8 deletions

File tree

include/vix/async/core/scheduler.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,12 @@ namespace vix::async::core
199199

200200
{
201201
std::unique_lock<std::mutex> lock(m_);
202+
202203
cv_.wait(lock, [this]()
203204
{ return stop_requested_.load(std::memory_order_acquire) ||
204205
!handle_q_.empty() ||
205206
!fn_q_.empty(); });
206207

207-
if (stop_requested_.load(std::memory_order_acquire))
208-
{
209-
handle_q_.clear();
210-
fn_q_.clear();
211-
break;
212-
}
213-
214208
if (!handle_q_.empty())
215209
{
216210
h = handle_q_.front();
@@ -221,6 +215,10 @@ namespace vix::async::core
221215
fn = std::move(fn_q_.front());
222216
fn_q_.pop_front();
223217
}
218+
else if (stop_requested_.load(std::memory_order_acquire))
219+
{
220+
break;
221+
}
224222
}
225223

226224
if (h)

src/net/asio_await.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,25 @@ namespace vix::async::net::detail
8181
vix::async::core::io_context *ctx,
8282
std::coroutine_handle<> h) noexcept
8383
{
84-
if (!ctx || !h)
84+
if (!h)
8585
{
8686
return;
8787
}
8888

89+
if (!ctx)
90+
{
91+
h.resume();
92+
return;
93+
}
94+
95+
auto &sched = ctx->get_scheduler();
96+
97+
if (sched.stop_requested())
98+
{
99+
h.resume();
100+
return;
101+
}
102+
89103
ctx->post(h);
90104
}
91105

src/net/asio_tcp.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ namespace vix::async::net
154154
void close() noexcept override
155155
{
156156
std::error_code ec;
157+
158+
if (!sock_.is_open())
159+
{
160+
return;
161+
}
162+
163+
sock_.cancel(ec);
164+
ec.clear();
165+
166+
sock_.shutdown(tcp::socket::shutdown_both, ec);
167+
ec.clear();
168+
157169
sock_.close(ec);
158170
}
159171

@@ -245,6 +257,15 @@ namespace vix::async::net
245257
void close() noexcept override
246258
{
247259
std::error_code ec;
260+
261+
if (!acc_.is_open())
262+
{
263+
return;
264+
}
265+
266+
acc_.cancel(ec);
267+
ec.clear();
268+
248269
acc_.close(ec);
249270
}
250271

src/net/asio_udp.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ namespace vix::async::net
143143
void close() noexcept override
144144
{
145145
std::error_code ec;
146+
147+
if (!sock_.is_open())
148+
{
149+
return;
150+
}
151+
152+
sock_.cancel(ec);
153+
ec.clear();
154+
146155
sock_.close(ec);
147156
}
148157

0 commit comments

Comments
 (0)