﻿# MPI cancellation: complete the request before deciding who owns the buffer

Handle MPI_Cancel as a cancellation request, inspect completion status and distinguish cancelled receives from communications that completed normally.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/mpi-request-cancel-completion/
Language: en

A speculative receiver is no longer needed, so its worker calls MPI_Cancel and immediately recycles the receive buffer. The cancellation call returned, but the communication outcome was not yet established. Correct cleanup requires a protocol for the request and the buffer, including the case where ordinary communication wins.

## Preserve the in-flight ownership record

Associate each outstanding request with its buffer, operation kind, peer selection and application purpose. A timeout in the application does not remove MPI's possible access to that storage. Keep the buffer live until the selected completion protocol permits reuse. The [asynchronous stage ownership guide](/journal/learn-async-shared-copy-pipeline/) explains the adjacent principle of submission versus completion, but MPI adds a cancellation outcome that the application must interpret explicitly.

## Treat cancellation as a request

The [MPI_Cancel reference](https://docs.open-mpi.org/en/main/man-openmpi/man3/MPI_Cancel.3.html) says the call is local and can return before cancellation occurs. It marks a pending nonblocking communication for cancellation. Either cancellation succeeds or communication succeeds, rather than both. Do not use the cancellation return itself as evidence that no message arrived. A matching operation may already have progressed far enough that normal communication completes, leaving the application responsible for handling that result despite its changed intentions.

## Complete and inspect the result

Use a completion path that retains status, such as the documented [MPI_Wait](https://docs.open-mpi.org/en/main/man-openmpi/man3/MPI_Wait.3.html), then examine the cancellation outcome. The [MPI_Test_cancelled reference](https://docs.open-mpi.org/en/main/man-openmpi/man3/MPI_Test_cancelled.3.html) says to check cancellation before other receive-status fields; those fields are undefined after successful cancellation. Avoid discarding status when the cleanup decision needs it. Request-handle removal is not the acceptance evidence chosen here, because the application needs to distinguish cancellation from an ordinarily completed receive before interpreting the buffer.

## Handle both legal outcomes

After successful receive cancellation, the reference specifies that the receive buffer is not altered by that receive. After normal completion, handle or intentionally discard the received data under application rules. Cancellation of a receive also does not satisfy a peer's matching send; the peer may need another matching receive. Design the peer protocol rather than assuming local cleanup establishes global quiescence. The [collective progress guide](/journal/guide-perf-nccl-ras-count-skew/) similarly cautions against inferring remote completion from one local observation.

## Test the race deliberately

Use a disposable exchange where the peer sends before cancellation in one path and does not match the speculative receive in another. Record request identity, completion result, cancellation flag and buffer ownership transition. Check that the normal-completion path does not interpret stale application intent as proof of cancellation. These are proposed contrasting fixtures, not a claim that every run will reproduce a particular race outcome or that production traffic has been interrupted for testing.

## Keep cleanup guarantees narrow

Persistent requests require their own active-versus-inactive lifecycle; successful cancellation does not destroy the persistent request. More broadly, MPI cleanup does not undo external application side effects or cancel unrelated communications. Acceptance means this request reaches a known outcome and the associated buffer is reused only under that outcome's contract. It does not establish that a remote computation stopped, that the peer received a cancellation notification, or that an entire distributed task was rolled back.
