@@ -10,6 +10,7 @@ use futures_lite::FutureExt;
10
10
use rand:: Rng ;
11
11
use tokio:: sync:: { oneshot, RwLock , RwLockWriteGuard } ;
12
12
use tokio:: task;
13
+ use tokio_util:: sync:: CancellationToken ;
13
14
14
15
use self :: connectivity:: ConnectivityStore ;
15
16
use crate :: config:: { self , Config } ;
@@ -389,7 +390,7 @@ async fn inbox_loop(
389
390
info ! ( ctx, "Starting inbox loop." ) ;
390
391
let ImapConnectionHandlers {
391
392
mut connection,
392
- stop_receiver ,
393
+ stop_token ,
393
394
} = inbox_handlers;
394
395
395
396
let ctx1 = ctx. clone ( ) ;
@@ -428,8 +429,8 @@ async fn inbox_loop(
428
429
}
429
430
} ;
430
431
431
- stop_receiver
432
- . recv ( )
432
+ stop_token
433
+ . cancelled ( )
433
434
. map ( |_| {
434
435
info ! ( ctx, "Shutting down inbox loop." ) ;
435
436
} )
@@ -703,7 +704,7 @@ async fn simple_imap_loop(
703
704
info ! ( ctx, "Starting simple loop for {folder_meaning}." ) ;
704
705
let ImapConnectionHandlers {
705
706
mut connection,
706
- stop_receiver ,
707
+ stop_token ,
707
708
} = inbox_handlers;
708
709
709
710
let ctx1 = ctx. clone ( ) ;
@@ -749,8 +750,8 @@ async fn simple_imap_loop(
749
750
}
750
751
} ;
751
752
752
- stop_receiver
753
- . recv ( )
753
+ stop_token
754
+ . cancelled ( )
754
755
. map ( |_| {
755
756
info ! ( ctx, "Shutting down IMAP loop for {folder_meaning}." ) ;
756
757
} )
@@ -768,7 +769,7 @@ async fn smtp_loop(
768
769
info ! ( ctx, "Starting SMTP loop." ) ;
769
770
let SmtpConnectionHandlers {
770
771
mut connection,
771
- stop_receiver ,
772
+ stop_token ,
772
773
idle_interrupt_receiver,
773
774
} = smtp_handlers;
774
775
@@ -836,8 +837,8 @@ async fn smtp_loop(
836
837
}
837
838
} ;
838
839
839
- stop_receiver
840
- . recv ( )
840
+ stop_token
841
+ . cancelled ( )
841
842
. map ( |_| {
842
843
info ! ( ctx, "Shutting down SMTP loop." ) ;
843
844
} )
@@ -982,9 +983,9 @@ impl Scheduler {
982
983
pub ( crate ) async fn stop ( self , context : & Context ) {
983
984
// Send stop signals to tasks so they can shutdown cleanly.
984
985
for b in self . boxes ( ) {
985
- b. conn_state . stop ( ) . await . log_err ( context ) . ok ( ) ;
986
+ b. conn_state . stop ( ) ;
986
987
}
987
- self . smtp . stop ( ) . await . log_err ( context ) . ok ( ) ;
988
+ self . smtp . stop ( ) ;
988
989
989
990
// Actually shutdown tasks.
990
991
let timeout_duration = std:: time:: Duration :: from_secs ( 30 ) ;
@@ -1014,8 +1015,8 @@ impl Scheduler {
1014
1015
/// Connection state logic shared between imap and smtp connections.
1015
1016
#[ derive( Debug ) ]
1016
1017
struct ConnectionState {
1017
- /// Channel to interrupt the whole connection.
1018
- stop_sender : Sender < ( ) > ,
1018
+ /// Cancellation token to interrupt the whole connection.
1019
+ stop_token : CancellationToken ,
1019
1020
/// Channel to interrupt idle.
1020
1021
idle_interrupt_sender : Sender < ( ) > ,
1021
1022
/// Mutex to pass connectivity info between IMAP/SMTP threads and the API
@@ -1024,13 +1025,9 @@ struct ConnectionState {
1024
1025
1025
1026
impl ConnectionState {
1026
1027
/// Shutdown this connection completely.
1027
- async fn stop ( & self ) -> Result < ( ) > {
1028
+ fn stop ( & self ) {
1028
1029
// Trigger shutdown of the run loop.
1029
- self . stop_sender
1030
- . send ( ( ) )
1031
- . await
1032
- . context ( "failed to stop, missing receiver" ) ?;
1033
- Ok ( ( ) )
1030
+ self . stop_token . cancel ( ) ;
1034
1031
}
1035
1032
1036
1033
fn interrupt ( & self ) {
@@ -1046,17 +1043,17 @@ pub(crate) struct SmtpConnectionState {
1046
1043
1047
1044
impl SmtpConnectionState {
1048
1045
fn new ( ) -> ( Self , SmtpConnectionHandlers ) {
1049
- let ( stop_sender , stop_receiver ) = channel :: bounded ( 1 ) ;
1046
+ let stop_token = CancellationToken :: new ( ) ;
1050
1047
let ( idle_interrupt_sender, idle_interrupt_receiver) = channel:: bounded ( 1 ) ;
1051
1048
1052
1049
let handlers = SmtpConnectionHandlers {
1053
1050
connection : Smtp :: new ( ) ,
1054
- stop_receiver ,
1051
+ stop_token : stop_token . clone ( ) ,
1055
1052
idle_interrupt_receiver,
1056
1053
} ;
1057
1054
1058
1055
let state = ConnectionState {
1059
- stop_sender ,
1056
+ stop_token ,
1060
1057
idle_interrupt_sender,
1061
1058
connectivity : handlers. connection . connectivity . clone ( ) ,
1062
1059
} ;
@@ -1072,15 +1069,14 @@ impl SmtpConnectionState {
1072
1069
}
1073
1070
1074
1071
/// Shutdown this connection completely.
1075
- async fn stop ( & self ) -> Result < ( ) > {
1076
- self . state . stop ( ) . await ?;
1077
- Ok ( ( ) )
1072
+ fn stop ( & self ) {
1073
+ self . state . stop ( ) ;
1078
1074
}
1079
1075
}
1080
1076
1081
1077
struct SmtpConnectionHandlers {
1082
1078
connection : Smtp ,
1083
- stop_receiver : Receiver < ( ) > ,
1079
+ stop_token : CancellationToken ,
1084
1080
idle_interrupt_receiver : Receiver < ( ) > ,
1085
1081
}
1086
1082
@@ -1092,16 +1088,16 @@ pub(crate) struct ImapConnectionState {
1092
1088
impl ImapConnectionState {
1093
1089
/// Construct a new connection.
1094
1090
async fn new ( context : & Context ) -> Result < ( Self , ImapConnectionHandlers ) > {
1095
- let ( stop_sender , stop_receiver ) = channel :: bounded ( 1 ) ;
1091
+ let stop_token = CancellationToken :: new ( ) ;
1096
1092
let ( idle_interrupt_sender, idle_interrupt_receiver) = channel:: bounded ( 1 ) ;
1097
1093
1098
1094
let handlers = ImapConnectionHandlers {
1099
1095
connection : Imap :: new_configured ( context, idle_interrupt_receiver) . await ?,
1100
- stop_receiver ,
1096
+ stop_token : stop_token . clone ( ) ,
1101
1097
} ;
1102
1098
1103
1099
let state = ConnectionState {
1104
- stop_sender ,
1100
+ stop_token ,
1105
1101
idle_interrupt_sender,
1106
1102
connectivity : handlers. connection . connectivity . clone ( ) ,
1107
1103
} ;
@@ -1117,14 +1113,13 @@ impl ImapConnectionState {
1117
1113
}
1118
1114
1119
1115
/// Shutdown this connection completely.
1120
- async fn stop ( & self ) -> Result < ( ) > {
1121
- self . state . stop ( ) . await ?;
1122
- Ok ( ( ) )
1116
+ fn stop ( & self ) {
1117
+ self . state . stop ( ) ;
1123
1118
}
1124
1119
}
1125
1120
1126
1121
#[ derive( Debug ) ]
1127
1122
struct ImapConnectionHandlers {
1128
1123
connection : Imap ,
1129
- stop_receiver : Receiver < ( ) > ,
1124
+ stop_token : CancellationToken ,
1130
1125
}
0 commit comments