Skip to content

Commit

Permalink
Feature: When accepting a connection, if the created file descriptor …
Browse files Browse the repository at this point in the history
…(FD) is 0, 1, or 2, avoid core dump (#129)
  • Loading branch information
yujun411522 authored May 6, 2024
1 parent 5100aa1 commit 5c32e3f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions trpc/runtime/iomodel/reactor/default/tcp_acceptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ int TcpAcceptor::HandleReadEvent() {
NetworkAddress peer_addr;
int conn_fd = socket_.Accept(&peer_addr);
if (conn_fd >= 0) {
// When Accept returns standard input(0),standard output(1),and standard error(2), just only log
if (conn_fd < 3) {
TRPC_LOG_WARN("Accept return std fd,conn_fd:" << conn_fd);
}
AcceptConnectionFunction& accept_handler = GetAcceptHandleFunction();
if (accept_handler) {
AcceptConnectionInfo info;
Expand Down
4 changes: 4 additions & 0 deletions trpc/runtime/iomodel/reactor/fiber/fiber_acceptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ FiberConnection::EventAction FiberAcceptor::OnTcpReadable() {
NetworkAddress peer_addr;
int conn_fd = socket_.Accept(&peer_addr);
if (conn_fd >= 0) {
// When Accept returns standard input(0),standard output(1),and standard error(2), just only log
if (conn_fd < 3) {
TRPC_LOG_WARN("Accept return std fd,conn_fd:" << conn_fd);
}
if (accept_handler_) {
AcceptConnectionInfo info;

Expand Down
6 changes: 3 additions & 3 deletions trpc/runtime/iomodel/reactor/fiber/fiber_reactor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ void TerminateAllReactor() {
}

Reactor* GetReactor(std::size_t scheduling_group, int fd) {
TRPC_CHECK(fd != 0 && fd != -1,
"You're likely passing in a fd got from calling `Get()` on an "
"invalid `Handle`.");
TRPC_CHECK(fd != -1,
"You're likely passing in a fd got from calling `Get()` on an "
"invalid `Handle`.");
if (fd == -2) {
fd = Random<int>();
}
Expand Down

0 comments on commit 5c32e3f

Please sign in to comment.