|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::future::Future; |
| 19 | +use crate::cube_ext::catch_unwind::{ |
| 20 | + async_try_with_catch_unwind, try_with_catch_unwind, PanicError, |
| 21 | +}; |
| 22 | +use futures::sink::SinkExt; |
| 23 | +use tokio::task::JoinHandle; |
| 24 | +use tracing_futures::Instrument; |
| 25 | + |
| 26 | +/// Calls [tokio::spawn] and additionally enables tracing of the spawned task as part of the current |
| 27 | +/// computation. This is CubeStore approach to tracing, so all code must use this function instead |
| 28 | +/// of replace [tokio::spawn]. |
| 29 | +pub fn spawn<T>(task: T) -> JoinHandle<T::Output> |
| 30 | +where |
| 31 | + T: Future + Send + 'static, |
| 32 | + T::Output: Send + 'static, |
| 33 | +{ |
| 34 | + if let Some(s) = new_subtask_span() { |
| 35 | + tokio::spawn(async move { |
| 36 | + let _p = s.parent; // ensure parent stays alive. |
| 37 | + task.instrument(s.child).await |
| 38 | + }) |
| 39 | + } else { |
| 40 | + tokio::spawn(task) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// Propagates current span to blocking operation. See [spawn] for details. |
| 45 | +pub fn spawn_blocking<F, R>(f: F) -> JoinHandle<R> |
| 46 | +where |
| 47 | + F: FnOnce() -> R + Send + 'static, |
| 48 | + R: Send + 'static, |
| 49 | +{ |
| 50 | + if let Some(s) = new_subtask_span() { |
| 51 | + tokio::task::spawn_blocking(move || { |
| 52 | + let _p = s.parent; // ensure parent stays alive. |
| 53 | + s.child.in_scope(f) |
| 54 | + }) |
| 55 | + } else { |
| 56 | + tokio::task::spawn_blocking(f) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +struct SpawnSpans { |
| 61 | + parent: tracing::Span, |
| 62 | + child: tracing::Span, |
| 63 | +} |
| 64 | + |
| 65 | +fn new_subtask_span() -> Option<SpawnSpans> { |
| 66 | + let parent = tracing::Span::current(); |
| 67 | + if parent.is_disabled() { |
| 68 | + return None; |
| 69 | + } |
| 70 | + // TODO: ensure this is always enabled. |
| 71 | + let child = tracing::info_span!(parent: &parent, "subtask"); |
| 72 | + Some(SpawnSpans { parent, child }) |
| 73 | +} |
| 74 | + |
| 75 | +/// Executes future [f] in a new tokio thread. Catches panics. |
| 76 | +pub fn spawn_with_catch_unwind<F, T, E>(f: F) -> JoinHandle<Result<T, E>> |
| 77 | +where |
| 78 | + F: Future<Output = Result<T, E>> + Send + 'static, |
| 79 | + T: Send + 'static, |
| 80 | + E: From<PanicError> + Send + 'static, |
| 81 | +{ |
| 82 | + let task = async move { |
| 83 | + match async_try_with_catch_unwind(f).await { |
| 84 | + Ok(result) => result, |
| 85 | + Err(panic) => Err(E::from(panic)), |
| 86 | + } |
| 87 | + }; |
| 88 | + spawn(task) |
| 89 | +} |
| 90 | + |
| 91 | +/// Executes future [f] in a new tokio thread. Feeds the result into [tx] oneshot channel. Catches panics. |
| 92 | +pub fn spawn_oneshot_with_catch_unwind<F, T, E>( |
| 93 | + f: F, |
| 94 | + tx: futures::channel::oneshot::Sender<Result<T, E>>, |
| 95 | +) -> JoinHandle<Result<(), Result<T, E>>> |
| 96 | +where |
| 97 | + F: Future<Output = Result<T, E>> + Send + 'static, |
| 98 | + T: Send + 'static, |
| 99 | + E: From<PanicError> + Send + 'static, |
| 100 | +{ |
| 101 | + let task = async move { |
| 102 | + match async_try_with_catch_unwind(f).await { |
| 103 | + Ok(result) => tx.send(result), |
| 104 | + Err(panic) => tx.send(Err(E::from(panic))), |
| 105 | + } |
| 106 | + }; |
| 107 | + spawn(task) |
| 108 | +} |
| 109 | + |
| 110 | +/// Executes future [f] in a new tokio thread. Catches panics and feeds them into a [tx] mpsc channel |
| 111 | +pub fn spawn_mpsc_with_catch_unwind<F, T, E>( |
| 112 | + f: F, |
| 113 | + mut tx: futures::channel::mpsc::Sender<Result<T, E>>, |
| 114 | +) -> JoinHandle<()> |
| 115 | +where |
| 116 | + F: Future<Output = ()> + Send + 'static, |
| 117 | + T: Send + 'static, |
| 118 | + E: From<PanicError> + Send + 'static, |
| 119 | +{ |
| 120 | + let task = async move { |
| 121 | + match async_try_with_catch_unwind(f).await { |
| 122 | + Ok(_) => (), |
| 123 | + Err(panic) => { |
| 124 | + tx.send(Err(E::from(panic))).await.ok(); |
| 125 | + } |
| 126 | + } |
| 127 | + }; |
| 128 | + spawn(task) |
| 129 | +} |
| 130 | + |
| 131 | +/// Executes fn [f] in a new tokio thread. Catches panics and feeds them into a [tx] mpsc channel. |
| 132 | +pub fn spawn_blocking_mpsc_with_catch_unwind<F, R, T, E>( |
| 133 | + f: F, |
| 134 | + tx: tokio::sync::mpsc::Sender<Result<T, E>>, |
| 135 | +) -> JoinHandle<()> |
| 136 | +where |
| 137 | + F: FnOnce() -> R + Send + 'static, |
| 138 | + R: Send + 'static, |
| 139 | + T: Send + 'static, |
| 140 | + E: From<PanicError> + Send + 'static, |
| 141 | +{ |
| 142 | + let task = move || match try_with_catch_unwind(f) { |
| 143 | + Ok(_) => (), |
| 144 | + Err(panic) => { |
| 145 | + tx.blocking_send(Err(E::from(panic))).ok(); |
| 146 | + } |
| 147 | + }; |
| 148 | + spawn_blocking(task) |
| 149 | +} |
0 commit comments