lundi 20 juillet 2015

boost::asio::io_service crash in win_mutex lock

I've been having a problem with boost::asio where timer and/or sockets created using a global io_service instance crash during construction. The system where the crash occurs is as follows:

  • Windows 7

  • Visual Studio 2013 Express for Windows Desktop; v 12.0.31101.00 Update 4

  • Boost 1.57, dynamically linked, compiled with multithreading as, e.g. boost_thread-vc120-mt-gd-1_57.dll

I've been able to replicate the issue in the following simplified code:

// file global_io_service.h

#ifndef INCLUDED_GLOBAL_IO_SERVICE_H
#define INCLUDED_GLOBAL_IO_SERVICE_H

#include <boost/asio/io_service.hpp>

#include <iostream>
#include <string>

namespace foo{

class foo_base_io_service{ 

public:

    foo_base_io_service(const std::string& name)
      : d_who_am_i(name)
    {
        std::cout << "constructing copy " << ++foo_base_io_service::num_instances << "my name is " << d_who_am_i << std::endl;
    }

    boost::asio::io_service& get_ref()
    {
        std::cout << "class requested copy of " << d_who_am_i << std::endl;
        return d_ios;
    }

private:

    // this class is not copyable
    foo_base_io_service(const foo_base_io_service&);
    foo_base_io_service& operator=(const foo_base_io_service&);

    std::string d_who_am_i;
    static int num_instances;

    boost::asio::io_service d_ios;
};

extern foo_base_io_service global_timer_io_service;

} // namespace foo

#endif

// File global_io_service.cpp

#include "global_io_service.h"

namespace foo{
    foo_base_io_service global_timer_io_service("FOO_TIMER_SERVICE");

    // static initialization
    int foo_base_io_service::num_instances = 0;
}

// FILE main.cpp

#include <WinSock2.h>
#include "global_io_service.h"
#include <boost/asio/deadline_timer.hpp>

int main(int argc, char *argv[])
{

    // causes crash
    boost::asio::deadline_timer crash_timer(foo::global_timer_io_service.get_ref());


    return 0 ;
}

Here is the backtrace of the crash:

test_io_service.exe!boost::asio::detail::win_mutex::lock() Line 51

test_io_service.exe!boost::asio::detail::scoped_lock::scoped_lock(boost::asio::detail::win_mutex & m) Line 47

test_io_service.exe!boost::asio::detail::win_iocp_io_service::do_add_timer_queue(boost::asio::detail::timer_queue_base & queue) Line 477

test_io_service.exe!boost::asio::detail::win_iocp_io_service::add_timer_queue >(boost::asio::detail::timer_queue > & queue) Line 79

test_io_service.exe!boost::asio::detail::deadline_timer_service >::deadline_timer_service >(boost::asio::io_service & io_service) Line 69

test_io_service.exe!boost::asio::deadline_timer_service >::deadline_timer_service >(boost::asio::io_service & io_service) Line 78

test_io_service.exe!boost::asio::detail::service_registry::create > >(boost::asio::io_service & owner) Line 81

test_io_service.exe!boost::asio::detail::service_registry::do_use_service(const boost::asio::io_service::service::key & key, boost::asio::io_service::service * (boost::asio::io_service &) * factory) Line 123

test_io_service.exe!boost::asio::detail::service_registry::use_service > >() Line 49

test_io_service.exe!boost::asio::use_service > >(boost::asio::io_service & ios) Line 34

test_io_service.exe!boost::asio::basic_io_object >,0>::basic_io_object >,0>(boost::asio::io_service & io_service) Line 91

test_io_service.exe!boost::asio::basic_deadline_timer,boost::asio::deadline_timer_service > >::basic_deadline_timer,boost::asio::deadline_timer_service > >(boost::asio::io_service & io_service) Line 151

test_io_service.exe!main(int argc, char * * argv) Line 16 C++

Here's what I've learned:

  • The issue does not occur in Ubuntu 14.04, Ubuntu 14.10 or Red Hat 6.5 with boost 1.54.
  • The issue appears related to the order of inclusion of Winsock2. For instance, exchanging the order of inclusion with global_io_service.h eliminates the crash.
  • The issue appears related to the extern linkage of global_timer_io_service. Moving the definition of global_timer_io_service into main.cpp eliminates the crash.
  • I've found reports of similar crashes occuring on io_service internal critical sections. Those issues were mostly related to lifetime of io_service objects being passed into timer/socket constructors. In my case, I think the io_service I'm using was already constructed before main is entered.
  • My gut says there is a race condition (perhaps some global state setup in WinSock2?) that prevents proper construction of the io_service object.

Hopefully, I'm having a bad day and invoking undefined behavior. Otherwise, I'd like to understand why this is happening? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire