avoid race condition on scheduler start

This commit is contained in:
dignifiedquire
2020-03-18 16:34:18 +01:00
parent 9d313b4e0e
commit 9981e84a42
2 changed files with 11 additions and 12 deletions

View File

@@ -132,8 +132,8 @@ impl Context {
panic!("Already running"); panic!("Already running");
} }
let scheduler = Scheduler::run(self.clone()); let l = &mut *self.inner.scheduler.write().await;
*self.inner.scheduler.write().await = scheduler; l.run(self.clone());
} }
pub async fn stop(&self) { pub async fn stop(&self) {

View File

@@ -186,12 +186,20 @@ async fn smtp_loop(ctx: Context, smtp_handlers: SmtpConnectionHandlers) {
impl Scheduler { impl Scheduler {
/// Start the scheduler, panics if it is already running. /// Start the scheduler, panics if it is already running.
pub fn run(ctx: Context) -> Self { pub fn run(&mut self, ctx: Context) {
let (mvbox, mvbox_handlers) = ImapConnectionState::new(); let (mvbox, mvbox_handlers) = ImapConnectionState::new();
let (sentbox, sentbox_handlers) = ImapConnectionState::new(); let (sentbox, sentbox_handlers) = ImapConnectionState::new();
let (smtp, smtp_handlers) = SmtpConnectionState::new(); let (smtp, smtp_handlers) = SmtpConnectionState::new();
let (inbox, inbox_handlers) = ImapConnectionState::new(); let (inbox, inbox_handlers) = ImapConnectionState::new();
*self = Scheduler::Running {
inbox,
mvbox,
sentbox,
smtp,
probe_network: false,
};
let ctx1 = ctx.clone(); let ctx1 = ctx.clone();
task::spawn(async move { inbox_loop(ctx1, inbox_handlers).await }); task::spawn(async move { inbox_loop(ctx1, inbox_handlers).await });
@@ -208,17 +216,8 @@ impl Scheduler {
let ctx1 = ctx.clone(); let ctx1 = ctx.clone();
task::spawn(async move { smtp_loop(ctx1, smtp_handlers).await }); task::spawn(async move { smtp_loop(ctx1, smtp_handlers).await });
let res = Scheduler::Running {
inbox,
mvbox,
sentbox,
smtp,
probe_network: false,
};
info!(ctx, "scheduler is running"); info!(ctx, "scheduler is running");
println!("RUN DONE"); println!("RUN DONE");
res
} }
fn set_probe_network(&mut self, val: bool) { fn set_probe_network(&mut self, val: bool) {