@@ -90,7 +90,7 @@ impl<T, U> Sender<T, U> {
9090 }
9191 let ( tx, rx) = oneshot:: channel ( ) ;
9292 self . inner
93- . send ( Envelope ( Some ( ( val, Callback :: Retry ( tx ) ) ) ) )
93+ . send ( Envelope ( Some ( ( val, Callback :: Retry ( Some ( tx ) ) ) ) ) )
9494 . map ( move |_| rx)
9595 . map_err ( |mut e| ( e. 0 ) . 0 . take ( ) . expect ( "envelope not dropped" ) . 0 )
9696 }
@@ -101,7 +101,7 @@ impl<T, U> Sender<T, U> {
101101 }
102102 let ( tx, rx) = oneshot:: channel ( ) ;
103103 self . inner
104- . send ( Envelope ( Some ( ( val, Callback :: NoRetry ( tx ) ) ) ) )
104+ . send ( Envelope ( Some ( ( val, Callback :: NoRetry ( Some ( tx ) ) ) ) ) )
105105 . map ( move |_| rx)
106106 . map_err ( |mut e| ( e. 0 ) . 0 . take ( ) . expect ( "envelope not dropped" ) . 0 )
107107 }
@@ -131,15 +131,15 @@ impl<T, U> UnboundedSender<T, U> {
131131 pub ( crate ) fn try_send ( & mut self , val : T ) -> Result < RetryPromise < T , U > , T > {
132132 let ( tx, rx) = oneshot:: channel ( ) ;
133133 self . inner
134- . send ( Envelope ( Some ( ( val, Callback :: Retry ( tx ) ) ) ) )
134+ . send ( Envelope ( Some ( ( val, Callback :: Retry ( Some ( tx ) ) ) ) ) )
135135 . map ( move |_| rx)
136136 . map_err ( |mut e| ( e. 0 ) . 0 . take ( ) . expect ( "envelope not dropped" ) . 0 )
137137 }
138138
139139 pub ( crate ) fn send ( & mut self , val : T ) -> Result < Promise < U > , T > {
140140 let ( tx, rx) = oneshot:: channel ( ) ;
141141 self . inner
142- . send ( Envelope ( Some ( ( val, Callback :: NoRetry ( tx ) ) ) ) )
142+ . send ( Envelope ( Some ( ( val, Callback :: NoRetry ( Some ( tx ) ) ) ) ) )
143143 . map ( move |_| rx)
144144 . map_err ( |mut e| ( e. 0 ) . 0 . take ( ) . expect ( "envelope not dropped" ) . 0 )
145145 }
@@ -215,33 +215,59 @@ impl<T, U> Drop for Envelope<T, U> {
215215
216216pub ( crate ) enum Callback < T , U > {
217217 #[ 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+ }
220244}
221245
222246impl < T , U > Callback < T , U > {
223247 #[ cfg( feature = "http2" ) ]
224248 pub ( crate ) fn is_canceled ( & self ) -> bool {
225249 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 ! ( ) ,
228253 }
229254 }
230255
231256 pub ( crate ) fn poll_canceled ( & mut self , cx : & mut task:: Context < ' _ > ) -> Poll < ( ) > {
232257 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 ! ( ) ,
235261 }
236262 }
237263
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 > ) > ) {
239265 match self {
240- Callback :: Retry ( tx) => {
241- let _ = tx. send ( val) ;
266+ Callback :: Retry ( ref mut tx) => {
267+ let _ = tx. take ( ) . unwrap ( ) . send ( val) ;
242268 }
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 ) ) ;
245271 }
246272 }
247273 }
0 commit comments