use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::task::{Context, Poll};
use crate::coordinator::InvocationCoordinator;
use crate::BoxError;
use crate::DurableContext;
use crate::error::{ChildFnError, OperationError};
#[must_use = "futures do nothing unless polled or spawned"]
#[non_exhaustive]
pub struct DurableFuture<O> {
inner: Pin<Box<dyn Future<Output = Result<O, OperationError>> + Send>>,
awaited: Option<(Arc<AtomicBool>, Arc<InvocationCoordinator>)>,
claim: Option<PendingClaim<O>>,
task_cx: crate::task::TaskContext,
begin: Option<BeginFn<O>>,
first_poll_start: bool,
gate: Option<Box<PollGate>>,
}
struct PollGate {
ctx: DurableContext,
}
impl PollGate {
fn new(ctx: &DurableContext) -> Self {
Self { ctx: ctx.clone() }
}
fn admit(&mut self) -> Result<(), OperationError> {
self.ctx.enforce_task_ownership()
}
}
type BeginFn<O> =
Box<dyn FnOnce() -> Pin<Box<dyn Future<Output = Result<O, OperationError>> + Send>> + Send>;
pub(crate) struct PendingClaim<O> {
method: &'static str,
context: String,
run: Box<dyn FnOnce() -> DurableFuture<O> + Send>,
}
impl<O> PendingClaim<O> {
pub(crate) fn new(
method: &'static str,
context: String,
run: impl FnOnce() -> DurableFuture<O> + Send + 'static,
) -> Self {
Self {
method,
context,
run: Box::new(run),
}
}
}
impl<O> std::fmt::Debug for DurableFuture<O> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DurableFuture").finish_non_exhaustive()
}
}
impl<O: Send + 'static> DurableFuture<O> {
pub(crate) fn from_async<F>(fut: F) -> Self
where
F: Future<Output = Result<O, OperationError>> + Send + 'static,
{
Self {
inner: Box::pin(fut),
awaited: None,
claim: None,
task_cx: crate::task::TaskContext::none(),
begin: None,
first_poll_start: false,
gate: None,
}
}
pub(crate) fn map_output<T, M>(self, map: M) -> DurableFuture<T>
where
T: Send + 'static,
M: FnOnce(Result<O, OperationError>) -> Result<T, OperationError> + Send + 'static,
{
let Self {
inner,
awaited,
claim,
task_cx,
begin,
first_poll_start,
gate,
} = self;
match claim {
Some(claim) => {
let PendingClaim {
method,
context,
run,
} = claim;
DurableFuture::deferred(PendingClaim::new(method, context, move || {
run().map_output(map)
}))
}
None => match begin {
Some(begin) => DurableFuture {
inner: Box::pin(std::future::pending()),
awaited,
claim: None,
task_cx,
begin: Some(Box::new(move || {
let rest = begin();
Box::pin(async move { map(rest.await) })
})),
first_poll_start,
gate,
},
None => DurableFuture {
inner: Box::pin(async move { map(inner.await) }),
awaited,
claim: None,
task_cx,
begin: None,
first_poll_start,
gate,
},
},
}
}
pub(crate) fn deferred(claim: PendingClaim<O>) -> Self {
Self {
inner: Box::pin(std::future::pending()),
awaited: None,
claim: Some(claim),
task_cx: crate::task::TaskContext::none(),
begin: None,
first_poll_start: false,
gate: None,
}
}
pub(crate) fn claim_now(&mut self) {
if let Some(claim) = self.claim.take() {
let claimed = (claim.run)();
self.inner = claimed.inner;
self.task_cx = claimed.task_cx;
self.gate = claimed.gate;
}
}
pub(crate) fn is_unclaimed_combinator(&self) -> bool {
self.claim.is_some()
}
pub(crate) fn unclaimed_input_message(&self, outer: &str) -> Option<String> {
self.claim.as_ref().map(|claim| {
format!(
"context usage error in \"{method}\": the combinator was created on context \
\"{context}\", but combinator \"{outer}\" started it as an input. Start the \
inner combinator with .spawn() and pass the returned handle to the outer \
combinator.",
method = claim.method,
context = claim.context,
)
})
}
pub(crate) fn for_operation<F>(ctx: &DurableContext, fut: F) -> Self
where
F: Future<Output = Result<O, OperationError>> + Send + 'static,
{
let gate = ctx.clone();
let mut future = Self::from_async(async move {
gate.admit_operation().await;
drop(gate);
fut.await
});
future.task_cx = crate::task::TaskContext::current();
future.gate = Some(Box::new(PollGate::new(ctx)));
future
}
pub(crate) fn task_context(&self) -> crate::task::TaskContext {
self.task_cx.clone()
}
pub(crate) fn for_started_operation<F>(ctx: &DurableContext, fut: F) -> Self
where
F: Future<Output = Result<O, OperationError>> + Send + 'static,
{
let mut future = Self::from_async(fut);
future.task_cx = crate::task::TaskContext::current();
future.gate = Some(Box::new(PollGate::new(ctx)));
future
}
pub(crate) fn for_start_step<F>(
ctx: &DurableContext,
begin: impl FnOnce() -> F + Send + 'static,
) -> Self
where
F: Future<Output = Result<O, OperationError>> + Send + 'static,
{
Self {
inner: Box::pin(std::future::pending()),
awaited: None,
claim: None,
begin: Some(Box::new(move || Box::pin(begin()))),
first_poll_start: false,
task_cx: crate::task::TaskContext::current(),
gate: Some(Box::new(PollGate::new(ctx))),
}
}
fn begin_now(&mut self) {
if let Some(begin) = self.begin.take() {
self.inner = begin();
}
}
pub(crate) fn begin_in_creator_context(&mut self) {
if let Some(begin) = self.begin.take() {
self.inner = self.task_cx.enter(begin);
}
}
pub(crate) fn for_first_poll_operation<F>(ctx: &DurableContext, fut: F) -> Self
where
F: Future<Output = Result<O, OperationError>> + Send + 'static,
{
let mut future = Self::for_operation(ctx, fut);
future.first_poll_start = true;
future
}
pub(crate) fn starts_on_first_poll(&self) -> bool {
self.first_poll_start
}
pub(crate) fn spawn_blessed(
mut future: Self,
ctx: &DurableContext,
awaited: Option<Arc<AtomicBool>>,
) -> Self {
if let Some(refused) = Self::refuse_foreign_spawn(ctx) {
return refused;
}
future.claim_now();
let coordinator = ctx.coordinator();
let task_cx = future.task_context();
match crate::task::spawn_tracked(task_cx.clone(), coordinator, ctx.task_ownership(), future)
{
Ok(task) => {
if let Some(abort) = task.abort_handle() {
ctx.admission().register_task(ctx.owners(), abort);
}
let mut handle = Self::from_async(async move { task.await.into_value() });
handle.awaited = awaited.map(|flag| (flag, Arc::clone(coordinator)));
handle.task_cx = task_cx;
handle
}
Err(no_runtime) => {
Self::from_async(
async move { Err(crate::task::NoRuntime::operation_error(no_runtime)) },
)
}
}
}
pub(crate) fn spawn_built(
ctx: &DurableContext,
awaited: Option<Arc<AtomicBool>>,
build: impl FnOnce() -> Self,
) -> Self {
if let Some(refused) = Self::refuse_foreign_spawn(ctx) {
return refused;
}
Self::spawn_blessed(build(), ctx, awaited)
}
pub(crate) fn spawn_started(
ctx: &DurableContext,
awaited: Option<Arc<AtomicBool>>,
start: impl FnOnce() -> Self,
) -> Self {
if let Some(refused) = Self::refuse_foreign_spawn(ctx) {
return refused;
}
if let Err(no_runtime) = crate::task::task_runtime(ctx.coordinator()).map(drop) {
return Self::from_async(async move {
Err(crate::task::NoRuntime::operation_error(no_runtime))
});
}
Self::spawn_blessed(start(), ctx, awaited)
}
fn refuse_foreign_spawn(ctx: &DurableContext) -> Option<Self> {
let reason = ctx.task_ownership().check_current_task().err()?;
ctx.coordinator().record_fatal(
"Error".to_owned(),
crate::context::foreign_caller_message("spawn"),
);
Some(Self::from_async(async move {
Err(OperationError::from_kind(
crate::error::OperationErrorKind::Step(crate::error::StepError::new(
crate::error::StepErrorKind::ExecutionFailed,
Some(reason.into()),
)),
))
}))
}
}
impl<O: Send + 'static> Future for DurableFuture<O> {
type Output = Result<O, OperationError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.claim_now();
if let Some(gate) = self.gate.as_mut()
&& let Err(refused) = gate.admit()
{
return Poll::Ready(Err(refused));
}
self.begin_now();
if let Some((flag, coordinator)) = self.awaited.take() {
flag.store(true, Ordering::SeqCst);
coordinator.notify_awaited();
}
self.inner.as_mut().poll(cx)
}
}
#[non_exhaustive]
pub struct Branch<O> {
name: Option<String>,
body: BranchBody<O>,
}
pub(crate) struct BranchBody<O> {
ctx_tx: tokio::sync::oneshot::Sender<DurableContext>,
future: Pin<Box<dyn Future<Output = Result<O, ChildFnError>> + Send>>,
}
impl<O> BranchBody<O> {
pub(crate) fn start(
self,
ctx: DurableContext,
) -> Pin<Box<dyn Future<Output = Result<O, ChildFnError>> + Send>> {
let _ = self.ctx_tx.send(ctx);
self.future
}
}
impl<O> std::fmt::Debug for Branch<O> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Branch")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl<O: Send + 'static> Branch<O> {
pub fn new<F, Fut>(name: impl Into<String>, factory: F) -> Self
where
F: FnOnce(DurableContext) -> Fut + Send + 'static,
Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
{
Self::with_name(Some(name.into()), factory)
}
pub fn unnamed<F, Fut>(factory: F) -> Self
where
F: FnOnce(DurableContext) -> Fut + Send + 'static,
Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
{
Self::with_name(None, factory)
}
fn with_name<F, Fut>(name: Option<String>, factory: F) -> Self
where
F: FnOnce(DurableContext) -> Fut + Send + 'static,
Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
{
let (ctx_tx, ctx_rx) = tokio::sync::oneshot::channel::<DurableContext>();
Self {
name,
body: BranchBody {
ctx_tx,
future: Box::pin(async move {
let ctx = ctx_rx.await.map_err(|_| {
ChildFnError::new("branch context was never delivered (coordinator gone)")
})?;
factory(ctx).await.map_err(ChildFnError::from)
}),
},
}
}
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub(crate) fn into_parts(self) -> (Option<String>, BranchBody<O>) {
(self.name, self.body)
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct Callback<O> {
id: String,
state: CallbackState<O>,
}
pub(crate) type CallbackResolver<O> =
Box<dyn FnOnce() -> Pin<Box<dyn Future<Output = Result<O, OperationError>> + Send>> + Send>;
#[derive(Debug)]
pub(crate) struct CallbackIdentity {
pub(crate) name: Option<String>,
pub(crate) plugin_name: Option<String>,
pub(crate) sub_type: String,
}
enum CallbackState<O> {
Settled(Option<Result<O, OperationError>>),
Pending {
ctx: DurableContext,
wire_id: String,
identity: CallbackIdentity,
resolver: CallbackResolver<O>,
},
}
impl<O: std::fmt::Debug> std::fmt::Debug for CallbackState<O> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Settled(outcome) => f.debug_tuple("Settled").field(outcome).finish(),
Self::Pending {
ctx,
wire_id,
identity,
..
} => f
.debug_struct("Pending")
.field("ctx", ctx)
.field("wire_id", wire_id)
.field("identity", identity)
.finish_non_exhaustive(),
}
}
}
impl<O: Send + 'static> Callback<O> {
pub(crate) fn new_settled(id: String, result: Result<O, OperationError>) -> Self {
Self {
id,
state: CallbackState::Settled(Some(result)),
}
}
pub(crate) fn new_pending(
id: String,
ctx: DurableContext,
wire_id: String,
identity: CallbackIdentity,
resolver: CallbackResolver<O>,
) -> Self {
Self {
id,
state: CallbackState::Pending {
ctx,
wire_id,
identity,
resolver,
},
}
}
pub fn id(&self) -> &str {
&self.id
}
pub fn result(self) -> DurableFuture<O> {
match self.state {
CallbackState::Settled(outcome) => DurableFuture::from_async(async move {
outcome.unwrap_or_else(|| {
Err(OperationError::from_kind(
crate::error::OperationErrorKind::Callback(
crate::error::CallbackError::new(
crate::error::CallbackErrorKind::Internal,
Some("settled callback had no outcome".into()),
),
),
))
})
}),
CallbackState::Pending {
ctx,
wire_id,
identity,
resolver,
} => Self::pending_result(ctx, wire_id, identity, resolver),
}
}
pub fn spawn_result(self) -> DurableFuture<O> {
match self.state {
CallbackState::Settled(_) => self.result(),
CallbackState::Pending {
ctx,
wire_id,
identity,
resolver,
} => {
let flag = Arc::new(AtomicBool::new(false));
let waiting = ctx.with_awaited(Arc::clone(&flag));
let future = Self::pending_result(waiting, wire_id, identity, resolver);
DurableFuture::spawn_blessed(future, &ctx, Some(flag))
}
}
}
fn pending_result(
ctx: DurableContext,
wire_id: String,
identity: CallbackIdentity,
resolver: CallbackResolver<O>,
) -> DurableFuture<O> {
let gate = ctx.clone();
DurableFuture::for_operation(&gate, async move {
ctx.enforce_task_ownership()?;
ctx.wait_for_record(&wire_id, |status| {
!matches!(
status,
crate::engine::CheckpointStatus::Started
| crate::engine::CheckpointStatus::Pending
)
})
.await;
if let Some(plugins) = ctx.plugins() {
let terminal = ctx
.with_checkpoint_record_by_wire(&wire_id, |record| {
crate::callback::is_terminal_callback_status(&record.status)
})
.unwrap_or(false);
if terminal {
let info = crate::callback::callback_info(
&ctx,
&wire_id,
&identity.sub_type,
identity.name.as_deref(),
identity.plugin_name.as_deref(),
)
.with_replay(false);
crate::plugin::operation_ended(plugins, &info);
}
}
resolver().await
})
}
}
#[non_exhaustive]
pub struct BranchSet {
branches: Vec<(Option<String>, BranchBody<BranchValue>)>,
decoders: Vec<Arc<dyn BranchDecoder>>,
json: Vec<bool>,
identity: Arc<SetIdentity>,
}
struct SetIdentity;
impl Default for BranchSet {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for BranchSet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let names: Vec<&str> = self
.branches
.iter()
.map(|(name, _)| name.as_deref().unwrap_or_default())
.collect();
f.debug_struct("BranchSet")
.field("names", &names)
.finish_non_exhaustive()
}
}
impl BranchSet {
#[must_use]
pub fn new() -> Self {
Self {
branches: Vec::new(),
decoders: Vec::new(),
json: Vec::new(),
identity: Arc::new(SetIdentity),
}
}
#[must_use]
pub fn add<T, F, Fut>(&mut self, name: impl Into<String>, factory: F) -> BranchHandle<T>
where
T: serde::Serialize + serde::de::DeserializeOwned + Send + 'static,
F: FnOnce(DurableContext) -> Fut + Send + 'static,
Fut: Future<Output = Result<T, BoxError>> + Send + 'static,
{
self.insert(name.into(), crate::JsonSerdes, factory, true)
}
#[must_use]
pub fn add_with_serdes<T, S, F, Fut>(
&mut self,
name: impl Into<String>,
serdes: S,
factory: F,
) -> BranchHandle<T>
where
T: Send + 'static,
S: crate::Serdes<T> + 'static,
F: FnOnce(DurableContext) -> Fut + Send + 'static,
Fut: Future<Output = Result<T, BoxError>> + Send + 'static,
{
self.insert(name.into(), serdes, factory, false)
}
fn insert<T, S, F, Fut>(
&mut self,
name: String,
serdes: S,
factory: F,
json: bool,
) -> BranchHandle<T>
where
T: Send + 'static,
S: crate::Serdes<T> + 'static,
F: FnOnce(DurableContext) -> Fut + Send + 'static,
Fut: Future<Output = Result<T, BoxError>> + Send + 'static,
{
let index = self.branches.len();
let record_name = if name.is_empty() {
format!("parallel-branch-{index}")
} else {
name.clone()
};
let key = BranchKey {
identity: Arc::clone(&self.identity),
index,
_marker: PhantomData,
};
let slot: BranchSlot<T> = Arc::new(std::sync::Mutex::new(None));
let serdes = Arc::new(serdes);
let decoder = BranchShared {
serdes: Arc::clone(&serdes),
slot: Arc::clone(&slot),
identity: Arc::clone(&self.identity),
index,
};
let body_slot = Arc::clone(&slot);
let body_identity = Arc::clone(&self.identity);
let branch = Branch::new(name, move |ctx: DurableContext| async move {
let value = factory(ctx).await?;
Ok(BranchValue {
value: Box::new(TypedBranchValue {
value,
serdes,
slot: body_slot,
}),
identity: body_identity,
index,
})
});
self.branches.push(branch.into_parts());
self.decoders.push(Arc::new(decoder));
self.json.push(json);
BranchHandle {
name: record_name,
slot,
key,
}
}
#[expect(clippy::type_complexity)]
pub(crate) fn into_parts(
self,
) -> (
Vec<(Option<String>, BranchBody<BranchValue>)>,
Arc<Vec<Arc<dyn BranchDecoder>>>,
Arc<[bool]>,
) {
(self.branches, Arc::new(self.decoders), Arc::from(self.json))
}
}
#[non_exhaustive]
pub struct BranchHandle<T> {
name: String,
slot: BranchSlot<T>,
key: BranchKey<T>,
}
impl<T> std::fmt::Debug for BranchHandle<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let has_result = lock_slot(&self.slot).is_some();
f.debug_struct("BranchHandle")
.field("name", &self.name)
.field("has_result", &has_result)
.finish_non_exhaustive()
}
}
impl<T> BranchHandle<T> {
#[must_use]
pub fn key(&self) -> BranchKey<T> {
self.key.clone()
}
pub fn into_result(self) -> Result<T, OperationError> {
let taken = lock_slot(&self.slot).take();
taken.unwrap_or_else(|| Err(not_completed_error(&self.name)))
}
}
type BranchSlot<T> = Arc<std::sync::Mutex<Option<Result<T, OperationError>>>>;
fn lock_slot<T>(
slot: &std::sync::Mutex<Option<Result<T, OperationError>>>,
) -> std::sync::MutexGuard<'_, Option<Result<T, OperationError>>> {
slot.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
fn not_completed_error(name: &str) -> OperationError {
OperationError::from_kind(crate::OperationErrorKind::ChildContext(
crate::error::ChildContextError::new(
crate::ChildContextErrorKind::NotCompleted,
Some(format!("parallel branch \"{name}\" has no result").into()),
),
))
}
pub struct BranchValue {
value: Box<dyn ErasedBranchValue>,
identity: Arc<SetIdentity>,
index: usize,
}
impl std::fmt::Debug for BranchValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BranchValue").finish_non_exhaustive()
}
}
impl BranchValue {
pub(crate) fn encode(
self,
ctx: crate::serdes::SerdesContext,
) -> Pin<Box<dyn Future<Output = Result<String, BoxError>> + Send>> {
self.value.encode(ctx)
}
pub(crate) fn publish(self) {
self.value.publish();
}
pub(crate) fn of_branch<T: 'static>(&self, key: &BranchKey<T>) -> Option<&T> {
if !Arc::ptr_eq(&self.identity, &key.identity) || self.index != key.index {
return None;
}
#[expect(clippy::expect_used)]
let value = self
.value
.value()
.downcast_ref::<T>()
.expect("a branch's key has the type of the branch's values");
Some(value)
}
}
pub struct BranchKey<T> {
identity: Arc<SetIdentity>,
index: usize,
_marker: PhantomData<fn(T) -> T>,
}
impl<T> BranchKey<T> {
pub(crate) fn index(&self) -> usize {
self.index
}
}
impl<T> Clone for BranchKey<T> {
fn clone(&self) -> Self {
Self {
identity: Arc::clone(&self.identity),
index: self.index,
_marker: PhantomData,
}
}
}
impl<T> std::fmt::Debug for BranchKey<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BranchKey")
.field("index", &self.index)
.finish_non_exhaustive()
}
}
pub(crate) trait ErasedBranchValue: Send {
fn encode(
self: Box<Self>,
ctx: crate::serdes::SerdesContext,
) -> Pin<Box<dyn Future<Output = Result<String, BoxError>> + Send>>;
fn publish(self: Box<Self>);
fn value(&self) -> &dyn std::any::Any;
}
pub(crate) trait BranchDecoder: Send + Sync {
fn decode(
&self,
wire: String,
ctx: crate::serdes::SerdesContext,
) -> Pin<Box<dyn Future<Output = Result<BranchValue, BoxError>> + Send + '_>>;
fn publish_error(&self, err: OperationError);
}
struct TypedBranchValue<T, S> {
value: T,
serdes: Arc<S>,
slot: BranchSlot<T>,
}
impl<T, S> ErasedBranchValue for TypedBranchValue<T, S>
where
T: Send + 'static,
S: crate::Serdes<T>,
{
fn encode(
self: Box<Self>,
ctx: crate::serdes::SerdesContext,
) -> Pin<Box<dyn Future<Output = Result<String, BoxError>> + Send>> {
let Self { value, serdes, .. } = *self;
Box::pin(async move { serdes.serialize(value, ctx).await })
}
fn publish(self: Box<Self>) {
let Self { value, slot, .. } = *self;
*lock_slot(&slot) = Some(Ok(value));
}
fn value(&self) -> &dyn std::any::Any {
&self.value
}
}
struct BranchShared<T, S> {
serdes: Arc<S>,
slot: BranchSlot<T>,
identity: Arc<SetIdentity>,
index: usize,
}
impl<T, S> BranchDecoder for BranchShared<T, S>
where
T: Send + 'static,
S: crate::Serdes<T>,
{
fn decode(
&self,
wire: String,
ctx: crate::serdes::SerdesContext,
) -> Pin<Box<dyn Future<Output = Result<BranchValue, BoxError>> + Send + '_>> {
Box::pin(async move {
let value = self.serdes.deserialize(wire, ctx).await?;
Ok(BranchValue {
value: Box::new(TypedBranchValue {
value,
serdes: Arc::clone(&self.serdes),
slot: Arc::clone(&self.slot),
}),
identity: Arc::clone(&self.identity),
index: self.index,
})
})
}
fn publish_error(&self, err: OperationError) {
*lock_slot(&self.slot) = Some(Err(err));
}
}
#[cfg(test)]
#[expect(clippy::panic)]
#[expect(clippy::disallowed_methods)]
mod tests {
use super::*;
use crate::engine::CheckpointLog;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
fn test_ctx_with_client() -> DurableContext {
let client = Arc::new(crate::client::InMemoryExecutionClient::new(Vec::new()));
DurableContext::new_root_with_client(
"test-function-arn".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::empty()),
client,
"token0".to_owned(),
)
}
use std::future::IntoFuture as _;
thread_local! {
static TAGS: std::cell::RefCell<Vec<String>> = const { std::cell::RefCell::new(Vec::new()) };
static ENTERED: std::cell::RefCell<Vec<(String, Vec<String>)>> = const { std::cell::RefCell::new(Vec::new()) };
}
fn current_tags() -> Vec<String> {
TAGS.with(|tags| tags.borrow().clone())
}
struct Tag;
impl Tag {
fn wrap(site: &str, next: &mut dyn FnMut()) {
TAGS.with(|tags| tags.borrow_mut().push(site.to_owned()));
next();
TAGS.with(|tags| tags.borrow_mut().pop());
}
}
impl crate::plugin::Plugin for Tag {
fn wrap_invocation(&self, _info: &crate::plugin::InvocationInfo, next: &mut dyn FnMut()) {
Self::wrap("inv", next);
}
fn wrap_user_function(&self, info: &crate::plugin::OperationInfo, next: &mut dyn FnMut()) {
let site = info.name().unwrap_or(info.id());
ENTERED.with(|entered| {
entered.borrow_mut().push((site.to_owned(), current_tags()));
});
Self::wrap(site, next);
}
}
impl crate::plugin::PluginFactory for Tag {
fn create_plugin(
&self,
_info: &crate::plugin::InvocationInfo,
) -> Result<Option<Box<dyn crate::plugin::Plugin>>, BoxError> {
Ok(Some(Box::new(Tag)))
}
}
fn tagged_ctx() -> (DurableContext, crate::plugin::Frames) {
let factory: Arc<dyn crate::plugin::PluginFactory> = Arc::new(Tag);
let plugins = crate::plugin::create_plugins(
&[("Tag", factory)],
None,
crate::plugin::tests_support_info,
)
.expect("a factory was given");
let ctx = DurableContext::new_root_with_client_and_defaults(
"test-function-arn".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::empty()),
Arc::new(crate::client::InMemoryExecutionClient::new(Vec::new())),
"token0".to_owned(),
std::time::Duration::ZERO,
0,
Some(Arc::clone(&plugins)),
);
(ctx, crate::plugin::Frames::invocation(Some(plugins)))
}
type Seen = Arc<std::sync::Mutex<Vec<(&'static str, Vec<String>)>>>;
fn recording(
seen: &Seen,
name: &'static str,
value: u32,
) -> impl FnOnce(crate::StepContext) -> std::future::Ready<Result<u32, BoxError>>
+ Clone
+ Send
+ 'static {
let seen = Arc::clone(seen);
move |_| {
seen.lock().unwrap().push((name, current_tags()));
std::future::ready(Ok(value))
}
}
#[test]
fn map_output_keeps_task_context_and_pending_claim() {
let subscriber = lambda_runtime::tracing::subscriber::registry();
{
let _guard = test_tracing::install_test_subscriber(subscriber);
let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.expect("runtime");
let _guard = runtime.enter();
let span_a = tracing::info_span!("A");
let span_b = tracing::info_span!("B");
assert!(span_a.id().is_some() && span_b.id().is_some());
for spawned in [false, true] {
let (ctx, inv) = tagged_ctx();
let frames_a =
inv.with_user_function(Arc::new(crate::plugin::OperationInfo::for_test("a")));
let frames_b =
inv.with_user_function(Arc::new(crate::plugin::OperationInfo::for_test("b")));
let seen: Seen = Arc::default();
let (step, mut join) = span_a.in_scope(|| {
frames_a.enter(|| {
let step = ctx
.step(recording(&seen, "solo", 1))
.name("solo")
.future()
.map_output(|r| r.map(|v| v + 1));
let s1 = ctx.step(recording(&seen, "s1", 1)).name("s1").future();
let s2 = ctx.step(recording(&seen, "s2", 2)).name("s2").future();
let join = ctx
.try_join_all([s1, s2])
.name("join")
.into_future()
.map_output(|r| r.map(|v| v.iter().sum::<u32>() * 10));
(step, join)
})
});
assert_eq!(
step.task_context().frames().enter(current_tags),
["inv", "a"],
"spawned: {spawned}"
);
assert_eq!(step.task_context().span().id(), span_a.id());
assert!(join.is_unclaimed_combinator(), "spawned: {spawned}");
assert_eq!(ctx.mint_id().positional(), "4", "spawned: {spawned}");
span_b.in_scope(|| {
frames_b.enter(|| {
if spawned {
join.claim_now();
} else {
let waker = std::task::Waker::noop();
let mut cx = Context::from_waker(waker);
drop(Pin::new(&mut join).poll(&mut cx));
}
});
});
assert!(!join.is_unclaimed_combinator(), "spawned: {spawned}");
assert_eq!(ctx.mint_id().positional(), "6", "spawned: {spawned}");
assert_eq!(
join.task_context().frames().enter(current_tags),
["inv", "b"],
"the claim captured B's frames; spawned: {spawned}"
);
assert_eq!(join.task_context().span().id(), span_b.id());
let step = DurableFuture::spawn_blessed(step, &ctx, None);
assert_eq!(runtime.block_on(step).expect("the step succeeds"), 2);
let join = DurableFuture::spawn_blessed(join, &ctx, None);
assert_eq!(runtime.block_on(join).expect("the join succeeds"), 30);
let mut seen = std::mem::take(&mut *seen.lock().unwrap());
seen.sort_by_key(|(name, _)| *name);
let tags = |list: &[&str]| -> Vec<String> {
list.iter().map(|tag| (*tag).to_owned()).collect()
};
assert_eq!(
seen,
[
("s1", tags(&["inv", "a", "s1"])),
("s2", tags(&["inv", "a", "s2"])),
("solo", tags(&["inv", "a", "solo"])),
],
"spawned: {spawned}"
);
let entered = ENTERED.with(|entered| std::mem::take(&mut *entered.borrow_mut()));
let joins: Vec<_> = entered.iter().filter(|(site, _)| site == "join").collect();
assert!(!joins.is_empty(), "the join ran as a user function");
assert!(
joins
.iter()
.all(|(_, around)| *around == tags(&["inv", "b"])),
"{entered:?}; spawned: {spawned}"
);
}
};
}
#[test]
fn parallel_mixed_keeps_the_task_context_of_its_creation() {
let subscriber = lambda_runtime::tracing::subscriber::registry();
{
let _guard = test_tracing::install_test_subscriber(subscriber);
let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.expect("runtime");
let _guard = runtime.enter();
let ctx = test_ctx_with_client();
let span = tracing::info_span!("creation");
let mixed = span.in_scope(|| {
let mut set = BranchSet::new();
let _handle = set.add("b", |_c| async { Ok(1_u32) });
ctx.parallel_mixed(set).into_future()
});
assert!(span.id().is_some());
assert_eq!(mixed.task_context().span().id(), span.id());
let plain = span.in_scope(|| ctx.step(|_| async { Ok(1_u32) }).future());
let mapped = plain.map_output(|r| r.map(|v| v + 1));
assert_eq!(mapped.task_context().span().id(), span.id());
};
}
#[test]
fn poll_gate_refuses_raw_task_with_reused_identity() {
let ctx = test_ctx_with_client();
let mut gate = PollGate::new(&ctx);
let ownership = Arc::clone(ctx.task_ownership());
let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
let (first, second) = runtime
.block_on(async move {
tokio::spawn(async move {
let id = tokio::task::id();
ownership.bless_task(id);
let first = gate.admit().is_ok();
ownership.unbless_task(id);
let second = gate.admit().is_ok();
(first, second)
})
.await
})
.unwrap();
assert!(first, "the blessed task was refused");
assert!(!second, "a raw task with a reused task ID was admitted");
let fatal = ctx.coordinator().fatal_error().expect("fatal recorded");
assert_eq!(
fatal.error_message,
crate::context::foreign_caller_message("execute")
);
}
#[tokio::test]
async fn claim_now_claims_once() {
let ctx = test_ctx_with_client();
let mut all = ctx.try_join_all(Vec::<DurableFuture<i32>>::new()).future();
assert!(all.is_unclaimed_combinator());
all.claim_now();
assert!(!all.is_unclaimed_combinator());
all.claim_now();
assert_eq!(
all.await.expect("an empty join succeeds"),
Vec::<i32>::new()
);
assert_eq!(
ctx.mint_id().positional(),
"2",
"the combinator claimed 1 only"
);
}
#[tokio::test]
async fn claim_now_is_noop_for_non_combinator() {
let ctx = test_ctx_with_client();
let mut step = ctx.step(|_| async { Ok(1_i32) }).future();
assert!(!step.is_unclaimed_combinator());
assert_eq!(step.unclaimed_input_message("9"), None);
step.claim_now();
assert_eq!(ctx.mint_id().positional(), "2", "only the step claimed");
assert_eq!(step.await.expect("the step succeeds"), 1);
}
#[tokio::test]
async fn spawned_panic_propagates_original_payload() {
let ctx = test_ctx_with_client();
let fut: DurableFuture<i32> = ctx.step(|_| async { panic!("kaboom-spawn") }).spawn();
let join_err = tokio::spawn(fut).await.unwrap_err();
assert!(join_err.is_panic(), "panic must stay a panic: {join_err}");
let payload = join_err.into_panic();
let msg = payload
.downcast_ref::<&str>()
.map(|s| (*s).to_owned())
.or_else(|| payload.downcast_ref::<String>().cloned())
.unwrap_or_default();
assert!(
msg.contains("kaboom-spawn"),
"original panic payload must be preserved, got: {msg}"
);
}
#[test]
fn spawn_started_with_current_runtime_but_no_invocation_runtime_does_not_start() {
let ctx = test_ctx_with_client();
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("a runtime builds");
let started = Arc::new(AtomicBool::new(false));
let flag = Arc::clone(&started);
let error = runtime.block_on(async move {
let handle: DurableFuture<u32> = DurableFuture::spawn_started(&ctx, None, move || {
flag.store(true, Ordering::SeqCst);
DurableFuture::from_async(async { Ok(1_u32) })
});
handle.await.expect_err("no invocation runtime").to_string()
});
assert!(!started.load(Ordering::SeqCst), "start must not be called");
let expected = crate::task::NoRuntime::operation_error(crate::task::NoRuntime).to_string();
assert_eq!(error, expected);
}
#[tokio::test]
async fn spawn_outside_runtime_returns_error() {
let client = Arc::new(crate::client::InMemoryExecutionClient::new(Vec::new()));
let ran = Arc::new(AtomicBool::new(false));
let thread_client = Arc::clone(&client);
let thread_ran = Arc::clone(&ran);
let polled = crate::task::test_support::poll_once_without_runtime(move || {
DurableContext::new_root_with_client(
"arn:aws:lambda:us-east-1:1:function:test".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::empty()),
thread_client as Arc<dyn crate::client::ExecutionClient>,
"token0".to_owned(),
)
.step(move |_| async move {
thread_ran.store(true, Ordering::SeqCst);
Ok(1_i32)
})
.spawn()
});
let Poll::Ready(Err(err)) = polled else {
panic!("expected an immediate error, got {polled:?}");
};
let crate::error::OperationErrorKind::Step(step_err) = err.kind() else {
panic!(
"expected the task-ownership error kind, got {:?}",
err.kind()
);
};
assert!(matches!(
step_err.kind(),
crate::error::StepErrorKind::ExecutionFailed
));
assert!(
format!("{err:#}").contains(crate::task::NO_RUNTIME_MESSAGE),
"unexpected error: {err:#}"
);
assert!(!ran.load(Ordering::SeqCst), "the body must not run");
assert!(client.recorded_updates().is_empty(), "nothing is recorded");
}
#[tokio::test]
async fn dropped_spawned_future_keeps_task_running_without_panic() {
#[derive(Clone)]
struct DropGuard(Arc<AtomicBool>);
impl Drop for DropGuard {
fn drop(&mut self) {
self.0.store(true, Ordering::SeqCst);
}
}
let ctx = test_ctx_with_client();
let dropped = Arc::new(AtomicBool::new(false));
let started = Arc::new(AtomicBool::new(false));
let guard = DropGuard(Arc::clone(&dropped));
let body_started = Arc::clone(&started);
let fut: DurableFuture<i32> = ctx
.step(move |_| async move {
let _guard = guard;
body_started.store(true, Ordering::SeqCst);
std::future::pending::<()>().await;
Ok(0)
})
.spawn();
drop(fut);
for _ in 0..50 {
if started.load(Ordering::SeqCst) {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
assert!(
started.load(Ordering::SeqCst),
"the body runs after the drop"
);
assert!(
!dropped.load(Ordering::SeqCst),
"the dropped handle does not cancel the body"
);
let tasks = Arc::clone(ctx.coordinator().tasks());
tasks.abort_all();
tasks.wait_idle().await;
assert!(dropped.load(Ordering::SeqCst), "abort_all cancels the body");
}
#[test]
fn handle_of_a_set_that_never_ran() {
let mut set = BranchSet::new();
let handle = set.add("inventory", |_ctx| async { Ok(1_u32) });
drop(set);
let Err(err) = handle.into_result() else {
panic!("a handle whose set never ran has no result");
};
let crate::OperationErrorKind::ChildContext(child) = err.kind() else {
panic!("the error is a child context error: {err:?}");
};
assert!(matches!(
child.kind(),
crate::ChildContextErrorKind::NotCompleted
));
assert_eq!(child.kind().to_string(), "branch has no result");
let source = std::error::Error::source(child).expect("the error has a source");
assert_eq!(
source.to_string(),
r#"parallel branch "inventory" has no result"#
);
}
#[test]
fn empty_branch_name_records_the_default_name() {
let mut set = BranchSet::default();
let handle = set.add("", |_ctx| async { Ok(1_u32) });
drop(set);
let err = handle.into_result().expect_err("no result");
let source = std::error::Error::source(&err)
.and_then(std::error::Error::source)
.map(ToString::to_string);
assert_eq!(
source.as_deref(),
Some(r#"parallel branch "parallel-branch-0" has no result"#)
);
}
#[test]
fn branch_set_and_handle_debug() {
let mut set = BranchSet::new();
assert_eq!(format!("{set:?}"), "BranchSet { names: [], .. }");
let handle = set.add("a", |_ctx| async { Ok(1_u32) });
let _other = set.add_with_serdes("b", crate::JsonSerdes, |_ctx| async { Ok(2_u32) });
assert_eq!(format!("{set:?}"), r#"BranchSet { names: ["a", "b"], .. }"#);
assert_eq!(
format!("{handle:?}"),
r#"BranchHandle { name: "a", has_result: false, .. }"#
);
*lock_slot(&handle.slot) = Some(Ok(1));
assert_eq!(
format!("{handle:?}"),
r#"BranchHandle { name: "a", has_result: true, .. }"#
);
}
fn same_branch<T>(a: &BranchKey<T>, b: &BranchKey<T>) -> bool {
Arc::ptr_eq(&a.identity, &b.identity) && a.index == b.index
}
#[test]
fn branch_keys_name_their_branch_and_set() {
fn assert_clone_send_sync<K: Clone + Send + Sync>() {}
assert_clone_send_sync::<BranchKey<std::cell::Cell<String>>>();
let mut first = BranchSet::new();
let a = first.add("a", |_ctx| async { Ok(1_u32) });
let b = first.add("b", |_ctx| async { Ok(2_u32) });
let mut second = BranchSet::new();
let other_a = second.add("a", |_ctx| async { Ok(1_u32) });
assert!(same_branch(&a.key(), &a.key()));
assert!(same_branch(&a.key(), &a.key().clone()));
assert!(!same_branch(&a.key(), &b.key()));
assert!(!same_branch(&a.key(), &other_a.key()));
assert_eq!(format!("{:?}", b.key()), "BranchKey { index: 1, .. }");
}
}