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 9231103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:59:16+00:00 2026-06-18T05:59:16+00:00

Single-threaded application. It happens not every time, only after 1.5 hours of high load.

  • 0

Single-threaded application.

It happens not every time, only after 1.5 hours of high load.

  1. tcp::socket::async_connect
  2. tcp::socket::close (by deadline_timer)
  3. async_connect_handler gives success error_code (one of a million times), but socket is closed by(2). 99.999% of time it gives errno=125 (ECANCELED).

Is it possible that socket implementation or boost asio somehow do this:

  1. async_connect
  2. async success posted to io_service
  3. close by timer
  4. async success handled by me, not affected by close

Right now solved by saving state in my variables, ignoring accept success.

Linux 2.6 (fedora).
Boost 1.46.0

PS: ofcouse possible bug on my part… But runs smoothly for days if not 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-18T05:59:17+00:00Added an answer on June 18, 2026 at 5:59 am

    As Igor mentions in the comments, the completion handler is already queued.

    This scenario is the result of a separation in time between when an operation executes and when a handler is invoked. The documentation for io_service::run(), io_service::run_one(), io_service::poll(), and io_service::poll_one() is specific to mention handlers, and not operations. In the scenario, the socket::async_connect() operation and deadline_timer::async_wait() operation complete in the same event loop iteration. This results in both handlers being added to the io_service for deferred invocation, in an unspecified order.

    Consider the following snippet that accentuates the scenario:

    void handle_wait(const boost::system::error_code& error)
    {
      if (error) return;
      socket_.close();
    }
    
    timer_.expires_from_now(boost::posix_time::seconds(30));
    timer_.async_wait(&handle_wait);
    socket_.async_connect(endpoint_, handle_connect);
    boost::this_thread::sleep(boost::posix_time::seconds(60));
    io_service_.run_one();
    

    When io_service_.run_one() is invoked, both socket::async_connect() and deadline_timer::async_wait() operations may have completed, causing handle_wait and handle_connect to be ready for invocation from within the io_service in an unspecified order. To properly handle this unspecified order, additional logic need to occur from within handle_wait() and handle_connect() to query the current state, and determine if the other handler has been invoked, rather than depending solely on the status (error_code) of the operation.

    The easiest way to determine if the other handler has invoked is:

    • In handle_connect(), check if the socket is still open via is_open(). If the socket is still open, then handle_timer() has not been invoked. A clean way to indicate to handle_timer() that handle_connect() has ran is to update the expiry time.
    • In handle_timer(), check if the expiry time has passed. If this is true, then handle_connect() has not ran, so close the socket.

    The resulting handlers could look like the following:

    void handle_wait(const boost::system::error_code& error)
    {
      // On error, return early.
      if (error) return;
    
      // If the timer expires in the future, then connect handler must have
      // first.
      if (timer_.expires_at() > deadline_timer::traits_type::now()) return;
    
      // Timeout has occurred, so close the socket.
      socket_.close();
    }
    
    void handle_connect(const boost::system::error_code& error)
    {
      // The async_connect() function automatically opens the socket at the start
      // of the asynchronous operation. If the socket is closed at this time then
      // the timeout handler must have run first.
      if (!socket_.is_open()) return;
    
      // On error, return early.
      if (error) return;
    
      // Otherwise, a connection has been established.  Update the timer state
      // so that the timeout handler does not close the socket.
      timer_.expires_at(boost::posix_time::pos_infin);
    }
    

    Boost.Asio provides some examples for handling timeouts.

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

Sidebar

Related Questions

Can you configure a JBoss web application to be single threaded? Ie: only accept
I have a single-threaded application that uses 3 SQLite databases in 3 different files
Usually in a single threaded application, the main managed object context would reside in
According to Single-Threaded Application with Long-Running Calculation MSDN example, there is a possibility of
I have a single threaded application. If i use below code, i get sched_setscheduler():
I have a shutdown hook handler defined in a SINGLE threaded C application. int
we have a single threaded application that simulates the interaction of a hundred of
I am running function Foo from somebody else's library in a single-threaded application currently.
I noticed that on a single threaded application, SDL still spawns some threads on
I've got a (primarily) single threaded application which updates a swing chart component (jfreechart)

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.