diff --git a/clean.sh b/clean.sh index 3d4ee533..9f74221f 100755 --- a/clean.sh +++ b/clean.sh @@ -1,3 +1,7 @@ #! /bin/bash bazel clean --expunge + +# Delete xx.log or xx.bin on root directory of trpc-cpp project. +find ./ -maxdepth 1 -name "*.log" -exec rm {} \; +find ./ -maxdepth 1 -name "*.bin" -exec rm {} \; diff --git a/trpc/runtime/iomodel/reactor/common/io_handler.h b/trpc/runtime/iomodel/reactor/common/io_handler.h index 28875d0e..2e07e816 100644 --- a/trpc/runtime/iomodel/reactor/common/io_handler.h +++ b/trpc/runtime/iomodel/reactor/common/io_handler.h @@ -45,6 +45,9 @@ class IoHandler { /// @brief write data to the connection virtual int Writev(const iovec* iov, int iovcnt) = 0; + + /// @brief Destroy IO handler. + virtual void Destroy() {} }; } // namespace trpc diff --git a/trpc/runtime/iomodel/reactor/default/tcp_connection.cc b/trpc/runtime/iomodel/reactor/default/tcp_connection.cc index 2e913e83..99f2868a 100644 --- a/trpc/runtime/iomodel/reactor/default/tcp_connection.cc +++ b/trpc/runtime/iomodel/reactor/default/tcp_connection.cc @@ -101,6 +101,7 @@ bool TcpConnection::DoConnect() { << ", failed. error no: " << errno << ", msg: " << strerror(errno)); + GetIoHandler()->Destroy(); socket_.Close(); HandleClose(false); @@ -353,6 +354,9 @@ void TcpConnection::HandleClose(bool destroy) { } io_msgs_.clear(); + // Requirements: destroy IO-handler before close socket. + GetIoHandler()->Destroy(); + DisableReadWrite(); handshake_status_ = IoHandler::HandshakeStatus::kFailed; diff --git a/trpc/runtime/iomodel/reactor/fiber/fiber_tcp_connection.cc b/trpc/runtime/iomodel/reactor/fiber/fiber_tcp_connection.cc index 555e463b..00ebde16 100644 --- a/trpc/runtime/iomodel/reactor/fiber/fiber_tcp_connection.cc +++ b/trpc/runtime/iomodel/reactor/fiber/fiber_tcp_connection.cc @@ -31,6 +31,10 @@ FiberTcpConnection::FiberTcpConnection(Reactor* reactor, const Socket& socket) } FiberTcpConnection::~FiberTcpConnection() { + // Requirements: destroy IO-handler before close socket. + GetIoHandler()->Destroy(); + socket_.Close(); + TRPC_LOG_DEBUG("~FiberTcpConnection fd:" << socket_.GetFd() << ", conn_id:" << this->GetConnId()); TRPC_ASSERT(!socket_.IsValid()); } @@ -77,6 +81,7 @@ bool FiberTcpConnection::DoConnect() { << ", is_client:" << IsClient() << ", conn_id: " << this->GetConnId() << ", failed."); + GetIoHandler()->Destroy(); socket_.Close(); return false; @@ -361,7 +366,7 @@ void FiberTcpConnection::OnCleanup(CleanupReason reason) { writing_buffers_.Stop(); - socket_.Close(); + // For multi-threads-safety, move "socket_.Close()" to ~FiberTcpConnection(); } IoHandler::HandshakeStatus FiberTcpConnection::DoHandshake(bool from_on_readable) { diff --git a/trpc/transport/common/ssl/ssl.cc b/trpc/transport/common/ssl/ssl.cc index 495798c5..1f62850f 100644 --- a/trpc/transport/common/ssl/ssl.cc +++ b/trpc/transport/common/ssl/ssl.cc @@ -679,6 +679,7 @@ Ssl::~Ssl() { if (ssl_) { SSL_shutdown(ssl_); SSL_free(ssl_); + ssl_ = nullptr; } } diff --git a/trpc/transport/common/ssl_io_handler.cc b/trpc/transport/common/ssl_io_handler.cc index a38d79f2..115e3040 100644 --- a/trpc/transport/common/ssl_io_handler.cc +++ b/trpc/transport/common/ssl_io_handler.cc @@ -76,6 +76,14 @@ int SslIoHandler::Writev(const struct iovec* iov, int iovcnt) { return n; } +void SslIoHandler::Destroy() { + if (ssl_) { + ssl_->Shutdown(); + ssl_ = nullptr; + handshaked_ = false; + } +} + } // namespace trpc::ssl #endif diff --git a/trpc/transport/common/ssl_io_handler.h b/trpc/transport/common/ssl_io_handler.h index 14d6a0d2..76c57be9 100644 --- a/trpc/transport/common/ssl_io_handler.h +++ b/trpc/transport/common/ssl_io_handler.h @@ -35,6 +35,8 @@ class SslIoHandler : public IoHandler { int Writev(const struct iovec* iov, int iovcnt) override; + void Destroy() override; + private: Connection* conn_{nullptr}; SslPtr ssl_{nullptr};