Context Deadline Exceeded

context deadline exceeded

Quick Answer

The operation did not complete within the context's deadline. Increase the deadline or optimize the slow operation.

Why This Happens

context.DeadlineExceeded is returned when an operation cannot complete before the context's deadline expires. Unlike context.Canceled, this specifically means the timeout was hit, not that someone explicitly cancelled. This is common in HTTP requests, database queries, and RPC calls.

The Problem

func queryDB(ctx context.Context) error {
    ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
    defer cancel()
    // slow query exceeds 100ms deadline
    return db.QueryRowContext(ctx, "SELECT * FROM large_table").Scan(&result)
}

The Fix

func queryDB(ctx context.Context) error {
    ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
    defer cancel()
    return db.QueryRowContext(ctx, "SELECT id FROM large_table WHERE id = $1", targetID).Scan(&result)
}

Step-by-Step Fix

  1. 1

    Identify the slow operation

    Find which operation is taking longer than the deadline allows.

  2. 2

    Determine the cause

    Check if the operation is legitimately slow or if the deadline is unreasonably short.

  3. 3

    Optimize or increase timeout

    Either optimize the slow operation (add indexes, reduce data) or increase the deadline to match realistic needs.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free