Skip to content

Asio SSL client timeout/error-handling patch. Make POSIX signal-friendly interface.#356

Open
officer-34762 wants to merge 2 commits into
reo7sp:masterfrom
officer-34762:master
Open

Asio SSL client timeout/error-handling patch. Make POSIX signal-friendly interface.#356
officer-34762 wants to merge 2 commits into
reo7sp:masterfrom
officer-34762:master

Conversation

@officer-34762

Copy link
Copy Markdown

Hi. First of all, I updated ssl client using boost asio to handle timeout of read-write/connect more carefully also throwing detailed errors. Second, launch_once function was created to customize limit/timeout values and allowed notify types array without new object creation. Instead of creating TgLongPoll object every time so as to change values of arguments start function in an every cycle of a loop i can make launch_once call with another values.
Next you got like this in your samples:

    signal(SIGINT, [](int s) {
        printf("SIGINT got\n");
        exit(0); // It must be '_exit(0)'
    });

I have to get special processing on signal fired so i can't just do what i would want and call exit(0) because it's recommended to use async thread safe only functions in such handlers. And more, long_poll.start() might freeze if i get unstable network connection and an application receive a signal. This is my walkthrough of how I achieved this:

std::atomic<int> g_received_signal{0};

void interrupt_handler(int signal) {
  g_received_signal = signal;
}

// in main func
signal(SIGINT, interrupt_handler);
signal(SIGTERM, interrupt_handler);
signal(SIGHUP, force_reconf_handler);

int received_signal = 0;
TgBot::TgLongPoll long_poll(bot);
  while (received_signal = g_received_signal.load(), !exit_tg(received_signal)) {
    try {
      if(1/*trigger*/)
         long_poll.launch_once(100, 0, {});
      else // Something specific happened wherever
         long_poll.launch_once(1000, 50, {"inline_query", "callback_query"/*, etc*/});
    } catch (const std::exception &e) {
      spdlog::error("Error in long poll: {}", e.what());
    }
  }

// Some important things should be finished here instead doing it in signal handler
return EXIT_SUCCESS; // or exit(0)

I tried to save support of Asio library from Boost 1.65.1 version. Though i didn't check working it!

reo7sp
reo7sp previously requested changes Jul 13, 2026

@reo7sp reo7sp left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several blocking correctness issues in the current implementation:

  1. The minimum supported Boost 1.65.1 build does not compile.
  2. DNS resolution is still synchronous and outside the timeout/cancellation path.
  3. launch_once() does not make its timeout effective at the HTTP transport layer.

The public API shown in the PR description also cannot be called with the provided initializer-list example. Please address these points and add coverage for the minimum Boost version and timeout paths.

boost::asio::steady_timer timer(io_context_);
timer.expires_from_now(dur);

bool timed_out = false;

@reo7sp reo7sp Jul 13, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dur is undefined, so the Boost 1.65.1 build fails. Did you mean timeout?

@reo7sp reo7sp dismissed their stale review July 13, 2026 18:17

Replacing with shorter, line-specific comments.

@reo7sp reo7sp left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the marked lines.

boost::asio::steady_timer timer(io_context_);
timer.expires_from_now(dur);

bool timed_out = false;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dur is undefined. This does not compile with Boost < 1.66. Use timeout here.

Comment thread src/net/TgLongPoll.cpp
// confirm handled updates
_updates = _api->getUpdates(_lastUpdateId, limit, timeout, allowUpdates);
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout is passed to Telegram, but the HTTP client still uses the timeout set in the constructor. Update _httpClient._timeout here too, otherwise launch_once(..., 50, ...) can still fail after the old timeout.

void start();
void launch_once(std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowUpdates);

private:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example from the PR description cannot call this with {"inline_query", "callback_query"}. Consider accepting std::vector<std::string> or provide an overload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants