2019独角兽企业重金招聘Python工程师标准>>>
最近在设计一个多线程分块支持续传的http的异步客户端时, 测试部门经常发现http下载模
块退出时偶尔会卡住, 在win7系统上由为明显. 反复检查代码, 并未明显问题, 于是专门写
了一个反复退出的单元测试, 立即发现问题, 并定位在io_service的析构函数中, 奇怪的是,
我的投递io的所有socket都早已经关闭, run线程也已经退出, 按理说, 这时的io_service的
outstanding_work_应该为0才是, 可我一看它却是1, 于是始终在win_iocp_io_service.hpp的
shutdown_service里一直循环调用GetQueuedCompletionStatus, 从而导致无法正常退出...
很明显, 这是asio对outstanding_work_计数维护的有问题, 为了解决
问题, 于是我很快想到不使用iocp, 添加宏BOOST_ASIO_DISABLE_IOCP一切就正常了...
由于自己使用的是boost.1.45版本, 于是换了个boost.1.46.1再试试, 结果一样. 难道这么严
重的bug跨在了这两个非常重要的发行版本上而没人知道?
在官方的邮件列表中细节检查, 终于看到了某人的bug报告和我描述的情况差不多, 而且
在那个人报告了bug的第二天, asio作者就发布了补丁, 但这个补丁并未更新到boost.1.45
和boost.1.46中, 唉, 这两个版本可是大版本啊, 估计受害人不少...
不过幸运的是, 我在boost的主分枝中看到了修正的代码.
下面是这个补丁内容:
From 81a6a51c0cb66de6bc77e1fa5dcd46b2794995e4 Mon Sep 17 00:00:00 2001
From: Christopher Kohlhoff
Date: Wed, 23 Mar 2011 15:03:56 +1100
Subject: [PATCH] On Windows, ensure the count of outstanding work is decremented for
abandoned operations (i.e. operations that are being cleaned up within
the io_service destructor).
---
asio/include/asio/detail/impl/dev_poll_reactor.ipp | 2 ++
asio/include/asio/detail/impl/epoll_reactor.ipp | 2 ++
asio/include/asio/detail/impl/kqueue_reactor.ipp | 2 ++
asio/include/asio/detail/impl/select_reactor.ipp | 2 ++
.../asio/detail/impl/signal_set_service.ipp | 2 ++
asio/include/asio/detail/impl/task_io_service.ipp | 7 +++++++
.../asio/detail/impl/win_iocp_io_service.ipp | 11 +++++++++++
asio/include/asio/detail/task_io_service.hpp | 4 ++++
asio/include/asio/detail/win_iocp_io_service.hpp | 4 ++++
9 files changed, 36 insertions(+), 0 deletions(-)
diff --git a/asio/include/asio/detail/impl/dev_poll_reactor.ipp b/asio/include/asio/detail/impl/dev_poll_reactor.ipp
index 06d89ea..2a01993 100644
--- a/asio/include/asio/detail/impl/dev_poll_reactor.ipp
+++ b/asio/include/asio/detail/impl/dev_poll_reactor.ipp
@@ -63,6 +63,8 @@ void dev_poll_reactor::shutdown_service()
op_queue_[i].get_all_operations(ops);
timer_queues_.get_all_timers(ops);
+
+ io_service_.abandon_operations(ops);
}
// Helper class to re-register all descriptors with /dev/poll.
diff --git a/asio/include/asio/detail/impl/epoll_reactor.ipp b/asio/include/asio/detail/impl/epoll_reactor.ipp
index 22f567a..d08dedb 100644
--- a/asio/include/asio/detail/impl/epoll_reactor.ipp
+++ b/asio/include/asio/detail/impl/epoll_reactor.ipp
@@ -84,6 +84,8 @@ void epoll_reactor::shutdown_service()
}
timer_queues_.get_all_timers(ops);
+
+ io_service_.abandon_operations(ops);
}
void epoll_reactor::fork_service(asio::io_service::fork_event fork_ev)
diff --git a/asio/include/asio/detail/impl/kqueue_reactor.ipp b/asio/include/asio/detail/impl/kqueue_reactor.ipp
index f0cdf73..45aff60 100644
--- a/asio/include/asio/detail/impl/kqueue_reactor.ipp
+++ b/asio/include/asio/detail/impl/kqueue_reactor.ipp
@@ -74,6 +74,8 @@ void kqueue_reactor::shutdown_service()
}
timer_queues_.get_all_timers(ops);
+
+ io_service_.abandon_operations(ops);
}
void kqueue_reactor::fork_service(asio::io_service::fork_event fork_ev)
diff --git a/asio/include/asio/detail/impl/select_reactor.ipp b/asio/include/asio/detail/impl/select_reactor.ipp
index f4e0314..00fd9fc 100644
--- a/asio/include/asio/detail/impl/select_reactor.ipp
+++ b/asio/include/asio/detail/impl/select_reactor.ipp
@@ -81,6 +81,8 @@ void select_reactor::shutdown_service()
op_queue_[i].get_all_operations(ops);
timer_queues_.get_all_timers(ops);
+
+ io_service_.abandon_operations(ops);
}
void select_reactor::fork_service(asio::io_service::fork_event fork_ev)
diff --git a/asio/include/asio/detail/impl/signal_set_service.ipp b/asio/include/asio/detail/impl/signal_set_service.ipp
index f0f0e78..4cde184 100644
--- a/asio/include/asio/detail/impl/signal_set_service.ipp
+++ b/asio/include/asio/detail/impl/signal_set_service.ipp
@@ -145,6 +145,8 @@ void signal_set_service::shutdown_service()
reg = reg->next_in_table_;
}
}
+
+ io_service_.abandon_operations(ops);
}
void signal_set_service::fork_service(
diff --git a/asio/include/asio/detail/impl/task_io_service.ipp b/asio/include/asio/detail/impl/task_io_service.ipp
index cb585d5..0a2c6fa 100644
--- a/asio/include/asio/detail/impl/task_io_service.ipp
+++ b/asio/include/asio/detail/impl/task_io_service.ipp
@@ -230,6 +230,13 @@ void task_io_service::post_deferred_completions(
}
}
+void task_io_service::abandon_operations(
+ op_queue& ops)
+{
+ op_queue ops2;
+ ops2.push(ops);
+}
+
std::size_t task_io_service::do_one(mutex::scoped_lock& lock,
task_io_service::idle_thread_info* this_idle_thread)
{
diff --git a/asio/include/asio/detail/impl/win_iocp_io_service.ipp b/asio/include/asio/detail/impl/win_iocp_io_service.ipp
index ca3125e..7aaa6b8 100644
--- a/asio/include/asio/detail/impl/win_iocp_io_service.ipp
+++ b/asio/include/asio/detail/impl/win_iocp_io_service.ipp
@@ -262,6 +262,17 @@ void win_iocp_io_service::post_deferred_completions(
}
}
+void win_iocp_io_service::abandon_operations(
+ op_queue& ops)
+{
+ while (win_iocp_operation* op = ops.front())
+ {
+ ops.pop();
+ ::InterlockedDecrement(&outstanding_work_);
+ op->destroy();
+ }
+}
+
void win_iocp_io_service::on_pending(win_iocp_operation* op)
{
if (::InterlockedCompareExchange(&op->ready_, 1, 0) == 1)
diff --git a/asio/include/asio/detail/task_io_service.hpp b/asio/include/asio/detail/task_io_service.hpp
index 285d83e..654f83c 100644
--- a/asio/include/asio/detail/task_io_service.hpp
+++ b/asio/include/asio/detail/task_io_service.hpp
@@ -105,6 +105,10 @@ public:
// that work_started() was previously called for each operation.
ASIO_DECL void post_deferred_completions(op_queue& ops);
+ // Process unfinished operations as part of a shutdown_service operation.
+ // Assumes that work_started() was previously called for the operations.
+ ASIO_DECL void abandon_operations(op_queue& ops);
+
private:
// Structure containing information about an idle thread.
struct idle_thread_info;
diff --git a/asio/include/asio/detail/win_iocp_io_service.hpp b/asio/include/asio/detail/win_iocp_io_service.hpp
index a562834..b5d7f0b 100644
--- a/asio/include/asio/detail/win_iocp_io_service.hpp
+++ b/asio/include/asio/detail/win_iocp_io_service.hpp
@@ -126,6 +126,10 @@ public:
ASIO_DECL void post_deferred_completions(
op_queue& ops);
+ // Enqueue unfinished operation as part of a shutdown_service operation.
+ // Assumes that work_started() was previously called for the operations.
+ ASIO_DECL void abandon_operations(op_queue& ops);
+
// Called after starting an overlapped I/O operation that did not complete
// immediately. The caller must have already called work_started() prior to
// starting the operation.
--
1.7.0.1
注: 在boost.asio中, 使用这个补丁时, 需要将ASIO_DECL 改成 BOOST_ ASIO_DECL
注: 最新发布的boost1.47版本已经修复了这个bug, 建议尽量采用新版本的boost.
这是我第二次在使用asio的过程中, 发现的比较严重的bug了, 不过幸运的是, 每一次都能在官方的论坛
或邮件列表中找到解决方案.