Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixed alive() method.
  • Loading branch information
skasperski committed Feb 4, 2021
commit 9f84305f52c52f6fb3090516fcf8f845fad7522f
54 changes: 9 additions & 45 deletions src/Spawner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Spawner& Spawner::getInstace()
}


Spawner::ProcessHandle::ProcessHandle(Deployment *deploment, bool redirectOutputv, const std::string &logDir) : isRunning(true), deployment(deploment)
Spawner::ProcessHandle::ProcessHandle(Deployment *deploment, bool redirectOutputv, const std::string &logDir) : deployment(deploment)
{
std::string cmd;
std::vector< std::string > args;
Expand Down Expand Up @@ -178,55 +178,19 @@ Spawner::ProcessHandle::ProcessHandle(Deployment *deploment, bool redirectOutput

bool Spawner::ProcessHandle::alive() const
{
//if it was already determined before that the process is already dead,
//we can stop here. Otherwise waitpid would fail!
if(!isRunning){
return isRunning;
}

int status = 0;
pid_t ret = waitpid(pid, &status, WNOHANG);

if(ret < 0 )
{
throw std::runtime_error(std::string("WaitPid failed ") + strerror(errno));
}

if(!status)
{
return isRunning;
}

if(WIFEXITED(status))
{
int exitStatus = WEXITSTATUS(status);
std::cout << "Process " << pid << " terminated normaly, return code " << exitStatus << std::endl;
isRunning = false;
}

if(WIFSIGNALED(status))
{
isRunning = false;

int sigNum = WTERMSIG(status);

if(sigNum == SIGSEGV)
{

std::cout << "Process " << processName << " segfaulted " << std::endl;
}
else
{
std::cout << "Process " << processName << " was terminated by SIG " << sigNum << std::endl;
}

}

return isRunning;
if(ret == 0)
return true;
else if(ret == pid)
return false;
else if(ret == -1 )
throw std::runtime_error(std::string("waitpid failed: ") + strerror(errno));
else
throw std::runtime_error("waitpid returned undocumented value");
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this function has a bug: the name of the function suggests that it's okay to call it on a non-running process with the expected return value false. This behavior was ensured with the previously present isRunning flag.

Whats the behavior now, if we call a process, kill it and then call alive after the collection of the termination signal again. I think the function would not show the expected behavior.

See also comment regarding killAll and handles.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This should not be an issue. When the process just finished, waitpid will return the pid of the exited process, hence alive will just return false. When waitpid returns -1, it means that it could not check the status of the process.




const Deployment& Spawner::ProcessHandle::getDeployment() const
{
return *deployment;
Expand Down
1 change: 0 additions & 1 deletion src/Spawner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Spawner : public boost::noncopyable
public:
class ProcessHandle
{
mutable bool isRunning;
pid_t pid;
void redirectOutput(const std::string &filename);
std::string processName;
Expand Down