Polling Async Tasks

Some operations on Stream's API take longer than a single HTTP response can wait for. Hard-delete-channels, channel export, user export, and similar batch jobs return a task_id immediately and run in the background. To get the result, poll the task status endpoint.

The SDK ships a helper that polls for you and surfaces the outcome as either a typed result (when the task completes) or a typed exception (when it fails or the wait elapses).

Waiting on a task

using GetStream;

try
{
    var result = await client.WaitForTaskAsync(taskId);
    // result.Status == "completed"
}
catch (GetStreamTaskException ex)
{
    // task ended with status: failed
    Console.WriteLine($"task {ex.TaskId} failed: {ex.Description}");
}
catch (GetStreamTransportException ex) when (ex.ErrorType == "timeout")
{
    // wait elapsed; task may still be running on the server
}

Behavior

Task outcomeHelper's reaction
status: "completed"Returns the task result payload.
status: "failed"Raises the SDK's task exception with task_id, error_type, description, stack_trace, version.
Deadline exceededRaises the SDK's transport exception with error_type = "timeout". The task may still be running on the server.

Java exposes the server-side stack trace via getStackTraceText(), not getStackTrace(). The latter is reserved for the JVM's own Throwable.getStackTrace() (StackTraceElement[]).

Defaults

ParameterDefault
Poll interval1 second
Wait timeout60 seconds

Override either knob if your task is expected to run longer, or if you want a tighter loop.

await client.WaitForTaskAsync(taskId, TimeSpan.FromSeconds(5), TimeSpan.FromMinutes(10));

Polling manually

If you need a custom polling loop (back-off, progress logging, external cancellation), call getTask yourself. The helper is a convenience over the same endpoint.

import time

while True:
    response = client.get_task(task_id)
    if response.data.status in ("completed", "failed"):
        break
    time.sleep(1.0)

The Async variant in your SDK (e.g. Python's AsyncStream.wait_for_task, .NET's WaitForTaskAsync) is non-blocking and accepts the same parameters.