From 260d87c6eb79c32baa91b4f168809c11c2039c93 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Mon, 16 Nov 2026 15:01:34 +0900 Subject: [PATCH] Fix a bug that the server process may call kill(-1, SIGUSR1) Closes GH-228 `kill(-1, ...)` sends the signal to all processes that the calling process has permission to send signals to. So tests were sometimes killed by `SIGUSR1`. The server process copies `session->executorPID` to `localSession->peerPID` when `session->initialized` is `true`. But `session->executorPID` was set only by the main process after `WaitForBackgroundWorkerStartup()`. If the executor process set `session->initialized = true` before it, the server process copied `InvalidPid` (`-1`) and used it for `kill()`. The executor process sets `session->executorPID` by itself before it sets `session->initialized = true` with this change. This also adds `Processor::signal_process()` that does nothing for `InvalidPid` and uses it for `kill()` calls that didn't check `InvalidPid`. Co-Authored-By: Claude Fable 5.2 --- src/afs.cc | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/afs.cc b/src/afs.cc index db83d8f..c2fd671 100644 --- a/src/afs.cc +++ b/src/afs.cc @@ -637,6 +637,17 @@ class Processor { return 0; } + // kill(-1, ...) sends the signal to all processes that we can send + // signals to. So we must not call kill() with InvalidPid (-1). + void signal_process(pid_t pid) + { + if (ARROW_PREDICT_FALSE(pid == InvalidPid)) + { + return; + } + kill(pid, SIGUSR1); + } + arrow::Status wait(LocalSessionData* session, SharedRingBuffer* buffer, WaitMode mode) { const char* tag = (mode == WaitMode::Read) ? "wait read" : "wait written"; @@ -1493,6 +1504,11 @@ class Executor : public WorkerProcessor { // sleep(5); pgstat_report_activity(STATE_RUNNING, (std::string(Tag) + ": opening").c_str()); auto session = find_session(); + // The main process also sets this after + // WaitForBackgroundWorkerStartup() but the server process may + // refer to this before that. So we set this here before we set + // session->initialized. + session->executorPID = MyProcPid; PG_TRY(); { auto databaseName = @@ -2713,7 +2729,7 @@ class Proxy : public WorkerProcessor { session->action = Action::Select; } P("%s: %s: %s: kill executor: %d", Tag, tag_, tag, executorPID); - kill(executorPID, SIGUSR1); + signal_process(executorPID); } selectRequests_.clear(); } @@ -2849,7 +2865,7 @@ class Proxy : public WorkerProcessor { session->nUpdatedRecords = -1; } P("%s: %s: %s: kill executor: %d", Tag, tag_, tag, executorPID); - kill(executorPID, SIGUSR1); + signal_process(executorPID); request->processing = true; ++it; } @@ -2966,7 +2982,7 @@ class Proxy : public WorkerProcessor { set_shared_string(session->preparedStatementHandle, std::string("")); } P("%s: %s: %s: kill executor: %d", Tag, tag_, tag, executorPID); - kill(executorPID, SIGUSR1); + signal_process(executorPID); request->processing = true; ++it; } @@ -3086,7 +3102,7 @@ class Proxy : public WorkerProcessor { session->action = Action::ClosePreparedStatement; } P("%s: %s: %s: kill executor: %d", Tag, tag_, tag, executorPID); - kill(executorPID, SIGUSR1); + signal_process(executorPID); request->processing = true; ++it; } @@ -3201,7 +3217,7 @@ class Proxy : public WorkerProcessor { session->setParametersFinished = false; } P("%s: %s: %s: kill executor: %d", Tag, tag_, tag, executorPID); - kill(executorPID, SIGUSR1); + signal_process(executorPID); request->processing = true; ++it; } @@ -3243,7 +3259,7 @@ class Proxy : public WorkerProcessor { } ARROW_RETURN_NOT_OK(writer->Close()); P("%s: %s: %s: kill executor: %d", Tag, tag_, tag, executorPID); - kill(executorPID, SIGUSR1); + signal_process(executorPID); } { std::unique_lock lock(mutex_); @@ -3312,7 +3328,7 @@ class Proxy : public WorkerProcessor { session->action = Action::SelectPreparedStatement; } P("%s: %s: %s: kill executor: %d", Tag, tag_, tag, executorPID); - kill(executorPID, SIGUSR1); + signal_process(executorPID); } selectPreparedStatementRequests_.clear(); } @@ -3430,7 +3446,7 @@ class Proxy : public WorkerProcessor { session->nUpdatedRecords = -1; } P("%s: %s: %s: kill executor: %d", Tag, tag_, tag, executorPID); - kill(executorPID, SIGUSR1); + signal_process(executorPID); request->processing = true; ++it; } @@ -3473,7 +3489,7 @@ class Proxy : public WorkerProcessor { } ARROW_RETURN_NOT_OK(writer->Close()); P("%s: %s: %s: kill executor: %d", Tag, tag_, tag, executorPID); - kill(executorPID, SIGUSR1); + signal_process(executorPID); } { std::unique_lock lock(mutex_); @@ -3541,7 +3557,7 @@ class Proxy : public WorkerProcessor { tag_, tag, request->localSession->peerPID); - kill(request->localSession->peerPID, SIGUSR1); + signal_process(request->localSession->peerPID); } readRequests_.clear(); } @@ -3639,7 +3655,7 @@ class Proxy : public WorkerProcessor { tag_, tag, request->localSession->peerPID); - kill(request->localSession->peerPID, SIGUSR1); + signal_process(request->localSession->peerPID); } writeRequests_.clear(); } @@ -3933,7 +3949,7 @@ class MainProcessor : public Processor { } dshash_seq_term(&sessionsStatus); P("%s: %s: %s: end", Tag, tag_, tag); - kill(sharedData_->serverPID, SIGUSR1); + signal_process(sharedData_->serverPID); } dshash_table* sessions_;