﻿# hipGetLastError versus hipPeekAtLastError: observing a failure can change diagnostic state

Build HIP error reporting around return values, per-thread last-error state and asynchronous completion without clearing the evidence before its owner records it.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-hip-last-error-peek-reset-completion/
Language: en

## Distinguish a returned status from remembered state

A wrapper checks a HIP call's return value, while a later helper asks for the last error. Those are different observations. The [HIP error-handling guide](https://rocmdocs.amd.com/projects/HIP/en/latest/how-to/hip_runtime_api/error_handling.html) describes per-thread remembered error state and the difference between get and peek. Keep the direct status attached to the operation that produced it. A later remembered error should not be silently attributed to whichever source line happened to inspect it.

## Decide who may clear the evidence

hipGetLastError reads and resets the remembered state; hipPeekAtLastError observes without the same reset. The [API reference](https://rocm.docs.amd.com/projects/HIP/en/latest/reference/hip_runtime_api/modules/error_handling.html) is the contract to match against installed headers. If a logging helper consumes the state before a caller records it, the caller can see success and lose the original evidence. Give diagnostic ownership a deliberate place in the wrapper rather than allowing unrelated utilities to clear it incidentally.

## Keep completion separate from launch inspection

A kernel launch can be accepted while device execution has not finished. Observing no immediate last error does not prove numerical success or exclude a later asynchronous failure. Check the appropriate completion boundary and preserve its status separately. Conversely, resetting remembered state is not recovery from an illegal operation. It only changes the diagnostic record. Keep the original failure, the selected synchronization result and any subsequent cleanup errors identifiable instead of overwriting them with one final code.

## Preserve the release-specific meaning

The current programming guide documents a change around ROCm 7.0 in what hipGetLastError remembers. That makes copied expectations from older examples especially risky. Capture the installed release and test the actual intended semantics in a harmless fixture. Use [documented error-code meanings](https://rocm.docs.amd.com/projects/HIP/en/latest/reference/error_codes.html) to interpret a returned status, but do not infer that a human-readable error name identifies the complete causal chain or permits arbitrary retries after every failure.

## Test diagnostic ownership deliberately

Exercise a known rejected API argument without damaging unrelated work, record the direct result, then compare peek and get behavior according to the installed contract. Also test successful completed output in a separate normal path. Related guides explain [HIP portability](/journal/learn-hip-launch-portability/) and [reproducible environments](/journal/software-stack-reproducibility/). The objective is traceable error observation: every recorded code should state which operation and phase it describes, and whether another helper already changed the remembered state.
