Skip to content

Missing defer cancel() in resolveQuery (resolve.go) #3209

Description

@hklcf

Bug Description

In context_cancel.go, the cancel() function from context.WithTimeout is not deferred, so if client.Exchange panics (e.g., due to a nil pointer in the DNS library), the context resources (timer goroutine) will leak until the timeout fires.

Affected Code

resolve.go:59-61:

ctx, cancel := context.WithTimeout(context.Background(), readTimeout)
response, rtt, err := client.Exchange(ctx, msg, "udp", server)
cancel()

Note that other parts of the codebase (e.g., xtransport.go:483, plugin_dns64.go:205) correctly use defer cancel().

Impact

If client.Exchange panics, cancel() is never called, causing a context/timer leak. While rare, this is a correctness issue.

Fix

ctx, cancel := context.WithTimeout(context.Background(), readTimeout)
defer cancel()
response, rtt, err := client.Exchange(ctx, msg, "udp", server)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions