-
Notifications
You must be signed in to change notification settings - Fork 25
fix: make db thread safe #680
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
Conversation
Manual Deploy AvailableYou can trigger a manual deploy of this PR branch to testnet: Alternative: Comment
Comment updated automatically when the PR is synchronized. |
📝 WalkthroughWalkthroughSchedulerDatabase.conn was changed from a synchronous Possibly related PRs
Suggested reviewers
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro 📒 Files selected for processing (9)
🧰 Additional context used🧠 Learnings (5)📓 Common learnings📚 Learning: 2025-11-07T13:09:52.253ZApplied to files:
📚 Learning: 2025-11-12T09:46:27.553ZApplied to files:
📚 Learning: 2025-11-20T17:25:23.444ZApplied to files:
📚 Learning: 2025-11-04T10:53:50.922ZApplied to files:
🧬 Code graph analysis (6)test-integration/test-task-scheduler/tests/test_schedule_task.rs (2)
test-integration/test-task-scheduler/tests/test_cancel_ongoing_task.rs (1)
test-integration/test-task-scheduler/tests/test_unauthorized_reschedule.rs (1)
test-integration/test-task-scheduler/tests/test_schedule_error.rs (1)
test-integration/test-task-scheduler/tests/test_reschedule_task.rs (1)
magicblock-task-scheduler/src/service.rs (3)
🔇 Additional comments (12)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
bmuddha
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.
We should avoid locking in the code which runs inside of an async context, I understand that it's highly unlikely that this lock might result in runtime starvation, but considering that there are scores of locks employed throughout the codebase, it's nigh impossible to analyze the interplay of lock acquisition/release flow. In the future, we should replace all the Lock uses with lock free alternatives.
For now I'd suggest to synchronize via non-blocking primitives like semaphores, in which case unsafe impl is justified always even if connection is used across multiple threads.
struct SchedulerDatabase {
connection: Connection,
semaphore: Arc<Semaphore>,
}
impl SchedulerDatabase {
fn new() -> Self {
// ...
let semaphore = Arc::new(Semaphore::new(1));
let connection = Connection;
Self {
connection,
semaphore,
}
}
async fn connection(&self) -> ConnectionGuard<'_> {
let permit = self
.semaphore
.acquire()
.await
.expect("semaphore cannot be closed");
ConnectionGuard {
permit,
connection: &self.connection,
}
}
async fn do_something(&self) {
let con = self.connection();
// Do some CRUD with connection
// ...
// con goes out of scope, releasing semaphore
// permit and the next request can proceed
}
}
struct ConnectionGuard<'db> {
permit: SemaphorePermit<'db>,
connection: &'db Connection,
}
impl Deref for ConnectionGuard<'_> {
type Target = Connection;
fn deref(&self) -> &Self::Target {
&self.connection
}
}|
Addressed in 5d10cf8 |
bmuddha
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.
LGTM
GabrielePicco
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.
LGTM!
* master: fix: don't unbork for same delegation slot as remote slot (#702) feat: use simplified configuration management (#685) Report Intent's patched errors (#667) fix: replace cargo-expand with syn-based verification (#611) fix: make db thread safe (#680) fix: reset program cache upon error (#696) chore: use smaller runners (#695) feat: cranked commits (#656) fix: refresh stuck undelegating accounts that are closed on-chain (#691) Fix: abort truncation if validator exited (#689)
TaskSchedulerServicehasunsafe impl Sync for TaskSchedulerService {}despiteSchedulerDatabasebeing a wrapper aroundrusqlite::Connection, which is not thread-safe. This is not yet a critical issue because it is only used by a single thread but could become a problem.This solves it by wrapping the database with a
RwLockSummary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.