Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

fixes graceful termination when aleth --test is invoked but require… #5146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 44 additions & 1 deletion libweb3jsonrpc/UnixSocketServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
#if !defined(_WIN32)

#include "UnixSocketServer.h"
#include <algorithm>
#include <iostream>
#include <sys/select.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
Expand Down Expand Up @@ -54,16 +57,45 @@ fs::path getIpcPathOrDataDir()
return getDataDir();
return path;
}

/**
Copy link
Member

Choose a reason for hiding this comment

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

We use only // for multiline comments

* Waits for one of the file descriptors to become readable.
* @retval true One file descriptor became available.
* @retval false Most likely an error occured, or a timeout.
*/
static bool waitForReadable(std::initializer_list<int> sockets)
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't need to be declared static as it's in the unnamed namespace

{
fd_set in, out, err;
FD_ZERO(&in);
FD_ZERO(&out);
FD_ZERO(&err);

int const maxfd = *std::max_element(sockets.begin(), sockets.end());
for (int fd : sockets)
FD_SET(fd, &in);

return select(maxfd + 1, &in, &out, &err, nullptr) > 0;
}

}

UnixDomainSocketServer::UnixDomainSocketServer(string const& _appId):
IpcServerBase((getIpcPathOrDataDir() / fs::path(_appId + ".ipc")).string().substr(0, c_socketPathMaxLength))
IpcServerBase((getIpcPathOrDataDir() / fs::path(_appId + ".ipc")).string().substr(0, c_socketPathMaxLength)),
m_exitChannel{-1, -1}
{
if (pipe(m_exitChannel) < 0)
abort(); // I don't like. Replace me with throw.
Copy link
Member

Choose a reason for hiding this comment

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

Throw exception here.
Declare it in IpcServerBase.h as

DEV_SIMPLE_EXCEPTION(FailedToCreatePipe);

Then throw here like

    BOOST_THROW_EXCEPTION(FailedToCreatePipe());


fcntl(m_exitChannel[0], F_SETFL, fcntl(m_exitChannel[0], F_GETFL) | O_NONBLOCK);
fcntl(m_exitChannel[1], F_SETFL, fcntl(m_exitChannel[1], F_GETFL) | O_NONBLOCK);
}

UnixDomainSocketServer::~UnixDomainSocketServer()
{
StopListening();

::close(m_exitChannel[0]);
::close(m_exitChannel[1]);
}

bool UnixDomainSocketServer::StartListening()
Expand Down Expand Up @@ -92,6 +124,10 @@ bool UnixDomainSocketServer::StartListening()

bool UnixDomainSocketServer::StopListening()
{
int dummy = 0x90;
if (::write(m_exitChannel[1], &dummy, sizeof(dummy)) < 0)
cerr << "Failed to notify UNIX domain server loop. " << strerror(errno) << "\n";
Copy link
Member

Choose a reason for hiding this comment

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

Better output it to clog(VerbosityWarn, "rpc"), It will not need \n in the end.


shutdown(m_socket, SHUT_RDWR);
close(m_socket);
m_socket = -1;
Expand All @@ -108,6 +144,13 @@ void UnixDomainSocketServer::Listen()
socklen_t addressLen = sizeof(m_address);
while (m_running)
{
// Block until either m_socket is readable or skip tail and recheck for m_running.
// We do this test before calling accept() in order to gracefully exit
// this function when an external thread has set m_running to true
// (such as a signal handler in the main thread).
if (!waitForReadable({m_socket, m_exitChannel[0]}))
continue;

int connection = accept(m_socket, (sockaddr*) &(m_address), &addressLen);
if (connection > 0)
{
Expand Down
1 change: 1 addition & 0 deletions libweb3jsonrpc/UnixSocketServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class UnixDomainSocketServer : public IpcServerBase<int>
size_t Read(int _connection, void* _data, size_t _size) override;

sockaddr_un m_address;
int m_exitChannel[2];
std::atomic<int> m_socket{0};
};

Expand Down