fix(metrics): Prevent race condition crash during metrics collection on shutdown#3741
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: adduali1310 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
good catch! thank you /milestone 0.43.0 |
|
Hey @adduali1310 , sorry, but we had experienced some delay due to rate-limiting issues. The change looks good, even if that |
213f8d3 to
5eb3468
Compare
|
Apologies for the delay @ekoops . Separated print_stats into a separate action as requested and is also called separately in the teardown sequence. |
Hey! Sorry, we were busy with the new release. Now we can work for merging this. I see tests failure in CI, but I haven't had time to go through it. Can you rebase on top of master? I hope this fill fix the issue. If the CI become green, I'll definitely merge this (notice that the failing jobs are marked as "required"). |
3bda69d to
0730de3
Compare
|
Hey @ekoops So took me a while to get to the bottom of things but quick update on the CI status after rebasing. I ran into a couple of issues.
This changed the computed engine checksum. I built locally and confirmed by diffing falco --list-events output between master and my branch.
These tests run Falco with -M 3 and check stderr for the "Events detected:" string from print_stats(). The problem is a teardown ordering conflict:
Swapping the order would fix the test but breaks the original intent on master, s.outputs.reset() was intentionally called before print_stats() to flush the output queue and avoid intermixed output. So s.outputs isn't a viable guard since it's always null by the time print_stats needs to run, and there's no clean flag in state to indicate "we actually processed events." I think the simplest correct fix is to move print_stats() back into process_events() (at the end, before the return) and only keep cleanup_outputs as the separate teardown action. print_stats() itself doesn't have the race condition with the webserver — only s.outputs.reset() does. This preserves the original ordering (reset outputs, then print stats) and the teardown safety. Unless you have a better or alternative guard in mind to keep print_stats as a separate teardown action let me know, how you would prefer to proceed. |
|
Mmm I see. Yes please, let's bring back |
…on shutdown This fixes a segmentation fault that occurs when /metrics endpoint is accessed during Falco shutdown. The crash happens as the webserver continues serving /metrics requests after outputs and inspectors have been destroyed. Changes: - Create cleanup_outputs action to handle outputs destruction - Create print_stats action for stats printing - Reorder teardown steps to stop webserver before destorying outputs - Move outputs.reset() from process_events to cleanup_outputs() This eliminates the race condition by ensuring the webserver stops accepting requests before any subsystems are destroyed. The synchronisation behaviour of output.reset() block till queue flushed is preserved. Signed-off-by: Adnan Ali <adduali1310@hotmail.com>
0730de3 to
8a2f071
Compare
|
Hey @ekoops Made the requested change. Could you take a look when possible? I would presume rerunning this test should fix it. |
Job re-launched. Let's see. |
|
Seems to have worked? @leogr |
This fixes a segmentation fault that occurs when /metrics endpoint is accessed during Falco shutdown. The crash happens as there is a very minute window, wherein the webserver continues serving /metrics requests after outputs have been destroyed.
The race condition is timing-dependent and occurs in this window:
/metricsrequestsChanges:
This eliminates the race condition by ensuring the webserver stops accepting requests before any subsystems are destroyed. The synchronisation behaviour of output.reset() block till queue flushed is preserved.
What type of PR is this?
/kind bug
Any specific area of the project related to this PR?
/area engine
What this PR does / why we need it:
Fixes a race condition that causes a segmentation fault during Falco shutdown when prometheus metrics endpoint is accessed. The crash occurs in a very short time window before the webserver is stopped but after outputs are destroyed.
Due to the destruction, accessing them leads to a SEGV and invalid access.
The fix reorders the shutdown sequence to stop the webserver before destroying the subsystems.
Which issue(s) this PR fixes:
Fixes #3739
Special notes for your reviewer:
Created a simple bash script to reproduce the issue.
Before Fix:
After Fix:
Post the fix in this PR, can confirm haven't seen the crashes.
Can be confirmed by running the script aggressively as well.
Does this PR introduce a user-facing change?: