mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
5.5 KiB
161 lines
5.5 KiB
8 years ago
|
# gRPC Error
|
||
8 years ago
|
|
||
|
## Background
|
||
|
|
||
8 years ago
|
`grpc_error` is the c-core's opaque representation of an error. It holds a
|
||
|
collection of integers, strings, timestamps, and child errors that related to
|
||
|
the final error.
|
||
8 years ago
|
|
||
8 years ago
|
always present are:
|
||
8 years ago
|
|
||
8 years ago
|
* GRPC_ERROR_STR_FILE and GRPC_ERROR_INT_FILE_LINE - the source location where
|
||
|
the error was generated
|
||
|
* GRPC_ERROR_STR_DESCRIPTION - a human readable description of the error
|
||
|
* GRPC_ERROR_TIME_CREATED - a timestamp indicating when the error happened
|
||
|
|
||
|
An error can also have children; these are other errors that are believed to
|
||
|
have contributed to this one. By accumulating children, we can begin to root
|
||
|
cause high level failures from low level failures, without having to derive
|
||
|
execution paths from log lines.
|
||
|
|
||
|
grpc_errors are refcounted objects, which means they need strict ownership
|
||
|
semantics. An extra ref on an error can cause a memory leak, and a missing ref
|
||
|
can cause a crash.
|
||
|
|
||
|
This document serves as a detailed overview of grpc_error's ownership rules. It
|
||
|
should help people use the errors, as well as help people debug refcount related
|
||
|
errors.
|
||
8 years ago
|
|
||
|
## Clarification of Ownership
|
||
|
|
||
8 years ago
|
If a particular function is said to "own" an error, that means it has the
|
||
|
responsibility of calling unref on the error. A function may have access to an
|
||
|
error without ownership of it.
|
||
8 years ago
|
|
||
8 years ago
|
This means the function may use the error, but must not call unref on it, since
|
||
|
that will be done elsewhere in the code. A function that does not own an error
|
||
|
may explicitly take ownership of it by manually calling GRPC_ERROR_REF.
|
||
8 years ago
|
|
||
|
## Ownership Rules
|
||
|
|
||
|
There are three rules of error ownership, which we will go over in detail.
|
||
8 years ago
|
|
||
|
* If `grpc_error` is returned by a function, the caller owns a ref to that
|
||
|
instance.
|
||
|
* If a `grpc_error` is passed to a `grpc_closure` callback function, then that
|
||
|
function does not own a ref to the error.
|
||
|
* if a `grpc_error` is passed to *any other function*, then that function
|
||
|
takes ownership of the error.
|
||
8 years ago
|
|
||
|
### Rule 1
|
||
|
|
||
8 years ago
|
> If `grpc_error` is returned by a function, the caller owns a ref to that
|
||
|
> instance.*
|
||
8 years ago
|
|
||
8 years ago
|
For example, in the following code block, error1 and error2 are owned by the
|
||
|
current function.
|
||
8 years ago
|
|
||
|
```C
|
||
8 years ago
|
grpc_error* error1 = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Some error occured");
|
||
8 years ago
|
grpc_error* error2 = some_operation_that_might_fail(...);
|
||
|
```
|
||
|
|
||
8 years ago
|
The current function would have to explicitly call GRPC_ERROR_UNREF on the
|
||
|
errors, or pass them along to a function that would take over the ownership.
|
||
8 years ago
|
|
||
|
### Rule 2
|
||
|
|
||
8 years ago
|
> If a `grpc_error` is passed to a `grpc_closure` callback function, then that
|
||
|
> function does not own a ref to the error.
|
||
8 years ago
|
|
||
|
A `grpc_closure` callback function is any function that has the signature:
|
||
|
|
||
|
```C
|
||
|
void (*cb)(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error);
|
||
|
```
|
||
|
|
||
|
This means that the error ownership is NOT transferred when a functions calls:
|
||
|
|
||
|
```C
|
||
|
c->cb(exec_ctx, c->cb_arg, err);
|
||
|
```
|
||
|
|
||
|
The caller is still responsible for unref-ing the error.
|
||
|
|
||
8 years ago
|
However, the above line is currently being phased out! It is safer to invoke
|
||
|
callbacks with `grpc_closure_run` and `grpc_closure_sched`. These functions are
|
||
|
not callbacks, so they will take ownership of the error passed to them.
|
||
8 years ago
|
|
||
|
```C
|
||
8 years ago
|
grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Some error occured");
|
||
8 years ago
|
grpc_closure_run(exec_ctx, cb, error);
|
||
|
// current function no longer has ownership of the error
|
||
|
```
|
||
|
|
||
8 years ago
|
If you schedule or run a closure, but still need ownership of the error, then
|
||
|
you must explicitly take a reference.
|
||
8 years ago
|
|
||
|
```C
|
||
8 years ago
|
grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Some error occured");
|
||
8 years ago
|
grpc_closure_run(exec_ctx, cb, GRPC_ERROR_REF(error));
|
||
|
// do some other things with the error
|
||
|
GRPC_ERROR_UNREF(error);
|
||
|
```
|
||
|
|
||
8 years ago
|
Rule 2 is more important to keep in mind when **implementing** `grpc_closure`
|
||
|
callback functions. You must keep in mind that you do not own the error, and
|
||
|
must not unref it. More importantly, you cannot pass it to any function that
|
||
|
would take ownership of the error, without explicitly taking ownership yourself.
|
||
|
For example:
|
||
8 years ago
|
|
||
|
```C
|
||
|
void on_some_action(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
|
||
|
// this would cause a crash, because some_function will unref the error,
|
||
|
// and the caller of this callback will also unref it.
|
||
|
some_function(error);
|
||
|
|
||
8 years ago
|
// this callback function must take ownership, so it can give that
|
||
8 years ago
|
// ownership to the function it is calling.
|
||
|
some_function(GRPC_ERROR_REF(error));
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Rule 3
|
||
|
|
||
8 years ago
|
> if a `grpc_error` is passed to *any other function*, then that function takes
|
||
|
> ownership of the error.
|
||
8 years ago
|
|
||
|
Take the following example:
|
||
|
|
||
|
```C
|
||
8 years ago
|
grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Some error occured");
|
||
8 years ago
|
// do some things
|
||
|
some_function(error);
|
||
|
// can't use error anymore! might be gone.
|
||
|
```
|
||
|
|
||
8 years ago
|
When some_function is called, it takes over the ownership of the error, and it
|
||
|
will eventually unref it. So the caller can no longer safely use the error.
|
||
8 years ago
|
|
||
8 years ago
|
If the caller needed to keep using the error (or passing it to other functions),
|
||
|
if would have to take on a reference to it. This is a common pattern seen.
|
||
8 years ago
|
|
||
|
```C
|
||
|
void func() {
|
||
8 years ago
|
grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Some error");
|
||
8 years ago
|
some_function(GRPC_ERROR_REF(error));
|
||
|
// do things
|
||
|
some_other_function(GRPC_ERROR_REF(error));
|
||
|
// do more things
|
||
|
some_last_function(error);
|
||
|
}
|
||
|
```
|
||
|
|
||
8 years ago
|
The last call takes ownership and will eventually give the error its final
|
||
|
unref.
|
||
8 years ago
|
|
||
8 years ago
|
When **implementing** a function that takes an error (and is not a
|
||
|
`grpc_closure` callback function), you must ensure the error is unref-ed either
|
||
|
by doing it explicitly with GRPC_ERROR_UNREF, or by passing the error to a
|
||
|
function that takes over the ownership.
|