Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent global CallError tracker from growing indefinitely #798

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 91 additions & 8 deletions godot-core/src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::global::godot_error;
use crate::meta::error::CallError;
use crate::meta::CallContext;
use crate::sys;
use std::collections::HashMap;
use std::sync::{atomic, Arc, Mutex};
use sys::Global;

Expand All @@ -41,24 +40,57 @@ sys::plugin_registry!(pub __GODOT_PLUGIN_REGISTRY: ClassPlugin);

// Note: if this leads to many allocated IDs that are not removed, we could limit to 1 per thread-ID.
// Would need to check if re-entrant calls with multiple errors per thread are possible.
#[derive(Default)]
struct CallErrors {
map: HashMap<i32, CallError>,
next_id: i32,
ring_buffer: Vec<Option<CallError>>,
next_id: u8,
generation: u16,
}

impl Default for CallErrors {
fn default() -> Self {
Self {
// [None; N] requires Clone.
ring_buffer: std::iter::repeat_with(|| None)
.take(Self::MAX_ENTRIES as usize)
.collect(),
Comment on lines +52 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just been going back through PRs i didnt get a chance to look at but just fyi you can do this now:

[const { None }; N]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is great to know, thanks! I was looking for alternative patterns but couldn't find this, maybe too recent 🙂

Will introduce as a drive-by-commit somewhere 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately with vec![...] it doesn't work:

error: no rules expected the token `const`
  --> godot-core/src/private.rs:53:31
   |
53 |             ring_buffer: vec![const { None }; Self::MAX_ENTRIES as usize],
   |                               ^^^^^ no rules expected this token in macro call
   |
   = note: while trying to match end of macro

And directly None caused the original error:

error[E0277]: the trait bound `Option<call_error::CallError>: Clone` is not satisfied
    --> godot-core/src/private.rs:53:32
     |
53   |             ring_buffer: vec![ None; Self::MAX_ENTRIES as usize],
     |                          ------^^^^-----------------------------
     |                          |     |
     |                          |     the trait `Clone` is not implemented for `Option<call_error::CallError>`
     |                          required by a bound introduced by this call
     |
     = note: required for `Option<call_error::CallError>` to implement `Clone`

What does work is this, but then again that's not really better than the current code:

            ring_buffer: [const { None }; Self::MAX_ENTRIES as usize]
                .into_iter()
                .collect(),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't you just do .into()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I tried that, but probably made a mistake. That works, thanks! 🙂

next_id: 0,
generation: 0,
}
}
}

impl CallErrors {
const MAX_ENTRIES: u8 = 32;

fn insert(&mut self, err: CallError) -> i32 {
let id = self.next_id;
self.next_id = self.next_id.wrapping_add(1);

self.map.insert(id, err);
id
self.next_id = self.next_id.wrapping_add(1) % Self::MAX_ENTRIES;
if self.next_id == 0 {
self.generation = self.generation.wrapping_add(1);
}

self.ring_buffer[id as usize] = Some(err);

(self.generation as i32) << 16 | id as i32
}

// Returns success or failure.
fn remove(&mut self, id: i32) -> Option<CallError> {
self.map.remove(&id)
let generation = (id >> 16) as u16;
let id = id as u8;

// If id < next_id, the generation must be the current one -- otherwise the one before.
if id < self.next_id {
if generation != self.generation {
return None;
}
} else if generation != self.generation.wrapping_sub(1) {
return None;
}

// Returns Some if there's still an entry, None if it was already removed.
self.ring_buffer[id as usize].take()
}
}

Expand Down Expand Up @@ -348,3 +380,54 @@ where
}
}
}

// ----------------------------------------------------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
use super::{CallError, CallErrors};
use crate::meta::CallContext;

fn make(index: usize) -> CallError {
let method_name = format!("method_{index}");
let ctx = CallContext::func("Class", &method_name);
CallError::failed_by_user_panic(&ctx, "some panic reason".to_string())
}

#[test]
fn test_call_errors() {
let mut store = CallErrors::default();

let mut id07 = 0;
let mut id13 = 0;
let mut id20 = 0;
for i in 0..24 {
let id = store.insert(make(i));
match i {
7 => id07 = id,
13 => id13 = id,
20 => id20 = id,
_ => {}
}
}

let e = store.remove(id20).expect("must be present");
assert_eq!(e.method_name(), "method_20");

let e = store.remove(id20);
assert!(e.is_none());

for i in 24..CallErrors::MAX_ENTRIES as usize {
store.insert(make(i));
}
for i in 0..10 {
store.insert(make(i));
}

let e = store.remove(id07);
assert!(e.is_none(), "generation overwritten");

let e = store.remove(id13).expect("generation not yet overwritten");
assert_eq!(e.method_name(), "method_13");
}
}
Loading