tetragon: Teach file writer to exit gracefully #185
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We observed that rarely if we SIGTERM Tetragon (e.g. what pod restart and destroy do before SIGKILL it) that a corrupted json file might get left behind. The theory is this is do to writers being killed in the middle of an operation.
To fix we do two things. First propagate a single context through the processManager and Server objects instead of just using context.Backgroun(). This allows the cancel() call from our SIGTERM handler to be handled by ctx.Done() in the respective go routines. Then we add a waitGroup to the signal handler to ensure it waits until the writer has exited.
I only fix the go routine responsible for writes here. We should audit the other routines as well.
And second I started thinking about how we nest the various objects, at the moment we pass a lot of input types through the New() constructors. But this looked bigger than a fix so held off for now on this.