@@ -90,7 +90,7 @@ impl<T, U> Sender<T, U> {
90
90
}
91
91
let ( tx, rx) = oneshot:: channel ( ) ;
92
92
self . inner
93
- . send ( Envelope ( Some ( ( val, Callback :: Retry ( tx ) ) ) ) )
93
+ . send ( Envelope ( Some ( ( val, Callback :: Retry ( Some ( tx ) ) ) ) ) )
94
94
. map ( move |_| rx)
95
95
. map_err ( |mut e| ( e. 0 ) . 0 . take ( ) . expect ( "envelope not dropped" ) . 0 )
96
96
}
@@ -101,7 +101,7 @@ impl<T, U> Sender<T, U> {
101
101
}
102
102
let ( tx, rx) = oneshot:: channel ( ) ;
103
103
self . inner
104
- . send ( Envelope ( Some ( ( val, Callback :: NoRetry ( tx ) ) ) ) )
104
+ . send ( Envelope ( Some ( ( val, Callback :: NoRetry ( Some ( tx ) ) ) ) ) )
105
105
. map ( move |_| rx)
106
106
. map_err ( |mut e| ( e. 0 ) . 0 . take ( ) . expect ( "envelope not dropped" ) . 0 )
107
107
}
@@ -131,15 +131,15 @@ impl<T, U> UnboundedSender<T, U> {
131
131
pub ( crate ) fn try_send ( & mut self , val : T ) -> Result < RetryPromise < T , U > , T > {
132
132
let ( tx, rx) = oneshot:: channel ( ) ;
133
133
self . inner
134
- . send ( Envelope ( Some ( ( val, Callback :: Retry ( tx ) ) ) ) )
134
+ . send ( Envelope ( Some ( ( val, Callback :: Retry ( Some ( tx ) ) ) ) ) )
135
135
. map ( move |_| rx)
136
136
. map_err ( |mut e| ( e. 0 ) . 0 . take ( ) . expect ( "envelope not dropped" ) . 0 )
137
137
}
138
138
139
139
pub ( crate ) fn send ( & mut self , val : T ) -> Result < Promise < U > , T > {
140
140
let ( tx, rx) = oneshot:: channel ( ) ;
141
141
self . inner
142
- . send ( Envelope ( Some ( ( val, Callback :: NoRetry ( tx ) ) ) ) )
142
+ . send ( Envelope ( Some ( ( val, Callback :: NoRetry ( Some ( tx ) ) ) ) ) )
143
143
. map ( move |_| rx)
144
144
. map_err ( |mut e| ( e. 0 ) . 0 . take ( ) . expect ( "envelope not dropped" ) . 0 )
145
145
}
@@ -215,33 +215,59 @@ impl<T, U> Drop for Envelope<T, U> {
215
215
216
216
pub ( crate ) enum Callback < T , U > {
217
217
#[ allow( unused) ]
218
- Retry ( oneshot:: Sender < Result < U , ( crate :: Error , Option < T > ) > > ) ,
219
- NoRetry ( oneshot:: Sender < Result < U , crate :: Error > > ) ,
218
+ Retry ( Option < oneshot:: Sender < Result < U , ( crate :: Error , Option < T > ) > > > ) ,
219
+ NoRetry ( Option < oneshot:: Sender < Result < U , crate :: Error > > > ) ,
220
+ }
221
+
222
+ impl < T , U > Drop for Callback < T , U > {
223
+ fn drop ( & mut self ) {
224
+ // FIXME(nox): What errors do we want here?
225
+ let error = crate :: Error :: new_user_dispatch_gone ( ) . with ( if std:: thread:: panicking ( ) {
226
+ "user code panicked"
227
+ } else {
228
+ "runtime dropped the dispatch task"
229
+ } ) ;
230
+
231
+ match self {
232
+ Callback :: Retry ( tx) => {
233
+ if let Some ( tx) = tx. take ( ) {
234
+ let _ = tx. send ( Err ( ( error, None ) ) ) ;
235
+ }
236
+ }
237
+ Callback :: NoRetry ( tx) => {
238
+ if let Some ( tx) = tx. take ( ) {
239
+ let _ = tx. send ( Err ( error) ) ;
240
+ }
241
+ }
242
+ }
243
+ }
220
244
}
221
245
222
246
impl < T , U > Callback < T , U > {
223
247
#[ cfg( feature = "http2" ) ]
224
248
pub ( crate ) fn is_canceled ( & self ) -> bool {
225
249
match * self {
226
- Callback :: Retry ( ref tx) => tx. is_closed ( ) ,
227
- Callback :: NoRetry ( ref tx) => tx. is_closed ( ) ,
250
+ Callback :: Retry ( Some ( ref tx) ) => tx. is_closed ( ) ,
251
+ Callback :: NoRetry ( Some ( ref tx) ) => tx. is_closed ( ) ,
252
+ _ => unreachable ! ( ) ,
228
253
}
229
254
}
230
255
231
256
pub ( crate ) fn poll_canceled ( & mut self , cx : & mut task:: Context < ' _ > ) -> Poll < ( ) > {
232
257
match * self {
233
- Callback :: Retry ( ref mut tx) => tx. poll_closed ( cx) ,
234
- Callback :: NoRetry ( ref mut tx) => tx. poll_closed ( cx) ,
258
+ Callback :: Retry ( Some ( ref mut tx) ) => tx. poll_closed ( cx) ,
259
+ Callback :: NoRetry ( Some ( ref mut tx) ) => tx. poll_closed ( cx) ,
260
+ _ => unreachable ! ( ) ,
235
261
}
236
262
}
237
263
238
- pub ( crate ) fn send ( self , val : Result < U , ( crate :: Error , Option < T > ) > ) {
264
+ pub ( crate ) fn send ( mut self , val : Result < U , ( crate :: Error , Option < T > ) > ) {
239
265
match self {
240
- Callback :: Retry ( tx) => {
241
- let _ = tx. send ( val) ;
266
+ Callback :: Retry ( ref mut tx) => {
267
+ let _ = tx. take ( ) . unwrap ( ) . send ( val) ;
242
268
}
243
- Callback :: NoRetry ( tx) => {
244
- let _ = tx. send ( val. map_err ( |e| e. 0 ) ) ;
269
+ Callback :: NoRetry ( ref mut tx) => {
270
+ let _ = tx. take ( ) . unwrap ( ) . send ( val. map_err ( |e| e. 0 ) ) ;
245
271
}
246
272
}
247
273
}
0 commit comments