polarismesh / polaris-cpp

Lightweight C++ SDK used as Proxyless Service Governance
Other
34 stars 14 forks source link

多provider情况下Heartbeat会core在inet_ntoa #57

Open Lohnwave opened 1 month ago

Lohnwave commented 1 month ago

我们创建了多个ProviderApi来将实例注册到多个不同的名字服务上。但在运行中,会随机出现core在心跳时解析host上。 看到TryLookup 中使用了非线程安全的inet_ntoa。为什么不适用线程安全的inet_ntop了? std::string target_address = inet_ntoa(*(struct in_addr*)host->h_addr_list[0]);

(gdb) bt

0 0x0000000001e08b8c in polaris::grpc::TryLookup (address=...) at polaris/network/grpc/http2.cpp:425

1 polaris::grpc::Http2Client::ConnectTo(std::string const&, int) () at polaris/network/grpc/http2.cpp:455

2 0x0000000001dbbe54 in polaris::grpc::GrpcClient::ConnectTo (port=, host=..., this=0x7fa36a607c80) at polaris/network/grpc/client.h:192

3 polaris::BlockRequest::PrepareClient() () at polaris/plugin/server_connector/grpc_server_connector.cpp:972

4 0x0000000001dbcdde in polaris::GrpcServerConnector::InstanceHeartbeat (this=, req=..., timeout_ms=)

at polaris/plugin/server_connector/grpc_server_connector.cpp:762

5 0x0000000001e0367b in polaris::ProviderApi::Heartbeat(polaris::InstanceHeartbeatRequest const&) () at polaris/provider/api.cpp:196

6 0x0000000001d84e90 in gxl_utils::PolarisWrapper::KeepHealth() () at gxl-utils/polaris/polaris_wrapper.cc:199

7 0x00000000020f65af in execute_native_thread_routine ()

8 0x00007fc0de8e2e65 in ?? ()

9 0x0000000000000000 in ?? ()

Lohnwave commented 1 month ago

对gethostbyname方法加锁可以解决这个问题,或者换成线程安全的getaddrinfo

static std::string TryLookup(const std::string& address) {
  // ..
  g_mt.lock();
  struct hostent* host = gethostbyname(address.c_str());
  g_mt.unlock();
  // ...