App-vNext/Polly · error · InvalidOperationException
TaskExecution is not initialized.
Error message
TaskExecution is not initialized.
What it means
TaskExecution<T>.Context throws InvalidOperationException when _activeContext is null, i.e. before InitializeAsync has run. TaskExecution is an internal hedging controller primitive that represents one hedged attempt; its Context is only valid between InitializeAsync and ResetAsync. Accessing Context outside that window violates the controller's lifecycle contract.
Source
Thrown at src/Polly.Core/Hedging/Controller/TaskExecution.cs:58
}
/// <summary>
/// Gets the task that represents the execution of the hedged task.
/// </summary>
/// <remarks>
/// This property is not-null once the <see cref="TaskExecution{T}"/> is initialized.
/// Awaiting this task will never throw as all exceptions are caught and stored
/// into <see cref="Outcome"/> property.
/// </remarks>
public Task? ExecutionTaskSafe { get; private set; }
public Outcome<T> Outcome { get; private set; }
public bool IsHandled { get; private set; }
public bool IsAccepted { get; private set; }
public ResilienceContext Context => _activeContext ?? throw new InvalidOperationException("TaskExecution is not initialized.");
public HedgedTaskType Type { get; set; }
public Action<TaskExecution<T>>? OnReset { get; set; }
public TimeSpan ExecutionTime => _timeProvider.GetElapsedTime(_startExecutionTimestamp, _stopExecutionTimestamp);
public int AttemptNumber { get; private set; }
public void AcceptOutcome()
{
if (ExecutionTaskSafe?.IsCompleted == true)
{
IsAccepted = true;
}
else
{
throw new InvalidOperationException("Unable to accept outcome for a task that is not completed.");View on GitHub (pinned to d0e46bdb1e)
Solutions
- Ensure InitializeAsync is awaited before any access to TaskExecution.Context; treat the object as invalid after ResetAsync.
- If writing a custom HedgingHandler.ActionGenerator, only read Context from the TaskExecution you receive after the controller invokes your generator (it is initialized by then).
- Avoid reflecting into or caching TaskExecution instances; let the HedgingResilienceStrategy own the lifecycle.
Defensive patterns
Strategy: validation
Prevention
- TaskExecution is internal infrastructure; do not reflect into or cache it.
- In a custom HedgingHandler.ActionGenerator, only read Context from the TaskExecution handed to you after invocation.
- Treat TaskExecution as invalid after ResetAsync; never access Context across the reset boundary.
When it happens
Trigger: Internal code reading the Context property before calling InitializeAsync, or after ResetAsync has cleared _activeContext. Practically surfaces only through Polly's hedging internals or a custom HedgingHandler.ActionGenerator that misuses the handed-off TaskExecution.
Common situations: A custom action generator that inspects taskExecution.Context inside its factory before the controller has initialized the attempt; a bug in a fork or in code reaching into internals via reflection; race where ResetAsync runs before a telemetry callback reads Context.
Related errors
- Unable to accept outcome for a task that is not completed.
- Cannot add any more resilience strategies to the builder aft
- This instance of 'CircuitBreakerStateProvider' is already in
- No predicates were configured. There must be at least one pr
- Disposing this resilience pipeline is not allowed because it
AI-assisted analysis of App-vNext/Polly@d0e46bdb1e (2026-08-13).
Data as JSON: /api/errors/f9fd04198ac8e443.
Report an issue: GitHub.