Opened 11 years ago

Closed 10 years ago

#6138 closed Bugs (invalid)

ip::tcp::resolver::cancel problem

Reported by: forever Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.48.0 Severity: Problem
Keywords: Cc:

Description

If connection is down, boost::asio::ip::tcp::resolver::cancel' doesn't work properly by attemting to resolve a host using boost::asio::ip::tcp::resolver::async_resolve'.

Handler has invoked with the code host_not_found_try_again' and not operation_aborted'.

Though it's documented that function forces the completion of any pending asynchronous operations on the host resolver, the handler for each cancelled operation will be invoked with the `boost::asio::error::operation_aborted' error code.

Link

#include <iostream>
#include <boost/asio.hpp>
 
int main() {
   boost::asio::io_service ios;
   boost::asio::deadline_timer timer(ios);
   boost::asio::ip::tcp::resolver resolver(ios);
   boost::asio::ip::tcp::resolver::query query("google.ru", "http");
 
   auto func = [&](const boost::system::error_code& e, boost::asio::ip::tcp::resolver::iterator it) {
      if ( e ) { std::cout << "e: " << e.message() << std::endl; }
      if ( it == boost::asio::ip::tcp::resolver::iterator() ) { std::cout << "null iterator" << std::endl; }
      if ( e || it == boost::asio::ip::tcp::resolver::iterator() ) { return; }
 
      for ( ; it != boost::asio::ip::tcp::resolver::iterator(); ++it) {
         std::cout << "it: " << it->endpoint() << std::endl;
      }
   };
 
   resolver.async_resolve(
      query,
      func
   );
 
   timer.expires_from_now(boost::posix_time::milliseconds(1000));
   timer.async_wait(
      [&](const boost::system::error_code& error){
         std::cout << "timer expired" << std::endl;
         resolver.cancel();
      }
   );
   ios.run();
}

Change History (1)

comment:1 by chris_kohlhoff, 10 years ago

Resolution: invalid
Status: newclosed

Cancellation is best effort. As your connection is down, the resolve operation is already in progress (and no longer really "pending") and so cannot be cancelled.

Note: See TracTickets for help on using tickets.