Skip to content

Null check for TaskContext in AsyncTaskActivity #1054

@leoquijano

Description

@leoquijano

Hello,

I'm doing an upgrade of DurableTask.Core from an older version to v2.16.1. During testing, I noticed that passing a null TaskContext to an instance of AsyncTaskActivity will raise a NullReferenceException, instead of the expected TaskFailureException.

This happens because the exception handler in that method will try to retrieve the ErrorPropagationMode property from the context parameter without doing a null check.

In TaskActivity.cs:

try
{
    result = await ExecuteAsync(context, parameter);
}
catch (Exception e) when (!Utils.IsFatal(e) && !Utils.IsExecutionAborting(e))
{
    string details = null;
    FailureDetails failureDetails = null;
    if (context.ErrorPropagationMode == ErrorPropagationMode.SerializeExceptions)
    {
        details = Utils.SerializeCause(e, DataConverter);
    }
    else
    {
        failureDetails = new FailureDetails(e);
    }

    throw new TaskFailureException(e.Message, e, details)
        .WithFailureDetails(failureDetails);
}

This means that an issue with the passed TaskContext wouldn't have failure details and might fail if the caller's error handling code is expecting TaskFailureException to be raised. Adding a null check in the if statement would fix the issue:

if (context != null && context.ErrorPropagationMode == ErrorPropagationMode.SerializeExceptions)
{
    details = Utils.SerializeCause(e, DataConverter);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions