From d1c4d7ab62094e9e0c34db4568cf884c93a2043a Mon Sep 17 00:00:00 2001 From: Gavin Halliday Date: Tue, 26 Nov 2024 12:06:04 +0000 Subject: [PATCH] HPCC-33031 Add the option to terminate a child process gracefully Signed-off-by: Gavin Halliday --- deployment/deploy/DeployTask.cpp | 2 +- ecl/eclccserver/eclccserver.cpp | 4 ++-- system/jlib/jthread.cpp | 9 +++++---- system/jlib/jthread.hpp | 2 +- thorlcr/activities/piperead/thprslave.cpp | 2 +- thorlcr/activities/pipewrite/thpwslave.cpp | 2 +- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/deployment/deploy/DeployTask.cpp b/deployment/deploy/DeployTask.cpp index 0294aeb8ee8..53be5dc60e8 100644 --- a/deployment/deploy/DeployTask.cpp +++ b/deployment/deploy/DeployTask.cpp @@ -1635,7 +1635,7 @@ class CDeployTask : public CInterface, implements IDeployTask { retcode = 5; errbuf.clear().append("Invalid or missing key passphrase. "); - pipe->abort(); + pipe->abort(false); return retcode; } diff --git a/ecl/eclccserver/eclccserver.cpp b/ecl/eclccserver/eclccserver.cpp index 34368e5aceb..8d8a91c94df 100644 --- a/ecl/eclccserver/eclccserver.cpp +++ b/ecl/eclccserver/eclccserver.cpp @@ -162,7 +162,7 @@ class AbortWaiter : public Thread ForEachItemIn(idx, pipes) { IPipeProcess *pipe = pipes.item(idx); - pipe->abort(); + pipe->abort(true); } break; } @@ -187,7 +187,7 @@ class AbortWaiter : public Thread CriticalBlock b(crit); pipes.append(LINK(pipe)); if (timedOut) - pipe->abort(); + pipe->abort(true); } void removePipe(IPipeProcess *pipe) { diff --git a/system/jlib/jthread.cpp b/system/jlib/jthread.cpp index 8545d2bd641..cafdfadc3b3 100644 --- a/system/jlib/jthread.cpp +++ b/system/jlib/jthread.cpp @@ -1690,7 +1690,7 @@ class CWindowsPipeProcess: implements IPipeProcess, public CInterface doCloseError(); } - void abort() + void abort(bool gracefull) override { CriticalBlock block(sect); if (pipeProcess != (HANDLE)-1) { @@ -2416,7 +2416,7 @@ protected: friend class PipeWriterThread; } } - void abort() + void abort(bool graceful) override { CriticalBlock block(sect); aborted = true; @@ -2432,10 +2432,11 @@ protected: friend class PipeWriterThread; if (pipeProcess != (HANDLE)-1) { if (title.length()) PROGLOG("%s: Forcibly killing pipe process %d",title.get(),pipeProcess); + int signalToSend = graceful ? SIGTERM : SIGKILL; if (newProcessGroup) - ::kill(-pipeProcess,SIGKILL); + ::kill(-pipeProcess,signalToSend); else - ::kill(pipeProcess,SIGKILL); // if this doesn't kill it we are in trouble + ::kill(pipeProcess,signalToSend); // if this doesn't kill it we are in trouble CriticalUnblock unblock(sect); wait(); } diff --git a/system/jlib/jthread.hpp b/system/jlib/jthread.hpp index 5d312aa9d2f..99a8cac362a 100644 --- a/system/jlib/jthread.hpp +++ b/system/jlib/jthread.hpp @@ -338,7 +338,7 @@ interface IPipeProcess: extends IInterface virtual void closeInput() = 0; // indicate finished input to pipe virtual void closeOutput() = 0; // indicate finished reading from pipe (generally called automatically) virtual void closeError() = 0; // indicate finished reading from pipe stderr - virtual void abort() = 0; + virtual void abort(bool gracefull) = 0; // graceful=true allows the child process to clean up. virtual void notifyTerminated(HANDLE pid,unsigned retcode) = 0; // internal virtual HANDLE getProcessHandle() = 0; // used to auto kill virtual void setenv(const char *var, const char *value) = 0; // Set a value to be passed in the called process environment diff --git a/thorlcr/activities/piperead/thprslave.cpp b/thorlcr/activities/piperead/thprslave.cpp index 400a27797c0..d185948dc80 100644 --- a/thorlcr/activities/piperead/thprslave.cpp +++ b/thorlcr/activities/piperead/thprslave.cpp @@ -164,7 +164,7 @@ class CPipeSlaveBase : public CSlaveActivity void abortPipe() { unregisterSelfDestructChildProcess(pipe->getProcessHandle()); - pipe->abort(); + pipe->abort(false); } public: diff --git a/thorlcr/activities/pipewrite/thpwslave.cpp b/thorlcr/activities/pipewrite/thpwslave.cpp index 7a4e9d431cf..63f26b6a83a 100644 --- a/thorlcr/activities/pipewrite/thpwslave.cpp +++ b/thorlcr/activities/pipewrite/thpwslave.cpp @@ -64,7 +64,7 @@ class CPipeWriteSlaveActivity : public ProcessSlaveActivity if (pipeOpen) { unregisterSelfDestructChildProcess(pipe->getProcessHandle()); - pipe->abort(); + pipe->abort(false); } ProcessSlaveActivity::abort(); }