@@ -254,34 +254,28 @@ bool Upgrade(IChannelHandlerContext ctx, IFullHttpRequest request)
254254 var upgradeEvent = new UpgradeEvent ( upgradeProtocol , request ) ;
255255
256256 IUpgradeCodec finalUpgradeCodec = upgradeCodec ;
257- ctx . WriteAndFlushAsync ( upgradeResponse ) . ContinueWith ( t =>
258- {
259- try
260- {
261- if ( t . Status == TaskStatus . RanToCompletion )
262- {
263- // Perform the upgrade to the new protocol.
264- this . sourceCodec . UpgradeFrom ( ctx ) ;
265- finalUpgradeCodec . UpgradeTo ( ctx , request ) ;
266-
267- // Notify that the upgrade has occurred. Retain the event to offset
268- // the release() in the finally block.
269- ctx . FireUserEventTriggered ( upgradeEvent . Retain ( ) ) ;
270-
271- // Remove this handler from the pipeline.
272- ctx . Channel . Pipeline . Remove ( this ) ;
273- }
274- else
275- {
276- ctx . Channel . CloseAsync ( ) ;
277- }
278- }
279- finally
280- {
281- // Release the event if the upgrade event wasn't fired.
282- upgradeEvent . Release ( ) ;
283- }
284- } , TaskContinuationOptions . ExecuteSynchronously ) ;
257+ try
258+ {
259+ Task writeTask = ctx . WriteAndFlushAsync ( upgradeResponse ) ;
260+
261+ // Perform the upgrade to the new protocol.
262+ this . sourceCodec . UpgradeFrom ( ctx ) ;
263+ finalUpgradeCodec . UpgradeTo ( ctx , request ) ;
264+
265+ // Remove this handler from the pipeline.
266+ ctx . Channel . Pipeline . Remove ( this ) ;
267+
268+ // Notify that the upgrade has occurred. Retain the event to offset
269+ // the release() in the finally block.
270+ ctx . FireUserEventTriggered ( upgradeEvent . Retain ( ) ) ;
271+
272+ writeTask . CloseOnFailure ( ctx . Channel ) ;
273+ }
274+ finally
275+ {
276+ // Release the event if the upgrade event wasn't fired.
277+ upgradeEvent . Release ( ) ;
278+ }
285279 return true ;
286280 }
287281
0 commit comments