@@ -215,64 +215,90 @@ private HttpConnectionContext CreateDerivedContext(IDuplexPipe transport)
215215
216216 private void StopProcessingNextRequest ( )
217217 {
218+ ProtocolSelectionState previousState ;
218219 lock ( _protocolSelectionLock )
219220 {
221+ previousState = _protocolSelectionState ;
222+
220223 switch ( _protocolSelectionState )
221224 {
222225 case ProtocolSelectionState . Initializing :
223- CloseUninitializedConnection ( new ConnectionAbortedException ( CoreStrings . ServerShutdownDuringConnectionInitialization ) ) ;
224226 _protocolSelectionState = ProtocolSelectionState . Aborted ;
225227 break ;
226228 case ProtocolSelectionState . Selected :
227- _requestProcessor . StopProcessingNextRequest ( ) ;
228- break ;
229229 case ProtocolSelectionState . Aborted :
230230 break ;
231231 }
232232 }
233+
234+ switch ( previousState )
235+ {
236+ case ProtocolSelectionState . Initializing :
237+ CloseUninitializedConnection ( new ConnectionAbortedException ( CoreStrings . ServerShutdownDuringConnectionInitialization ) ) ;
238+ break ;
239+ case ProtocolSelectionState . Selected :
240+ _requestProcessor . StopProcessingNextRequest ( ) ;
241+ break ;
242+ case ProtocolSelectionState . Aborted :
243+ break ;
244+ }
233245 }
234246
235247 private void OnInputOrOutputCompleted ( )
236248 {
249+ ProtocolSelectionState previousState ;
237250 lock ( _protocolSelectionLock )
238251 {
252+ previousState = _protocolSelectionState ;
253+
239254 switch ( _protocolSelectionState )
240255 {
241256 case ProtocolSelectionState . Initializing :
242- // OnReader/WriterCompleted callbacks are not wired until after leaving the Initializing state.
243- Debug . Assert ( false ) ;
244-
245- CloseUninitializedConnection ( new ConnectionAbortedException ( "HttpConnection.OnInputOrOutputCompleted() called while in the ProtocolSelectionState.Initializing state!?" ) ) ;
246257 _protocolSelectionState = ProtocolSelectionState . Aborted ;
247258 break ;
248259 case ProtocolSelectionState . Selected :
249- _requestProcessor . OnInputOrOutputCompleted ( ) ;
250- break ;
251260 case ProtocolSelectionState . Aborted :
252261 break ;
253262 }
263+ }
264+
265+ switch ( previousState )
266+ {
267+ case ProtocolSelectionState . Initializing :
268+ // ConnectionClosed callback is not wired up until after leaving the Initializing state.
269+ Debug . Assert ( false ) ;
254270
271+ CloseUninitializedConnection ( new ConnectionAbortedException ( "HttpConnection.OnInputOrOutputCompleted() called while in the ProtocolSelectionState.Initializing state!?" ) ) ;
272+ break ;
273+ case ProtocolSelectionState . Selected :
274+ _requestProcessor . OnInputOrOutputCompleted ( ) ;
275+ break ;
276+ case ProtocolSelectionState . Aborted :
277+ break ;
255278 }
256279 }
257280
258281 private void Abort ( ConnectionAbortedException ex )
259282 {
283+ ProtocolSelectionState previousState ;
284+
260285 lock ( _protocolSelectionLock )
261286 {
262- switch ( _protocolSelectionState )
263- {
264- case ProtocolSelectionState . Initializing :
265- CloseUninitializedConnection ( ex ) ;
266- break ;
267- case ProtocolSelectionState . Selected :
268- _requestProcessor . Abort ( ex ) ;
269- break ;
270- case ProtocolSelectionState . Aborted :
271- break ;
272- }
273-
287+ previousState = _protocolSelectionState ;
274288 _protocolSelectionState = ProtocolSelectionState . Aborted ;
275289 }
290+
291+ switch ( previousState )
292+ {
293+ case ProtocolSelectionState . Initializing :
294+ CloseUninitializedConnection ( ex ) ;
295+ break ;
296+ case ProtocolSelectionState . Selected :
297+ _requestProcessor . Abort ( ex ) ;
298+ break ;
299+ case ProtocolSelectionState . Aborted :
300+ break ;
301+ }
276302 }
277303
278304 private async Task < Stream > ApplyConnectionAdaptersAsync ( RawStream stream )
@@ -365,6 +391,12 @@ private void Tick()
365391 private void CloseUninitializedConnection ( ConnectionAbortedException abortReason )
366392 {
367393 _context . ConnectionContext . Abort ( abortReason ) ;
394+
395+ if ( _context . ConnectionAdapters . Count > 0 )
396+ {
397+ _adaptedTransport . Input . Complete ( ) ;
398+ _adaptedTransport . Output . Complete ( ) ;
399+ }
368400 }
369401
370402 public void OnTimeout ( TimeoutReason reason )
0 commit comments