Skip to content

Commit af625bf

Browse files
committed
Improve crash groups guide
1 parent 75d8a08 commit af625bf

1 file changed

Lines changed: 9 additions & 39 deletions

File tree

guides/useful_concepts/crash_groups.md

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,7 @@ end
3333
When an element belonging to a crash group crashes:
3434
1. All other elements in the same crash group are terminated by the pipeline.
3535
2. The pipeline's `handle_crash_group_down/3` callback is invoked.
36-
3. You can decide whether to restart the group, ignore the failure, or terminate the pipeline.
37-
38-
## Handling Failures
39-
40-
To react to a crash group failure, implement the `handle_crash_group_down/3` callback:
41-
42-
```elixir
43-
@impl true
44-
def handle_crash_group_down(crash_group_id, context, state) do
45-
# Logic to restart the group or handle the error
46-
end
47-
```
36+
3. You can decide whether to restart the group, ignore the failure, or handle this situation any other way.
4837

4938
## Flow of all callbacks reated to a crash within a crash group.
5039

@@ -64,7 +53,7 @@ end
6453
`context` passed to this callback will contain few extra fileds:
6554
* `context.exit_reason` - in this case equals `{%RuntimeError{message: "internal error"}, _stacktrace}`.
6655
* `context.group_name` - because `:filter` was spawned inside `:my_group` group, it equals `:my_group`. If a child is spawned beyond any crash group and is terminated gracefully, value of this field is `nil`.
67-
* `context.crash_initiator` - the same as child's reference, that is `:filter`.
56+
* `context.crash_initiator` - the same as child's reference, which is `:filter`.
6857

6958
`:filter` won't be present in `context.children`, so you could respawn it here, however it is suggested to do it in `handle_crash_group_down/3` later.
7059

@@ -82,10 +71,9 @@ end
8271
```
8372

8473
callback. Each time, `context` will contain following extra fields:
85-
* `context.exit_reason` - for these terminations, equals `{:shutdown, :membrane_crash_group_kill}`.
74+
* `context.exit_reason` - equals `{:shutdown, :membrane_crash_group_kill}`.
8675
* `context.group_name` - equals `:my_group`.
87-
* `context.crash_initiator` - TODO: continue
88-
76+
* `context.crash_initiator` - equals `:filter`.
8977

9078
Note, that `context.children` map always contains only children that are still alive. E.g. if `:source` is terminated first, `hanlde_child_terminated(:source, context, state)` will contain only `:sink` in `context.children` map and for `hanlde_child_terminated(:sink, context, state)` `context.children` will be empty.
9179

@@ -102,27 +90,9 @@ def handle_crash_group_down(:my_group, context, state) do
10290
end
10391
```
10492

105-
`context` passed as a third argument to `handle_crash_group_down/3` callback contains 3 additional fields, that usually don't occur in contexts of other callbacks:
106-
- `context.crash_initiator` - name or reference of the child that crashed first and caused the crash group to explode.
107-
- `context.crash_reason` - the reason with which `context.crash_initiator` crashed.
108-
- `context.members` - names/references of all children that were in the crash group.
109-
TODO: continue
110-
111-
112-
Question marks:
113-
- is crash group initiator in context.members?
114-
- what is the order in of:
115-
* `handle_crash_group_down`
116-
* `handle_child_terminated`
117-
* removing children from `context.children`, so that it becames possible to respawn new children with the same names?
118-
- what is the relation between `handle_child_pad_removed` and `handle_crash_group_down`?
119-
120-
121-
122-
## Use Cases
123-
## tutaj poniej mamy AI BS, no chodzi o to ze jak mamy element co sie wydupca, to nie chcemy zeby wszystko poszlo w piach, wiec wrzucamy go (i byc moze cos co i tak bysmy chcieli razem z nim zrestartowac) do crash groupy
124-
125-
- **Atomic logical units:** When a group of elements (like an encoder and its associated parser) cannot function independently.
126-
- **Resource Cleanup:** Ensuring that if a consumer crashes, the producer is also stopped to prevent buffered data from leaking memory.
127-
- **Error Recovery:** Grouping elements that require a specific initialization sequence that must be repeated upon failure.
93+
`context` passed as a third argument to `handle_crash_group_down/3` callback contains 3 additional fields:
94+
- `context.crash_initiator` - name or reference of the child that crashed first and caused the crash group to explode. In this case, equals `:filter`
95+
- `context.crash_reason` - the reason with which `context.crash_initiator` crashed. In this case, equals `{%RuntimeError{message: "internal error"}, _stacktrace}`.
96+
- `context.members` - names/references of all children that were in the crash group. In this case, equals `[:source, :filter, :sink]`
12897

98+
When `handle_crash_group_down/3` is executed, you can be sure that all group members are already terminated. It is a suggested place, to e.g. respawn crash group. Doing so in `handle_child_terminated/3` might lead to some problems, because the order of group members termination might vary. Moreover, if pipeline or bin terminates its children gracefully, using `t:Membrane.Pipline.Action.remove_children()` action, `handle_child_terminated/3` callback will be also executed with `context.exit_reason` set to `normal`.

0 commit comments

Comments
 (0)