8000 naively implement timeout for waitpid (#9943) · waveray/arangodb@844809e · GitHub
[go: up one dir, main page]

Skip to content

Commit 844809e

Browse files
jsteemannKVS85
andcommitted
naively implement timeout for waitpid (arangodb#9943)
Co-authored-by: KVS85 <vadim@arangodb.com>
1 parent ec4a33a commit 844809e

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

lib/Basics/process-utils.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <type_traits>
3535

3636
#include "process-utils.h"
37+
#include "Basics/system-functions.h"
3738

3839
#if defined(TRI_HAVE_MACOS_MEM_STATS)
3940
#include <sys/sysctl.h>
@@ -1029,16 +1030,41 @@ ExternalProcessStatus TRI_CheckExternalProcess(ExternalId pid, bool wait, uint32
10291030

10301031
if (external->_status == TRI_EXT_RUNNING || external->_status == TRI_EXT_STOPPED) {
10311032
#ifndef _WIN32
1032-
int opts;
1033-
int loc = 0;
1033+
if (timeout > 0) {
1034+
// if we use a timeout, it means we cannot use blocking
1035+
wait = false;
1036+
}
10341037

1038+
int opts;
10351039
if (wait) {
10361040
opts = WUNTRACED;
10371041
} else {
10381042
opts = WNOHANG | WUNTRACED;
10391043
}
10401044

1041-
TRI_pid_t res = waitpid(external->_pid, &loc, opts);
1045+
int loc = 0;
1046+
TRI_pid_t res = 0;
1047+
if (timeout) {
1048+
TRI_ASSERT((opts & WNOHANG) != 0);
1049+
double endTime = 0.0;
1050+
while (true) {
1051+
res = waitpid(external->_pid, &loc, opts);
1052+
if (res != 0) {
1053+
break;
1054+
}
1055+
double now = TRI_microtime();
1056+
if (endTime <= 0.000001) {
1057+
// set endtime only once
1058+
endTime = now + timeout / 1000.0;
1059+
} else if (now >= endTime) {
1060+
// timeout
1061+
break;
1062+
}
1063+
std::this_thread::sleep_for(std::chrono::milliseconds(20));
1064+
}
1065+
} else {
1066+
res = waitpid(external->_pid, &loc, opts);
1067+
}
10421068

10431069
if (res == 0) {
10441070
if (wait) {

lib/V8/v8-utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4422,7 +4422,7 @@ static void JS_StatusExternal(v8::FunctionCallbackInfo<v8::Value> const& args) {
44224422
/// @brief executes a external program
44234423
////////////////////////////////////////////////////////////////////////////////
44244424

4425-
static void JS_ExecuteAndWaitExternal(v8::FunctionCallbackInfo<v8::Value> const& args) {
4425+
static void JS_ExecuteExternalAndWait(v8::FunctionCallbackInfo<v8::Value> const& args) {
44264426
TRI_V8_TRY_CATCH_BEGIN(isolate);
44274427
v8::HandleScope scope(isolate);
44284428

@@ -5563,7 +5563,7 @@ void TRI_InitV8Utils(v8::Isolate* isolate, v8::Handle<v8::Context> context,
55635563
JS_ExecuteExternal);
55645564
TRI_AddGlobalFunctionVocbase(
55655565
isolate, TRI_V8_ASCII_STRING(isolate, "SYS_EXECUTE_EXTERNAL_AND_WAIT"),
5566-
JS_ExecuteAndWaitExternal);
5566+
JS_ExecuteExternalAndWait);
55675567
TRI_AddGlobalFunctionVocbase(
55685568
isolate, TRI_V8_ASCII_STRING(isolate, "SYS_GEN_RANDOM_ALPHA_NUMBERS"), JS_RandomAlphaNum);
55695569
TRI_AddGlobalFunctionVocbase(isolate,

0 commit comments

Comments
 (0)
0