-
Notifications
You must be signed in to change notification settings - Fork 10
Add Send + Sync bounds to resolve Arc clippy warnings #883
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
base: master
Are you sure you want to change the base?
Add Send + Sync bounds to resolve Arc clippy warnings #883
Conversation
- Add Send + Sync bounds to LogProvider trait - Add Send + Sync bounds to ExpiryPolicy trait - Add Send + Sync bounds to action traits: MetaAction, StaticAction, StatefulAction, HairpinAction, ActionDesc - Add Send + Sync bounds to SNAT implementation trait bounds - Add unsafe Send + Sync implementations for KStatNamed Fixes oxidecomputer#785: Port<N> should require Send + Sync bounds This minimal implementation only adds bounds where compilation errors occurred, ensuring Arc<Port<VpcNetwork>> and Arc<ExecCtx> are Send + Sync.
FelixMcFelix
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this, and sorry for the delay in getting back to you. Send/Sync reasoning need close thought, hopefully these comments are helpful.
| // 5. No shared mutable state: The raw pointer represents a kernel resource with | ||
| // a clear lifecycle (create -> install -> delete) with no Rust-side mutation. | ||
| #[cfg(all(not(feature = "std"), not(test)))] | ||
| unsafe impl<T: KStatProvider> Send for KStatNamed<T> {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that, currently, this bound is far too broad (i.e., if T is not Send or Sync, neither should KStatNamed be). I believe the correct narrowing might be that we impose, higher in this file, that pub trait KStatProvider: Send + Sync, since the data associated with each kstat will need to be accessed in at least one other kernel thread if there's a kstat invocation.
| // SAFETY: KStatNamed<T> is safe for Send + Sync under the following conditions: | ||
| // | ||
| // 1. Read-only sharing: After creation, the `ksp` pointer is never mutated - only | ||
| // passed to kstat_delete() in Drop. Multiple threads can safely read the same | ||
| // immutable pointer value. | ||
| // | ||
| // 2. Kernel manages concurrency: The kstat framework handles concurrent access to | ||
| // the underlying kernel structures via its own internal mechanisms. | ||
| // | ||
| // 3. Atomic statistics: Individual stats in `vals` use AtomicU64, ensuring each | ||
| // counter update is atomic and thread-safe. | ||
| // | ||
| // 4. Intentional design trade-off: We explicitly do NOT set ks_lock (see struct | ||
| // comment), accepting that different threads may see inconsistent snapshots | ||
| // of the stats as a group, while individual values remain uncorrupted. | ||
| // | ||
| // 5. No shared mutable state: The raw pointer represents a kernel resource with | ||
| // a clear lifecycle (create -> install -> delete) with no Rust-side mutation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be more concisely summarised, after applying the other suggested change, as a statement that:
- The underlying
Tmust itself beSend + Syncbecause it is fully visible to any user of thestruct. We meet this through the new type bound onKStatProvider. kspis itself safe to move between threads, sinceKSTAT(9S)imposes no MT constraints on callers.kspis never exposed via a&ref (nor is it used by any methods taking&self), and is only used duringdropas you point out above.
Fixes #785: Port should require Send + Sync bounds
This minimal implementation only adds bounds where compilation errors occurred, ensuring Arc<Port> and Arc are Send + Sync.
Before
After
Disclosure
I did use claud-code to assist with this.
Added this function
To identify where Send, Sync needed to be added. Then asked a lot of questions and poked around the code trying to determine safety of