2013-03-30 35 views
6

Boost的ASIO调度程序似乎有一个严重问题,而且我似乎无法找到解决方法。现象的症状是,尽管有待处理的I/O操作要求它在epoll_wait中进行阻塞,但仍等待分派的唯一线程仍留在pthread_cond_wait feven中。即使异步I/O操作正在等待,只有处理io_service的线程正在等待

我可以通过让一个线程在循环中调用poll_one直到它返回零来轻松地复制此问题。这可以使线程调用run卡在pthread_cond_wait而线程调用poll_one突破循环。据推测,io_service期待该线程返回到epoll_wait中的块,但没有义务这样做,并且期望似乎是致命的。

是否需要线程与io_service s静态关联?

下面是一个显示死锁的例子。这是处理这个io_service的唯一线程,因为其他人已经移动了。肯定有未决插座操作:

#0 pthread_co[email protected]@GLIBC_2.3.2() from /lib64/libpthread.so.0 
#1 boost::asio::detail::posix_event::wait<boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex> > (...) at /usr/include/boost/asio/detail/posix_event.hpp:80 
#2 boost::asio::detail::task_io_service::do_run_one (...) at /usr/include/boost/asio/detail/impl/task_io_service.ipp:405 
#3 boost::asio::detail::task_io_service::run (...) at /usr/include/boost/asio/detail/impl/task_io_service.ipp:146 

我相信错误是如下:如果一个线程提供服务的I/O队列是的阻止在I/O插槽就绪检查线程,它调用调度函数,如果有任何其他线程在io服务上被阻塞,它必须发出信号。它目前仅表示当时是否有处理程序可以运行。但是这不会让套接字准备就绪的线程检查。

+0

run_one()返回的代码是什么? –

+0

如果它返回1是正常的,只有当它返回0时才需要重置io_service。这听起来不像你做错任何事情,你可以发布sscce吗? –

+0

@DavidSchwartz你确定pthread_cond_timedwait调用来自asio吗?我在代码中遇到了一些麻烦。 – janm

回答

6

这是一个错误。我已经能够通过在task_io_service::do_poll_one的非关键部分添加延迟来复制它。以下是booost/asio/detail/impl/task_io_service.ipp中修改的task_io_service::do_poll_one()的片段。唯一添加的是睡眠。

std::size_t task_io_service::do_poll_one(mutex::scoped_lock& lock, 
    task_io_service::thread_info& this_thread, 
    const boost::system::error_code& ec) 
{ 
    if (stopped_) 
    return 0; 

    operation* o = op_queue_.front(); 
    if (o == &task_operation_) 
    { 
    op_queue_.pop(); 
    lock.unlock(); 

    { 
     task_cleanup c = { this, &lock, &this_thread }; 
     (void)c; 

     // Run the task. May throw an exception. Only block if the operation 
     // queue is empty and we're not polling, otherwise we want to return 
     // as soon as possible. 
     task_->run(false, this_thread.private_op_queue); 
     boost::this_thread::sleep_for(boost::chrono::seconds(3)); 
    } 

    o = op_queue_.front(); 
    if (o == &task_operation_) 
     return 0; 
    } 

... 

我的测试驱动程序是相当基本的:“”

  • 通过一个计时器,将打印的异步工作循环每3秒钟一次。
  • 产生一个将轮询io_service的单个线程。
  • 延迟允许新的线程时间轮询io_service,并有主要电话io_service::run()而轮询线程睡在task_io_service::do_poll_one()

测试代码:

#include <iostream> 

#include <boost/asio/io_service.hpp> 
#include <boost/asio/steady_timer.hpp> 
#include <boost/chrono.hpp> 
#include <boost/thread.hpp> 

boost::asio::io_service io_service; 
boost::asio::steady_timer timer(io_service); 

void arm_timer() 
{ 
    std::cout << "."; 
    std::cout.flush(); 
    timer.expires_from_now(boost::chrono::seconds(3)); 
    timer.async_wait(boost::bind(&arm_timer)); 
} 

int main() 
{ 
    // Add asynchronous work loop. 
    arm_timer(); 

    // Spawn poll thread. 
    boost::thread poll_thread(
    boost::bind(&boost::asio::io_service::poll, boost::ref(io_service))); 

    // Give time for poll thread service reactor. 
    boost::this_thread::sleep_for(boost::chrono::seconds(1)); 

    io_service.run(); 
} 

和Debug:

[[email protected] bug]$ gdb a.out 
... 
(gdb) r 
Starting program: /home/twsansbury/dev/bug/a.out 

[Thread debugging using libthread_db enabled] 
.[New Thread 0xb7feeb90 (LWP 31892)] 
[Thread 0xb7feeb90 (LWP 31892) exited]

在这一点上,arm_timer()已打印 “”一次(当它是武装时)。轮询螺纹以非阻塞的方式为反应堆提供服务,并且睡眠3秒,而op_queue_为空(将在task_cleanup c退出范围时被添加回op_queue_)。 op_queue_是空的,主线程调用io_service::run(),看到op_queue_是空的,并且自己成为first_idle_thread_,它在wakeup_event上等待。轮询线程完成睡眠,并返回0,使主线程等待wakeup_event

等待10〜秒,充足的时间为arm_timer()要准备好后,我中断调试器:

Program received signal SIGINT, Interrupt. 
0x00919402 in __kernel_vsyscall() 
(gdb) bt 
#0 0x00919402 in __kernel_vsyscall() 
#1 0x0081bbc5 in [email protected]@GLIBC_2.3.2() from /lib/libpthread.so.0 
#2 0x00763b3d in [email protected]@GLIBC_2.3.2() from /lib/libc.so.6 
#3 0x08059dc2 in void boost::asio::detail::posix_event::wait >(boost::asio::detail::scoped_lock&)() 
#4 0x0805a009 in boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lock&, boost::asio::detail::task_io_service_thread_info&, boost::system::error_code const&)() 
#5 0x0805a11c in boost::asio::detail::task_io_service::run(boost::system::error_code&)() 
#6 0x0805a1e2 in boost::asio::io_service::run()() 
#7 0x0804db78 in main()

侧由端的时间表如下:

   poll thread     |   main thread 
---------------------------------------+--------------------------------------- 
    lock()        | 
    do_poll_one()      |       
    |-- pop task_operation_ from   | 
    | queue_op_      | 
    |-- unlock()       | lock() 
    |-- create task_cleanup    | do_run_one() 
    |-- service reactor (non-block)  | `-- queue_op_ is empty 
    |-- ~task_cleanup()     |  |-- set thread as idle 
    | |-- lock()      |  `-- unlock() 
    | `-- queue_op_.push(    | 
    |  task_operation_)    | 
    `-- task_operation_ is    | 
     queue_op_.front()    | 
     `-- return 0      | // still waiting on wakeup_event 
    unlock()        |

尽我所知,没有修补的副作用:

if (o == &task_operation_) 
    return 0; 

到:

if (o == &task_operation_) 
{ 
    if (!one_thread_) 
    wake_one_thread_and_unlock(lock); 
    return 0; 
} 

无论如何,我已经提交了bug and fix。考虑留意官方回应的机票。