Skip to content

Unbuffered channel in Suspense example could leak goroutines #1352

Description

@scrhartley

The Suspense example uses goroutines which will finish when the channel is read. If the channel is never read (or only partially read) then goroutines will never finish. A buffered channel should be used so that the goroutines can finish without requiring the channel to be consumed. Although the example itself is OK, since it's referenced by the docs it should be able to be copy/pasted without errors potentially being introduced.

A similar pattern is described in the official Go blog article Go Concurrency Patterns: Pipelines and cancellation and it warns:

goroutines consume memory and runtime resources, and heap references in goroutine stacks keep data from being garbage collected. Goroutines are not garbage collected; they must exit on their own.

The alternative strategy is to use a done channel which would be closed by the http handler function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions