Implementation:Triton inference server Server CommonUtils
| Knowledge Sources | |
|---|---|
| Domains | Utilities, Error_Handling |
| Last Updated | 2026-02-13 17:00 GMT |
Overview
Foundational utility library providing shared constants, error-handling macros, and helper functions used across all Triton server components.
Description
common.h and common.cc form the foundational utility layer of the Triton server. The header defines HTTP header constants, server limits (max gRPC message size, JSON nesting depth, default max input size), and the critical error-handling macro family (RETURN_IF_ERR, FAIL_IF_ERR, THROW_IF_ERR, IGNORE_ERR). The implementation provides model version string parsing, environment variable retrieval, tensor shape formatting, element count calculation with overflow protection, Base64 decoding using libb64, and shared memory key validation.
Usage
Included by nearly every server source file. The macros and utility functions provide the common vocabulary for error handling and data manipulation throughout the codebase.
Code Reference
Source Location
- Repository: Triton Inference Server
- File: src/common.h
- Lines: 1-286
- File: src/common.cc
- Lines: 1-201
Signature
namespace triton { namespace server {
// Error-handling macros
#define RETURN_IF_ERR(X) /* returns TRITONSERVER_Error* if X fails */
#define FAIL_IF_ERR(X, MSG) /* logs and exits if X fails */
#define THROW_IF_ERR(X) /* throws exception if X fails */
#define IGNORE_ERR(X) /* deletes error without action */
// Utility functions
TRITONSERVER_Error* GetModelVersionFromString(
const std::string& version_string, int64_t* version);
const char* GetEnvironmentVariableOrDefault(
const char* variable_name, const char* default_value);
std::string ShapeToString(
const int64_t* dims, size_t dim_count);
int64_t GetElementCount(
const int64_t* dims, size_t dim_count);
TRITONSERVER_Error* Base64Decode(
const char* encoded, size_t encoded_len,
std::vector<char>* decoded);
TRITONSERVER_Error* ValidateSharedMemoryKey(
const std::string& shm_key);
// Constants
constexpr int64_t kMaxGRPCMessageSize = INT32_MAX;
constexpr int kMaxJsonNestingDepth = 64;
}} // namespace triton::server
Import
#include "common.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| version_string | string | Varies | Model version as string (e.g., "1", "latest") |
| dims | int64_t* | Varies | Tensor shape dimensions |
| encoded | const char* | Varies | Base64-encoded data |
Outputs
| Name | Type | Description |
|---|---|---|
| version | int64_t | Parsed model version number |
| shape_string | string | Human-readable shape (e.g., "[3,224,224]") |
| element_count | int64_t | Total elements in tensor (-1 on overflow) |
| decoded | vector<char> | Base64-decoded bytes |
Usage Examples
Error Handling with Macros
#include "common.h"
TRITONSERVER_Error* ProcessRequest() {
// Return early if any step fails
RETURN_IF_ERR(ValidateInput());
RETURN_IF_ERR(RunInference());
RETURN_IF_ERR(FormatOutput());
return nullptr; // success
}
void SafeCleanup() {
// Log error but don't propagate
FAIL_IF_ERR(
TRITONSERVER_ServerDelete(server),
"Failed to delete server");
}