From: Hwankyu Jhun Date: Wed, 20 Dec 2023 02:23:18 +0000 (+0900) Subject: Handle SIGCHLD event for process-pool X-Git-Tag: accepted/tizen/8.0/unified/20231220.164942~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=85e82ebef574b0b0da98aa1c78ce69c1f3c80d3f;p=platform%2Fcore%2Fappfw%2Flaunchpad.git Handle SIGCHLD event for process-pool If the process pool is terminated, the launchpad should remove the process information. And, if sending the request to the process pool is failed, the launchpad must create a new process to execute an application. Change-Id: I1d5096394593fe3db92fbd125f468bd9b73c498f Signed-off-by: Hwankyu Jhun --- diff --git a/src/launchpad-process-pool/app_executor.cc b/src/launchpad-process-pool/app_executor.cc index 06dda58c..afb2e94e 100644 --- a/src/launchpad-process-pool/app_executor.cc +++ b/src/launchpad-process-pool/app_executor.cc @@ -99,7 +99,9 @@ pid_t AppExecutor::Execute(const AppInfo* app_info) { if (process_pool_->IsPrepared()) { tizen_base::Parcel parcel; parcel.WriteParcelable(*app_info); - return process_pool_->Execute(parcel); + pid_t pid = process_pool_->Execute(parcel); + if (pid > 0) + return pid; } app_info_ = app_info; @@ -111,6 +113,10 @@ void AppExecutor::DisposeCandidateProcess() { process_pool_->SetTimer(); } +void AppExecutor::HandleSigchld(pid_t pid) { + process_pool_->HandleSigchld(pid); +} + void AppExecutor::OnExecution() { UserTracer::Print(std::to_string(getpid()) + "|after calling fork(). " + app_info_->GetAppId()); diff --git a/src/launchpad-process-pool/app_executor.hh b/src/launchpad-process-pool/app_executor.hh index 94b23fad..7fd168e9 100644 --- a/src/launchpad-process-pool/app_executor.hh +++ b/src/launchpad-process-pool/app_executor.hh @@ -40,6 +40,7 @@ class AppExecutor : public Executor::Delegator, pid_t Execute(const AppInfo* app_info); void DisposeCandidateProcess(); + void HandleSigchld(pid_t pid); private: void OnExecution() override; diff --git a/src/launchpad-process-pool/launchpad.cc b/src/launchpad-process-pool/launchpad.cc index 1562f156..c12f04a4 100644 --- a/src/launchpad-process-pool/launchpad.cc +++ b/src/launchpad-process-pool/launchpad.cc @@ -687,6 +687,7 @@ void Launchpad::OnSigchldReceived(pid_t pid) { launchpad::Log::Print("[SIGCHLD]", "pid(%7d)", pid); LoaderManager::GetInst().HandleSigchld(pid); + app_executor_->HandleSigchld(pid); } void Launchpad::OnLoaderPrepared(LoaderContext* loader_context) { diff --git a/src/launchpad-process-pool/loader_executor.cc b/src/launchpad-process-pool/loader_executor.cc index be307ec0..1a23a670 100644 --- a/src/launchpad-process-pool/loader_executor.cc +++ b/src/launchpad-process-pool/loader_executor.cc @@ -70,7 +70,9 @@ pid_t LoaderExecutor::Execute(const LoaderContext* loader_context, b.Add("LOADER_ARGS", loader_argv_); b.Add("LOADER_PRIORITY", std::to_string(priority)); auto parcel = CreateParcelFromBundle(&b); - return process_pool_->Execute(parcel); + pid_t pid = process_pool_->Execute(parcel); + if (pid > 0) + return pid; } return Executor::Execute(priority); @@ -85,6 +87,10 @@ void LoaderExecutor::DisposeCandidateProcess() { process_pool_->SetTimer(); } +void LoaderExecutor::HandleSigchld(pid_t pid) { + process_pool_->HandleSigchld(pid); +} + void LoaderExecutor::OnExecution() { std::vector loader_argv(loader_argv_.size() + 1); int loader_argc = loader_argv_.size(); diff --git a/src/launchpad-process-pool/loader_executor.hh b/src/launchpad-process-pool/loader_executor.hh index 110ffef3..9a7c7080 100644 --- a/src/launchpad-process-pool/loader_executor.hh +++ b/src/launchpad-process-pool/loader_executor.hh @@ -17,6 +17,8 @@ #ifndef LAUNCHPAD_PROCESS_POOL_LOADER_EXECUTOR_HH_ #define LAUNCHPAD_PROCESS_POOL_LOADER_EXECUTOR_HH_ +#include + #include #include #include @@ -41,6 +43,7 @@ class LoaderExecutor : public Executor::Delegator, pid_t Execute(const LoaderContext* loader_context, int priority); bool HasCandidateProcess() const; void DisposeCandidateProcess(); + void HandleSigchld(pid_t pid); private: LoaderExecutor(); diff --git a/src/launchpad-process-pool/loader_manager.cc b/src/launchpad-process-pool/loader_manager.cc index 4ddee1c9..472d5568 100644 --- a/src/launchpad-process-pool/loader_manager.cc +++ b/src/launchpad-process-pool/loader_manager.cc @@ -93,6 +93,7 @@ void LoaderManager::HandleSigchld(pid_t pid) { } RemoveLoaderContextsByCallerPid(pid); + LoaderExecutor::GetInst().HandleSigchld(pid); } void LoaderManager::AddDefaultLoaderContexts() { diff --git a/src/launchpad-process-pool/process_pool.cc b/src/launchpad-process-pool/process_pool.cc index 82456cc9..81a5c05b 100644 --- a/src/launchpad-process-pool/process_pool.cc +++ b/src/launchpad-process-pool/process_pool.cc @@ -102,22 +102,38 @@ pid_t ProcessPool::Execute(const tizen_base::Parcel& parcel) { return -1; auto process = std::move(queue_.front()); - queue_.pop(); - process->Send(parcel); + queue_.erase(queue_.begin()); + if (process->Send(parcel) < 0) + return -1; + return process->GetPid(); } void ProcessPool::Dispose() { - while (!queue_.empty()) { - auto process = std::move(queue_.front()); - queue_.pop(); + for (auto& process : queue_) { process->Kill(); _D("Kill process(%d)", process->GetPid()); } - + queue_.clear(); UnsetTimer(); } +void ProcessPool::HandleSigchld(pid_t pid) { + auto iter = queue_.begin(); + while (iter != queue_.end()) { + if ((*iter)->GetPid() == pid) { + iter = queue_.erase(iter); + break; + } + + iter++; + } + + int current_process_count = static_cast(queue_.size()); + if (current_process_count != num_processes_) + SetTimer(); +} + ProcessPool::Process::Process(pid_t pid, int fd) : pid_(pid), socket_(new Socket(fd)) { } @@ -186,7 +202,7 @@ void ProcessPool::PrepareProcess() { } close(pipe_fd_[0]); - queue_.push(std::make_shared(pid, pipe_fd_[1])); + queue_.push_back(std::make_shared(pid, pipe_fd_[1])); } } diff --git a/src/launchpad-process-pool/process_pool.hh b/src/launchpad-process-pool/process_pool.hh index d38e3272..7d159c2c 100644 --- a/src/launchpad-process-pool/process_pool.hh +++ b/src/launchpad-process-pool/process_pool.hh @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -48,6 +47,7 @@ class ProcessPool : public Executor::Delegator, bool IsPrepared() const; pid_t Execute(const tizen_base::Parcel& parcel); void Dispose(); + void HandleSigchld(pid_t pid); void SetTimer(); private: @@ -75,7 +75,7 @@ class ProcessPool : public Executor::Delegator, int num_processes_; IEvent* event_listener_; int pipe_fd_[2] = { -1, -1 }; - std::queue> queue_; + std::vector> queue_; guint timer_ = 0; };