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
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Changelog

## 0.6.0

### Removed

The legacy shutdown API in `torrust_server_lib::signals`, deprecated since `0.5.0`, is removed.
This is a breaking release.

| Removed | Migrate to |
| ------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| `Halted` | Inject a `tokio_util::sync::CancellationToken`. |
| `shutdown_signal` | `cancellation_signal(token)`. |
| `shutdown_signal_with_message` | `cancellation_signal(token)` followed by your own log line. |
| `global_shutdown_signal` | The executable subscribes to SIGINT/SIGTERM and cancels the injected token. |

`Started` and `cancellation_signal` are unchanged.

The crate no longer enables the `tokio` `signal` feature and no longer depends on `derive_more`.

For the migration steps, see the `0.5.0` section below: executables own SIGINT/SIGTERM and cancel an
injected `CancellationToken`.

## 0.5.0

### Deprecated
Expand Down
83 changes: 1 addition & 82 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ publish = true
readme = "README.md"
repository = "https://github.com/torrust/torrust-server-lib"
rust-version = "1.88"
version = "0.5.0"
version = "0.6.0"

[dependencies]
derive_more = { version = "2", features = [ "as_ref", "constructor", "display", "from" ] }
tokio = { version = "1", features = [ "macros", "net", "rt-multi-thread", "signal", "sync" ] }
tokio = { version = "1", features = [ "macros", "net", "rt-multi-thread", "sync" ] }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Suggestion][F1] Library code no longer needs tokio's macros, net or rt-multi-thread; 0.6.0 is the cheap window to trim them

After this change the non-test code uses only tokio::sync::Mutex (sync) and tokio::task::JoinHandle (rt). The last non-test use of macros was tokio::select! in the removed global_shutdown_signal and shutdown_signal; net and rt-multi-thread have no user at either commit. #[tokio::test] still needs macros and rt, which a dev-dependency can carry.

Dropping a feature from a published dependency is itself breaking for a consumer that relies on feature unification, so this release is the moment it costs nothing extra, for example tokio = { version = "1", features = [ "rt", "sync" ] } with [dev-dependencies] tokio = { version = "1", features = [ "macros", "rt" ] } and a changelog line. Optional: the tracker enables all of these itself, and keeping them is harmless.

tokio-util = { version = "0.7", features = [ "rt" ] }
torrust-net-primitives = "0.1.0"
tower-http = { version = "0", features = [ "compression-full", "cors", "propagate-header", "request-id", "trace" ] }
Expand Down
110 changes: 3 additions & 107 deletions src/signals.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! This module contains functions to handle signals.
use derive_more::Display;
use tokio_util::sync::CancellationToken;
use torrust_net_primitives::service_binding::ServiceBinding;
use tracing::instrument;
Expand All @@ -13,110 +12,20 @@ pub struct Started {
pub address: std::net::SocketAddr,
}

/// This is the message that the "launcher" spawned task receives from the main
/// application process to notify the service to shutdown.
///
#[deprecated(
since = "0.5.0",
note = "inject a `tokio_util::sync::CancellationToken` instead of sending `Halted` over a oneshot channel; removal is planned for torrust-server-lib 0.6.0"
)]
#[derive(Copy, Clone, Debug, Display)]
pub enum Halted {
Normal,
}

/// Resolves when the injected cancellation token is cancelled.
///
/// Unlike [`global_shutdown_signal`], this function never subscribes to an
/// operating-system signal. The caller owns the cancellation lifecycle.
/// This function never subscribes to an operating-system signal. The caller
/// owns the cancellation lifecycle.
#[instrument(skip(cancellation_token))]
pub async fn cancellation_signal(cancellation_token: CancellationToken) {
cancellation_token.cancelled().await;
}

/// Resolves on `ctrl_c` or the `terminate` signal.
///
/// # Panics
///
/// Will panic if the `ctrl_c` or `terminate` signal resolves with an error.
#[deprecated(
since = "0.5.0",
note = "libraries must not subscribe to SIGINT/SIGTERM; the executable should subscribe and cancel a `CancellationToken` passed to `cancellation_signal`; removal is planned for torrust-server-lib 0.6.0"
)]
#[instrument(skip())]
pub async fn global_shutdown_signal() {
let ctrl_c = async {
tokio::signal::ctrl_c().await.expect("failed to install Ctrl+C handler");
};

#[cfg(unix)]
let terminate = async {
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};

#[cfg(not(unix))]
let terminate = std::future::pending::<()>();

tokio::select! {
() = ctrl_c => {tracing::warn!("caught interrupt signal (ctrl-c), halting...");},
() = terminate => {tracing::warn!("caught interrupt signal (terminate), halting...");}
}
}

/// Resolves when the `stop_receiver` or the `global_shutdown_signal()` resolves.
///
/// # Panics
///
/// Will panic if the `stop_receiver` resolves with an error.
#[deprecated(
since = "0.5.0",
note = "use `cancellation_signal` with an injected `CancellationToken`; removal is planned for torrust-server-lib 0.6.0"
)]
#[allow(
deprecated,
reason = "the legacy function is implemented with the other legacy symbols until they are removed"
)]
#[instrument(skip(rx_halt))]
pub async fn shutdown_signal(rx_halt: tokio::sync::oneshot::Receiver<Halted>) {
let halt = async {
match rx_halt.await {
Ok(signal) => signal,
Err(err) => panic!("Failed to install stop signal: {err}"),
}
};

tokio::select! {
signal = halt => { tracing::debug!("Halt signal processed: {}", signal) },
() = global_shutdown_signal() => { tracing::debug!("Global shutdown signal processed") }
}
}

/// Same as `shutdown_signal()`, but shows a message when it resolves.
#[deprecated(
since = "0.5.0",
note = "use `cancellation_signal` with an injected `CancellationToken` and log the message yourself; removal is planned for torrust-server-lib 0.6.0"
)]
#[allow(
deprecated,
reason = "the legacy function is implemented with the other legacy symbols until they are removed"
)]
#[instrument(skip(rx_halt))]
pub async fn shutdown_signal_with_message(rx_halt: tokio::sync::oneshot::Receiver<Halted>, message: String) {
shutdown_signal(rx_halt).await;

tracing::info!("{message}");
}

#[cfg(test)]
mod tests {
use tokio::sync::oneshot;
use tokio_util::sync::CancellationToken;

#[allow(deprecated, reason = "the test covers the deprecated legacy API until it is removed")]
use super::{Halted, cancellation_signal, shutdown_signal};
use super::cancellation_signal;

#[tokio::test]
async fn cancellation_signal_should_resolve_when_the_token_is_cancelled() {
Expand All @@ -131,17 +40,4 @@ mod tests {

wait_task.await.expect("cancellation wait task should complete");
}

#[allow(deprecated, reason = "the test covers the deprecated legacy API until it is removed")]
#[tokio::test]
async fn shutdown_signal_should_resolve_when_it_receives_a_legacy_halted_message() {
let (halt_sender, halt_receiver) = oneshot::channel();
let wait_task = tokio::spawn(shutdown_signal(halt_receiver));

halt_sender
.send(Halted::Normal)
.expect("legacy halt receiver should still be waiting");

wait_task.await.expect("legacy shutdown wait task should complete");
}
}
Loading