3

I have a simple boost::asio::ip::tcp::acceptor which does almost nothing - it accepts connections in an infinite loop. I then have a number of connectors running at the same time trying to connect...

pSocket->async_connect(endpoint, 
        [=](boost::system::error_code error)
        {
            if(!error)
            {
                boost::asio::ip::tcp::no_delay noDelay(true);
                pSocket->set_option(noDelay, error);
                assert(!error);
                std::cout << error.message() << '\n'; // "An invalid argument was supplied"
            }
        });

Everything is running in infinite loops and I'm running 2 clients and 1 server, all loopback connections. After a while (hundreds of successful connects and disconnects) when setting the no_delay option on the connected socket I get the error An invalid argument was supplied.

Under what conditions can setting an option on a socket cause this error? Has anyone seen this before, and know why it's happening and/or a way to fix it?

Update: If I change the set set_option to something like...

do
{
    pSocket->set_option(noDelay, error);
} while(error);

...it will succeed - usually on the second try after it fails.

David
  • 27,652
  • 18
  • 89
  • 138

1 Answers1

3

Yesterday i debug set_option step by step, problem that set_option uses io_service_impl object but it contains bad socket pointer, that's why it crushes. But on socket creation nothing is set this socket, so i think you cant set option on socket in this way.

You must open socket before set_option, try this before async_connect and set_option:

sock->open(boost::asio::ip::tcp::v4());

Error is called Bad file descriptor which direct on non-initialized socket on set_option call.

Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
  • The error isn't even in the application with an `acceptor`. It's in the client, which calls `async_connect` – David Oct 11 '12 at 17:51
  • I'm not setting options on the accept side... Like I said - all this is on the connect side. – David Oct 11 '12 at 17:58
  • If I do it before the connect I get "The file handle supplied is not valid" - lol... worthless error. – David Oct 11 '12 at 18:04
  • The way I'm doing it works 99 times out of a hundred before it ever fails. Interestingly on the fail case if I detect it and simply call the same thing again, it succeeds. – David Oct 12 '12 at 13:05