Skip to content

Conversation

@markylaing
Copy link
Contributor

Operations should never be using the request context inside their run function body. The request context may have been cancelled before the operation is started.

When calling operations.OperationCreate, a context is passed in which is used to set the operation Requestor. The original context is not persisted beyond this.

This PR changes the operations.Operation to remove the cancel.Canceller in favour of using a cancellable context. The operation context is derived from the daemon shutdown context and has some key information copied from the input context (e.g. the Requestor).

I've then updated all operations in LXD to use (Operation).Context() wherever a context.Context argument is called for inside an operation.

This is another part of #16613 (comment)

Signed-off-by: Mark Laing <mark.laing@canonical.com>
This commit removes the `cancel.Canceller` from an `Operation` and changes
it back to a `context.Context` with a `context.CancelFunc`. This is so that
the `Operation` retains a context.

The context of the operation is the `Daemon` shutdown context plus key
information that may have been gained before the operation was started,
such as the `request.Requestor`.

Signed-off-by: Mark Laing <mark.laing@canonical.com>
@markylaing markylaing force-pushed the ctx-propagation-operations branch from a47e491 to cc2b118 Compare November 21, 2025 16:52
This commit changes the use of `context.Context` within all operations.
Within an operation, the `(Operation).Context()` should always+ be used.

The outer context, whether it be `(http.Request).Context()` or the
daemon shutdown context, should always be passed into
`operations.OperationCreate`.

+ Except in a revert or defer where we don't want the reverted call to
fail because the context was cancelled.

Signed-off-by: Mark Laing <mark.laing@canonical.com>
@markylaing markylaing force-pushed the ctx-propagation-operations branch from cc2b118 to 608445b Compare November 21, 2025 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant