Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix worker unexpected shutdown. #42

Merged
merged 1 commit into from Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/module.rs
Expand Up @@ -17,7 +17,7 @@ use crate::{
channel::Reporter,
execute::register_execute_functions,
util::{get_sapi_module_name, IPS},
worker::{init_worker, shutdown_worker},
worker::init_worker,
SKYWALKING_AGENT_ENABLE, SKYWALKING_AGENT_LOG_FILE, SKYWALKING_AGENT_LOG_LEVEL,
SKYWALKING_AGENT_SERVICE_NAME, SKYWALKING_AGENT_SKYWALKING_VERSION,
};
Expand Down Expand Up @@ -84,9 +84,7 @@ pub fn init() {
register_execute_functions();
}

pub fn shutdown() {
shutdown_worker();
}
pub fn shutdown() {}

fn init_logger() {
let log_level = ini_get::<Option<&CStr>>(SKYWALKING_AGENT_LOG_LEVEL)
Expand Down
21 changes: 6 additions & 15 deletions src/worker.rs
Expand Up @@ -17,7 +17,7 @@ use crate::{
channel, module::SOCKET_FILE_PATH, util::change_permission, SKYWALKING_AGENT_SERVER_ADDR,
SKYWALKING_AGENT_WORKER_THREADS,
};
use once_cell::sync::{Lazy, OnceCell};
use once_cell::sync::Lazy;
use phper::ini::ini_get;
use skywalking::reporter::{
grpc::{CollectItemConsume, GrpcReporter},
Expand All @@ -41,8 +41,6 @@ use tonic::{
};
use tracing::{debug, error, info, warn};

static WORKER_PID: OnceCell<libc::pid_t> = OnceCell::new();

pub fn init_worker() {
let server_addr = ini_get::<Option<&CStr>>(SKYWALKING_AGENT_SERVER_ADDR)
.and_then(|s| s.to_str().ok())
Expand All @@ -61,22 +59,15 @@ pub fn init_worker() {
error!("fork failed");
}
Ordering::Equal => {
// Ensure worker process exits when master process exists.
#[cfg(target_os = "linux")]
libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM);

let rt = new_tokio_runtime(worker_threads);
rt.block_on(start_worker(server_addr));
exit(0);
}
Ordering::Greater => {
WORKER_PID.set(pid).unwrap();
}
}
}
}

pub fn shutdown_worker() {
if let Some(pid) = WORKER_PID.get() {
debug!(pid, "Start to shutdown worker");
unsafe {
libc::kill(*pid, libc::SIGTERM);
Ordering::Greater => {}
}
}
}
Expand Down