Skip to content
Code quality Reviewed 2026-09-13

Non Async Task Null

What does this mean ?

A non-async method declared to return Task or Task<T> should return an actual task according to its contract. A null task is different from a successfully completed task whose result is null. Awaiting a null task fails at runtime.

What can happen ?

Callers can receive a NullReferenceException instead of the documented result or asynchronous failure. This is primarily an API reliability issue; a security consequence depends on the surrounding workflow.

Recommendation

Use Task.CompletedTask for successful no-result completion and Task.FromResult<T> for an already available result. Do not convert a real failure into successful completion. If the API promises a faulted or canceled task, preserve that contract. Enable nullable annotations where supported to document nullable results.

Sample Code

// Incorrect: the task object is null.
static Task<string?> ReadLabelBroken() => null!;

// Correct if “no label” is a valid result.
static Task<string?> ReadLabel() => Task.FromResult<string?>(null);

// Correct for successful work with no result.
static Task Finish() => Task.CompletedTask;

These fragments use C# nullable reference syntax and require System.Threading.Tasks. They do not start background work. Returning Task.FromResult(null) without a known generic type may also require an explicit type argument.

Regression checks

Assert the returned task is non-null and can be awaited. Verify a missing result remains null when allowed. Test failure and cancellation separately so they cannot be mistaken for a successful empty result.

References