Structured concurrency in practice: cancellation, timeouts, context propagation.
- User
Structured concurrency in practice: cancellation, timeouts, context propagation.
- Assistant
We are going to demonstrate structured concurrency with cancellation, timeouts, and context propagation in Go. We'll use the
contextpackage for cancellation and timeouts, and we'll manage goroutines in a way that they are all done when the main function returns.Steps:
- Create a parent context that will be used to cancel all child goroutines.
- Spawn multiple goroutines that do some work and respect the context for cancellation.
- Use a
sync.WaitGroupto wait for all goroutines to finish, but also make sure that we cancel the context when we are done. - Demonstrate a timeout by using
context.WithTimeout. - Propagate context to all child goroutines so they can be cancelled together.
We are going to simulate some work with
time.Sleepand check context done channel.Let's write an example that:
- Creates a context with a timeout (say 2 seconds).
- Spawns 3 workers that each try to do some work (simulated by sleeping for different amounts of time).
- The work function should periodically check the context to see if it's cancelled.
We want to see that when the timeout occurs, all workers should stop.
Alternatively, we can also demonstrate manual cancellation by pressing a key or after a certain condition.
However, note: the example should be self-contained and run without user input.
Let's do:
- A function
workerthat takes a context, an id, and a duration to sleep (simulating work). - The worker will sleep in small intervals and check the context.
We'll also show how to propagate values with context (like a request ID) but we'll keep it simple.
But note: the problem says "context propagation", so we'll show how to pass a value (like a trace ID) through context.
Let's do:
type key string const traceKey key = "trace"
Then we'll create a context with a value and pass it to workers.
Steps in code:
- Create a context with a timeout and a value (trace ID).
- Create a waitgroup for 3 workers.
- For each worker, start a goroutine that:
- extracts the trace ID from context and logs it.
- does work in a loop of small sleeps (say 100ms) until the total work time is reached or context is done.
- Wait for all workers to finish (either by completing or cancellation).
- Print when the main function exits.
However, note: if the timeout is 2 seconds and one worker is supposed to work for 3 seconds, it should be interrupted.
Let's code accordingly.
Important: We must call wg.Done() in each goroutine exactly once.
Let's write the example.
Structured Concurrency in Practice: Cancellation, Timeouts, Context Propagation
Structured concurrency ensures all concurrent operations complete before the scope exits, preventing "leaked" goroutines. Here's practical implementation in Go with examples:
Core Principles
- Child goroutines complete before parent returns
- Cancellation propagates downward
- Errors are aggregated and handled at the parent level
1. Basic Structured Concurrency Pattern
package main import ( "context" "fmt" "sync" "time" ) // worker performs a task with cancellation support func worker(ctx context.Context, id int, duration time.Duration) error { select { case <-time.After(duration): fmt.Printf("Worker %d completed after %v\n", id, duration) return nil case <-ctx.Done(): fmt.Printf("Worker %d cancelled: %v\n", id, ctx.Err()) return ctx.Err() } } func main() { // Parent context controls all children ctx, cancel := context.WithCancel(context.Background()) defer cancel() // Ensure cleanup on exit var wg sync.WaitGroup wg.Add(3) errors := make(chan error, 3) for i := 1; i <= 3; i++ { go func(id int) { defer wg.Done() if err := worker(ctx, id, time.Duration(2+id)*time.Second); err != nil { errors <- fmt.Errorf("worker %d: %w", id, err) } }(i) } // Wait for all workers (structured) wg.Wait() close(errors) // Collect errors (if any) for err := range errors { fmt.Println("Error:", err) } fmt.Println("All workers completed before main exits") }
2. Timeout Implementation
A. Single Operation Timeout
func operationWithTimeout(timeout time.Duration) (string, error) { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() result := make(chan string, 1) go func() { // Simulate long operation time.Sleep(3 * time.Second) result <- "operation complete" }() select { case res := <-result: return res, nil case <-ctx.Done(): return "", fmt.Errorf("timeout after %v: %w", timeout, ctx.Err()) } }B. Group Timeout (All Operations Must Complete)
func allOperationsMustComplete(timeout time.Duration) error { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() var wg sync.WaitGroup operations := []func() error{ func() error { time.Sleep(1 * time.Second); return nil }, func() error { time.Sleep(2 * time.Second); return nil }, func() error { time.Sleep(3 * time.Second); return nil }, // Will timeout } errCh := make(chan error, len(operations)) wg.Add(len(operations)) for i, op := range operations { go func(i int, op func() error) { defer wg.Done() if err := op(); err != nil { select { case errCh <- fmt.Errorf("operation %d: %w", i, err): case <-ctx.Done(): // Parent timed out } } }(i, op) } // Wait for either all operations or timeout go func() { wg.Wait() close(errCh) }() // Collect errors while respecting timeout for err := range errCh { fmt.Println("Operation failed:", err) } return ctx.Err() // Returns timeout if applicable }
3. Context Propagation Pattern
// Key types for context values type contextKey string const ( userIDKey contextKey = "userID" requestIDKey contextKey = "requestID" traceIDKey contextKey = "traceID" ) // Propagate context through call chain func handler(ctx context.Context) { // Add values to context ctx = context.WithValue(ctx, userIDKey, "user123") ctx = context.WithValue(ctx, requestIDKey, "req-456") // Pass to service layer if err := processRequest(ctx); err != nil { // Handle error with full context logRequest(ctx, err) } } func processRequest(ctx context.Context) error { // Extract values (type-safe retrieval) userID, _ := ctx.Value(userIDKey).(string) reqID, _ := ctx.Value(requestIDKey).(string) fmt.Printf("Processing request %s for user %s\n", reqID, userID) // Call repository with same context return repository.Query(ctx, "SELECT * FROM data") } func repository.Query(ctx context.Context, query string) error { // Database call with context for cancellation // If parent is cancelled, this cancels too return db.QueryContext(ctx, query) }
4. Complete Structured Concurrency Framework
// Structured group for managing goroutines type Group struct { wg sync.WaitGroup ctx context.Context cancel context.CancelFunc errCh chan error once sync.Once maxErrs int // Limit error accumulation } func NewGroup(parent context.Context, maxErrs int) *Group { ctx, cancel := context.WithCancel(parent) return &Group{ ctx: ctx, cancel: cancel, errCh: make(chan error, maxErrs), maxErrs: maxErrs, } } func (g *Group) Go(f func() error) { g.wg.Add(1) go func() { defer g.wg.Done() if err := f(); err != nil { g.once.Do(func() { // First error cancels the group g.cancel() }) select { case g.errCh <- err: case <-g.ctx.Done(): } } }() } func (g *Group) Wait() error { g.wg.Wait() close(g.errCh) var errs []error for err := range g.errCh { errs = append(errs, err) if len(errs) >= g.maxErrs { break } } if len(errs) > 0 { return fmt.Errorf("group failed: %w", joinErrors(errs)) } return g.ctx.Err() // Return cancellation/timeout if any } func joinErrors(errs []error) error { if len(errs) == 1 { return errs[0] } var msg []byte for i, err := range errs { if i > 0 { msg = append(msg, "; "...) } msg = append(msg, err.Error()...) } return errors.New(string(msg)) } // Usage example func main() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() g := NewGroup(ctx, 10) // Launch multiple operations for i := 0; i < 5; i++ { i := i g.Go(func() error { return doWork(ctx, i) }) } if err := g.Wait(); err != nil { fmt.Printf("Group failed: %v\n", err) } }
5. Best Practices Checklist
✅ DO:
- Use
context.Contextas first parameter - Call
cancel()indeferimmediately afterWithCancel/WithTimeout - Always check
ctx.Done()in loops - Use
Grouppattern for related goroutines - Propagate context through entire call chain
- Set reasonable timeouts (avoid indefinite operations)
❌ AVOID:
- Storing contexts in structs (they're immutable, pass as parameter)
- Using
context.Background()in libraries (accept from caller) - Creating contexts without cancellation (except root)
- Ignoring errors from
<-ctx.Done()(they indicate real problems)
6. Real-World Example: Parallel API Calls
func fetchUserData(ctx context.Context, userIDs []string) (map[string]*User, error) { g := NewGroup(ctx, len(userIDs)) results := make(chan map[string]*User, 1) mu := sync.Mutex{} userMap := make(map[string]*User) for _, id := range userIDs { id := id g.Go(func() error { user, err := apiClient.GetUser(ctx, id) if err != nil { return fmt.Errorf("user %s: %w", id, err) } mu.Lock() userMap[id] = user mu.Unlock() return nil }) } if err := g.Wait(); err != nil { return nil, err } return userMap, nil }
7. Testing Cancellation
func TestCancellation(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) done := make(chan struct{}) go func() { // Long operation that respects cancellation for { select { case <-ctx.Done(): close(done) return default: time.Sleep(50 * time.Millisecond) } } }() // Cancel after 100ms time.AfterFunc(100*time.Millisecond, cancel) select { case <-done: // Pass - cancelled properly case <-time.After(1 * time.Second): t.Fatal("operation didn't respect cancellation") } }
Key Takeaways
- Structured: All goroutines complete in well-defined scope
- Cancellation: Context propagates cancellation downward automatically
- Timeouts: Use
WithTimeout/WithDeadlinefor bounded operations - Error Handling: Aggregate errors from child operations
- Resource Safety:
defer cancel()prevents context leaks
This pattern prevents goroutine leaks, ensures deterministic cleanup, and makes concurrent code predictable and maintainable. The
Groupabstraction is particularly valuable for managing multiple related concurrent operations with unified cancellation.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.