This repository was archived by the owner on Oct 28, 2021. It is now read-only.
  
  
  - 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.2k
 
          fixes graceful termination when aleth --test is invoked but require…
          #5146
        
      
          
     Open
      
      
            christianparpart
  wants to merge
  3
  commits into
  ethereum:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
christianparpart:cp-fix-graceful-termination
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -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> | ||
| 
          
            
          
           | 
    @@ -54,16 +57,45 @@ fs::path getIpcPathOrDataDir() | |
| return getDataDir(); | ||
| return path; | ||
| } | ||
| 
     | 
||
| /** | ||
| * 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) | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't need to be declared   | 
||
| { | ||
| 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. | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throw exception here. Then throw here like  | 
||
| 
     | 
||
| 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() | ||
| 
          
            
          
           | 
    @@ -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"; | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better output it to   | 
||
| 
     | 
||
| shutdown(m_socket, SHUT_RDWR); | ||
| close(m_socket); | ||
| m_socket = -1; | ||
| 
        
          
        
         | 
    @@ -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) | ||
| { | ||
| 
          
            
          
           | 
    ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
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