Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use spider_execution_manager::client::grpc::GrpcStorageClient;
use spider_execution_manager::runtime::Runtime;
use spider_utils::config::YamlConfig;
use spider_utils::logging::set_up_logging;
use tokio::signal::unix::SignalKind;
use tokio::signal::unix::signal;

/// Command-line arguments for the execution manager.
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -63,6 +65,9 @@ async fn main() -> Result<(), Box<dyn Error>> {

tracing::info!(em_id, "Execution manager started.");
let mut run_handle = tokio::spawn(runtime.run());
let mut sigterm = signal(SignalKind::terminate()).inspect_err(
|error| tracing::error!(em_id, error = % error, "Failed to register SIGTERM handler."),
)?;
Comment on lines +68 to +70

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== repository files =="
git ls-files | rg '(^|/)execution_manager\.rs$|components/spider-(scheduler|storage)/src/bin/grpc_server\.rs$' || true

echo
echo "== relevant files =="
for f in \
  components/spider-execution-manager/src/bin/execution_manager.rs \
  components/spider-scheduler/src/bin/grpc_server.rs \
  components/spider-storage/src/bin/grpc_server.rs
do
  if [ -f "$f" ]; then
    echo "--- $f ($(wc -l < "$f") lines) ---"
    sed -n '1,140p' "$f" | nl -ba
  fi
done

echo
echo "== signal crate/docs dependency annotations =="
for f in Cargo.toml cargo.lock components/*/Cargo.toml components/**/Cargo.lock; do
  if [ -f "$f" ]; then
    if rg -n 'tokio|tokio-signal|signal|ctrlc|async-signal' "$f" >/dev/null 2>&1; then
      echo "--- $f ---"
      rg -n 'tokio|signal|ctrlc|async-signal' "$f" || true
    fi
  fi
done

echo
echo "== signal registration occurrences =="
rg -n 'signal\(SignalKind::terminate\)|signal\(|SignalKind::terminate|tokio::signal' .

Repository: y-scope/spider

Length of output: 487


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== lockfiles for tokio signal implementation snippets by package extraction =="
python3 - <<'PY'
from pathlib import Path
for path in Path('.').rglob('Cargo.lock'):
    if any(part in {'.git', 'target'} for part in path.parts):
        continue
    text = path.read_text(errors='ignore')
    idx = text.find('package = "tokio"')
    if idx != -1:
        print(f'--- {path} tokio package start: line={text[:idx].count(chr(10))+1} ---')
        for line_no, line in enumerate(text.splitlines(), 1):
            if 'version = ' in line or 'source = ' in line or 'dependencies' in line:
                print(f'{line_no}: {line}')
PY

Repository: y-scope/spider

Length of output: 231


🌐 Web query:

Tokio signal Unix install signal handler when created documentation

💡 Result:

In Tokio, signal handlers are installed lazily [1][2]. When you create a new tokio::signal::unix::Signal instance for a specific signal kind, the underlying OS signal handler is installed the first time that signal is registered for the process [3][1]. Key characteristics of this mechanism include: 1. Process-wide Scope: Once the signal handler is installed for a specific signal kind, it replaces the default platform behavior (SIG_DFL) for the duration of the entire process [3][1]. 2. Persistence: Dropping the Signal instance does not restore the original default signal handler [3][4]. Because signals are a global resource, Tokio's handler will continue to intercept that signal even after the listener is dropped [3][4]. If no active listeners are registered when a signal is subsequently received, the signal may be silently consumed by Tokio without the default system action (such as process termination) taking effect [4]. 3. Implementation Detail: Internally, Tokio uses a "self-pipe" pattern (typically using a UnixStream pair) to bridge the signal handler to the async reactor [5]. This allows the signal handler to remain minimal and safe while notifying the asynchronous runtime that a signal event has occurred [5][4]. Best practices for handling these behaviors include: - Keep Signal listeners active throughout the lifecycle of your application if you intend to catch those signals, or manage the signal state manually if you must drop them [4]. - For graceful shutdowns, you can use tokio::select! to monitor for signals alongside other application tasks [6][7]. - Be aware that if your application integrates with other libraries or languages (like Go) that also register signal handlers, conflicts may occur if the handlers are not configured compatibly [2]. For most use cases, creating a Signal instance via tokio::signal::unix::signal(SignalKind::...) is the standard approach to receiving asynchronous notifications for OS signals [8][9].

Citations:


🌐 Web query:

docs Tokio tokio::signal::unix::Signal installs Unix handler only when created manual

💡 Result:

The statement that Tokio's Unix signal handler is only installed when created manually is correct in the sense that the registration process is triggered lazily upon the creation of a Signal listener [1][2]. Specifically, the behavior works as follows: 1. Lazy Installation: The underlying OS signal handler is not installed globally when the Tokio runtime starts. Instead, it is registered the first time a Signal instance (or a related helper like ctrl_c) is created for a specific SignalKind [1][3]. 2. Persistence: Once a signal handler has been registered for a particular signal kind, it remains installed for the duration of the entire process [1][4]. The underlying libc signal handler is never unregistered, even if the Signal instance that triggered the initial registration is dropped [1][5]. 3. Impact: By registering a handler, Tokio effectively takes over the handling of that signal, replacing the default platform behavior (e.g., terminating the process on SIGINT) with the capability to translate that signal into an asynchronous stream event [1][3]. Subsequent deliveries of that signal will continue to be captured by Tokio, and the process's default behavior will not be restored even if the original Signal listener is destroyed [1][3]. In summary, you must explicitly create a listener to "opt-in" to handling a specific Unix signal within your application, at which point the global handler for that signal becomes active for the remainder of the process's lifecycle [1][4].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

for f in \
  components/spider-execution-manager/src/bin/execution_manager.rs \
  components/spider-scheduler/src/bin/grpc_server.rs \
  components/spider-storage/src/bin/grpc_server.rs
do
  echo "--- $f ($(wc -l < "$f") lines) ---"
  awk '{printf "%5d\t%s\n", NR, $0}' "$f" | sed -n '1,160p'
done

echo
echo "== signal registration occurrences =="
rg -n 'signal\(SignalKind::terminate\)|signal\(|SignalKind::terminate|tokio::signal' .

echo
echo "== root Cargo.toml signal dependency excerpts =="
for f in Cargo.toml components/spider-execution-manager/Cargo.toml components/spider-scheduler/Cargo.toml components/spider-storage/Cargo.toml; do
  if [ -f "$f" ]; then
    echo "--- $f ---"
    rg -n 'tokio|signal|ctrlc' "$f" || true
  fi
done

Repository: y-scope/spider

Length of output: 17460


Install all SIGTERM handlers before startup work begins.

Tokio only installs the Unix handler when signal(...)/ctrl_c() is first registered, so before those calls SIGTERM keeps its default termination behaviour.

  • components/spider-execution-manager/src/bin/execution_manager.rs#L68-L70: register before creating the runtime or spawning runtime.run().
  • components/spider-scheduler/src/bin/grpc_server.rs#L63-L65: register before config/storage/runtime/listener setup.
  • components/spider-storage/src/bin/grpc_server.rs#L58-L60: register before config/runtime/listener/server setup.
📍 Affects 3 files
  • components/spider-execution-manager/src/bin/execution_manager.rs#L68-L70 (this comment)
  • components/spider-scheduler/src/bin/grpc_server.rs#L63-L65
  • components/spider-storage/src/bin/grpc_server.rs#L58-L60
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@components/spider-execution-manager/src/bin/execution_manager.rs` around
lines 68 - 70, Register the SIGTERM handler before any startup work: in
components/spider-execution-manager/src/bin/execution_manager.rs lines 68-70,
move signal(SignalKind::terminate()) before runtime creation or spawning
runtime.run(); in components/spider-scheduler/src/bin/grpc_server.rs lines
63-65, move registration before config, storage, runtime, and listener setup;
and in components/spider-storage/src/bin/grpc_server.rs lines 58-60, move it
before config, runtime, listener, and server setup. Preserve the existing error
logging and handler behavior.


let () = tokio::select! {
result = tokio::signal::ctrl_c() => {
Expand All @@ -73,6 +78,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
cancellation_token.cancel();
run_handle.await
}
_ = sigterm.recv() => {
tracing::info!(em_id, "Received SIGTERM. Shutting down execution manager.");
cancellation_token.cancel();
run_handle.await
}
result = &mut run_handle => {
tracing::info!("Execution manager runtime exited.");
result
Expand Down
9 changes: 9 additions & 0 deletions components/spider-scheduler/src/bin/grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use spider_utils::config::YamlConfig;
use spider_utils::logging::set_up_logging;
use tokio::net::TcpListener;
use tokio::select;
use tokio::signal::unix::SignalKind;
use tokio::signal::unix::signal;
use tonic::transport::Server;
use tonic::transport::server::TcpIncoming;

Expand Down Expand Up @@ -58,6 +60,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
|error| tracing::error!(error = % error, "Failed to bind scheduler listen address."),
)?;
let incoming = TcpIncoming::from(listener);
let mut sigterm = signal(SignalKind::terminate()).inspect_err(
|error| tracing::error!(error = % error, "Failed to register SIGTERM handler."),
)?;

Server::builder()
.add_service(SchedulerServiceServer::new(grpc_service))
Expand All @@ -73,6 +78,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
tracing::info!("Received Ctrl-C. Shutting down scheduler gRPC server.");
cancellation_token.cancel();
}
_ = sigterm.recv() => {
tracing::info!("Received SIGTERM. Shutting down scheduler gRPC server.");
cancellation_token.cancel();
}
}
})
.await
Expand Down
9 changes: 9 additions & 0 deletions components/spider-storage/src/bin/grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use spider_utils::config::YamlConfig;
use spider_utils::logging::set_up_logging;
use tokio::net::TcpListener;
use tokio::select;
use tokio::signal::unix::SignalKind;
use tokio::signal::unix::signal;
use tonic::transport::Server;
use tonic::transport::server::TcpIncoming;

Expand Down Expand Up @@ -53,6 +55,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
|error| tracing::error!(error = % error, "Failed to bind storage listen address."),
)?;
let incoming = TcpIncoming::from(listener);
let mut sigterm = signal(SignalKind::terminate()).inspect_err(
|error| tracing::error!(error = % error, "Failed to register SIGTERM handler."),
)?;
let server_cancellation_token = cancellation_token.clone();
let server = tokio::spawn(
Server::builder()
Expand Down Expand Up @@ -82,6 +87,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
server_cancellation_token.cancel();
}
_ = sigterm.recv() => {
tracing::info!("Received SIGTERM. Shutting down storage gRPC server.");
server_cancellation_token.cancel();
}
}
}),
);
Expand Down
Loading