Add a name to the Tokio task (#7544)

The `console-subscriber` feature was added in https://github.com/sigp/lighthouse/pull/7529. However, the names of the running tasks are blank:

<img width="780" alt="image" src="https://github.com/user-attachments/assets/73332a03-20c6-43ba-b810-3d0a898bb236" />


  Set the task name using `tokio::task::Builder`, which is availble when the `tokio_unstable` is enabled.

<img width="924" alt="image" src="https://github.com/user-attachments/assets/26bdac1a-348b-4f83-84b0-adfd2ba3a8cb" />
This commit is contained in:
Akihito Nakano
2025-06-03 14:08:03 +09:00
committed by GitHub
parent f67068e1ec
commit cd83d8d95d
2 changed files with 29 additions and 4 deletions

View File

@@ -10,3 +10,6 @@ futures = { workspace = true }
metrics = { workspace = true } metrics = { workspace = true }
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
tracing = { workspace = true } tracing = { workspace = true }
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(tokio_unstable)"] }

View File

@@ -144,7 +144,7 @@ impl TaskExecutor {
) { ) {
let mut shutdown_sender = self.shutdown_sender(); let mut shutdown_sender = self.shutdown_sender();
if let Some(handle) = self.handle() { if let Some(handle) = self.handle() {
handle.spawn(async move { let fut = async move {
let timer = metrics::start_timer_vec(&metrics::TASKS_HISTOGRAM, &[name]); let timer = metrics::start_timer_vec(&metrics::TASKS_HISTOGRAM, &[name]);
if let Err(join_error) = task_handle.await { if let Err(join_error) = task_handle.await {
if let Ok(_panic) = join_error.try_into_panic() { if let Ok(_panic) = join_error.try_into_panic() {
@@ -153,7 +153,14 @@ impl TaskExecutor {
} }
} }
drop(timer); drop(timer);
}); };
#[cfg(tokio_unstable)]
tokio::task::Builder::new()
.name(&format!("{name}-monitor"))
.spawn_on(fut, &handle)
.expect("Failed to spawn monitor task");
#[cfg(not(tokio_unstable))]
handle.spawn(fut);
} else { } else {
debug!("Couldn't spawn monitor task. Runtime shutting down") debug!("Couldn't spawn monitor task. Runtime shutting down")
} }
@@ -199,6 +206,12 @@ impl TaskExecutor {
int_gauge.inc(); int_gauge.inc();
if let Some(handle) = self.handle() { if let Some(handle) = self.handle() {
#[cfg(tokio_unstable)]
tokio::task::Builder::new()
.name(name)
.spawn_on(future, &handle)
.expect("Failed to spawn task");
#[cfg(not(tokio_unstable))]
handle.spawn(future); handle.spawn(future);
} else { } else {
debug!("Couldn't spawn task. Runtime shutting down"); debug!("Couldn't spawn task. Runtime shutting down");
@@ -234,7 +247,7 @@ impl TaskExecutor {
let int_gauge_1 = int_gauge.clone(); let int_gauge_1 = int_gauge.clone();
int_gauge.inc(); int_gauge.inc();
if let Some(handle) = self.handle() { if let Some(handle) = self.handle() {
Some(handle.spawn(async move { let fut = async move {
futures::pin_mut!(exit); futures::pin_mut!(exit);
let result = match future::select(Box::pin(task), exit).await { let result = match future::select(Box::pin(task), exit).await {
future::Either::Left((value, _)) => Some(value), future::Either::Left((value, _)) => Some(value),
@@ -245,7 +258,16 @@ impl TaskExecutor {
}; };
int_gauge_1.dec(); int_gauge_1.dec();
result result
})) };
#[cfg(tokio_unstable)]
return Some(
tokio::task::Builder::new()
.name(name)
.spawn_on(fut, &handle)
.expect("Failed to spawn task"),
);
#[cfg(not(tokio_unstable))]
Some(handle.spawn(fut))
} else { } else {
debug!("Couldn't spawn task. Runtime shutting down"); debug!("Couldn't spawn task. Runtime shutting down");
None None