Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:27:38+00:00 2026-06-04T16:27:38+00:00

My code uses boost::asio and io_service in a single thread to perform various socket

  • 0

My code uses boost::asio and io_service in a single thread to perform various socket operations. All operations are asynchronous and every handler depends on the boost::system::error_code (particularly boost::asio::error::operation_aborted) to determine the result of the operation.

It’s been working perfectly well until I changed the logic to make several concurrent connections and pick the fastest one. That is, when the first async_read_some handler fires, I cancel other sockets (shutdown, close – everything) and proceed with the current one. In 95% of cases other sockets’ read handlers are invoked with the operation_aborted error. However sometimes, these read handlers are invoked without errors, telling me that they have successfully received N bytes.

But the documentation for socket::cancel() states:

This function causes all outstanding asynchronous connect, send and
receive operations to finish immediately, and the handlers for
cancelled operations will be passed the boost::asio::error::operation_aborted error.

So, the questions: Can I really rely on the operation_aborted error in production code? If I can, is it a bug in Asio from boost 1.46.1? If I can’t, is there any official documentation regarding this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:27:41+00:00Added an answer on June 4, 2026 at 4:27 pm

    Ok, the answers:

    1. No, I cannot rely on the operation_aborted error only.
    2. Of course, it’s not a bug in Asio, just a lack of experience on my side.
    3. There is a little bit of official documentation. It’s for timers, not sockets, however the same principles apply:

    If the timer has already expired when cancel() is called, then the handlers for asynchronous wait operations will:

    • have already been invoked; or
    • have been queued for invocation in the near future.

    Basically, I was wrong in assumption that if I use a single thread for io_service, then every operation will be blocked while some handler executes.

    The behavior I’m reporting actually makes a lot of sense and it seems that everyone who uses Asio knows that. I’ve combed through Asio’s mailing lists and have found a lot of discussion on the subject here, here, here and here.

    For instance, a write operation may complete successfully while
    you are inside a handler but before you have got around to calling
    socket cancel, causing its completion handler to be posted to the queue.
    As I understand it, the error code is determined by the status of the
    operation that completed, not the state of the socket at the time the
    handler is picked off the queue and executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My client application uses a boost::asio::ip::tcp::socket to connect to a remote server. If the
I'm in the process of constructing a Socket class that uses boost::asio . To
I'm trying to create a library that uses Boost ASIO (UDP multicast, asynchronous) and
I'm trying to write some code that uses boost::asio's sockets to send a message
I'm developing code that uses boost::asio . To test it, I need to mock
I am tring to port some code, that uses boost::unwrap_reference a lot to C++11.
I have the following code, in which Boost.Local uses a function callback to load
I'm using Valgrind --tool=drd to check my application that uses Boost::thread. Basically, the application
I have a section of code that uses a Boost library that results in
I have a C++ client app that uses Boost ASIO to make SSL connections

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.