qicosmos / rest_rpc

modern C++(C++11), simple, easy to use rpc framework
MIT License
1.66k stars 365 forks source link

core dumped: rpc超时发生core dump #79

Closed wangqiim closed 1 year ago

wangqiim commented 1 year ago

描述:在服务端的rpc中使用sleep模拟rpc处理时间比较长。client的超时时间小于服务器的执行时间。本地稳定复现coredump

cli.out: include/rest_rpc/rpc_client.hpp:618: void rest_rpc::rpc_client::call_back(uint64_t, const error_code&, nonstd::sv_lite::string_view): Assertion `f' failed.
Aborted (core dumped)

server.cpp

#include <rest_rpc.hpp>
using namespace rest_rpc;
using namespace rpc_service;
#include <fstream>
#include <iostream>
#include <chrono>
#include <thread>

#include "qps.h"

void hello(rpc_conn conn, const std::string &str) {
  static thread_local int remote_read_count = 0;
  using namespace std::chrono_literals;
  std::this_thread::sleep_for(5000ms);
  remote_read_count++;
  std::cout << "remote_read_count = " << remote_read_count << std::endl;
}

int main() {
  //  benchmark_test();
  std::cout << "std::thread::hardware_concurrency() = " << std::thread::hardware_concurrency() << std::endl;
  rpc_server server(9000, std::thread::hardware_concurrency());

  server.register_handler("hello", hello);
  server.set_network_err_callback(
      [](std::shared_ptr<connection> conn, std::string reason) {
        std::cout << "remote client address: " << conn->remote_address()
                  << " networking error, reason: " << reason << "\n";
      });

  bool stop = false;
  server.run();
  stop = true;
}

client

#include <chrono>
#include <fstream>
#include <iostream>
#include <rest_rpc.hpp>

using namespace rest_rpc;
using namespace rest_rpc::rpc_service;

void test_connect() {
  rpc_client client;
  client.enable_auto_reconnect(); // automatic reconnect
  client.enable_auto_heartbeat(); // automatic heartbeat
  bool r = client.connect("127.0.0.1", 9000);

  int count = 0;
  while (true) {
    if (client.has_connected()) {
      std::cout << "connected ok\n";
      try {
        client.call<3000>("hello", "purecpp");
      } catch (const std::exception &e) {
        std::cout << e.what() << std::endl;
      }
    } else {
      std::cout << "connected failed: " << count++ << "\n";
    }
    std::this_thread::sleep_for(std::chrono::seconds(1));
  }

}

int main() {
  test_connect();
  return 0;
}
qicosmos commented 1 year ago

做了fix:https://github.com/qicosmos/rest_rpc/commit/accb7d8f184a473a756ba6a951ae43ad43106dd0

你再测试一下看看。