Skip to content

Infinite unbound loop in analytics processor lead to 100% CPU usage #55

Description

@Totodore

The following code loops indefinitely without pause, this leads to 100% CPU usage in all our environment (dev/qual/prod). We disabled the analytics processor in the meantime.

Solution

Use rx.recv_timeout rather than try_recv which is non blocking.

.spawn(move || {
let mut last_flushed = chrono::Utc::now();
loop {
let data = rx.try_recv();
let mut analytics_data = analytics_data_locked.write().unwrap();
match data {
// Update the analytics data with feature_id received
Ok(feature_name) => {
analytics_data
.entry(feature_name)
.and_modify(|e| *e += 1)
.or_insert(1);
}
Err(flume::TryRecvError::Empty) => {}
Err(flume::TryRecvError::Disconnected) => {
debug!("Shutting down analytics thread ");
break;
}
};
if (chrono::Utc::now() - last_flushed).num_milliseconds() > timer as i64 {
flush(&client, &analytics_data, &analytics_endpoint);
analytics_data.clear();
last_flushed = chrono::Utc::now();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions