@@ -254,25 +254,17 @@ private async Task<bool> CreateTLSConnectionAsync(Stream stream)
254
254
}
255
255
}
256
256
257
- internal bool CloseSocket ( bool ? shutdownPipeline = true )
257
+ internal async Task < bool > CloseSocketAsync ( bool ? shutdownPipeline = true )
258
258
{
259
- // Cancel the background traffic processing tasks
260
- this . cancellationTokenSource . Cancel ( ) ;
261
-
262
- // Reset the tasks
263
- this . ConnectionPublishWriterTask = null ;
264
- this . ConnectionWriterTask = null ;
265
- this . ConnectionReaderTask = null ;
266
- this . ReceivedPacketsHandlerTask = null ;
267
- this . ConnectionMonitorTask = null ;
259
+ await this . CancelBackgroundTasksAsync ( ) . ConfigureAwait ( false ) ;
268
260
269
261
if ( shutdownPipeline == true )
270
262
{
271
263
if ( this . Reader != null && this . Writer != null )
272
264
{
273
265
// Dispose of the PipeReader and PipeWriter
274
- this . Reader . Complete ( ) ;
275
- this . Writer . Complete ( ) ;
266
+ await this . Reader . CompleteAsync ( ) . ConfigureAwait ( false ) ;
267
+ await this . Writer . CompleteAsync ( ) . ConfigureAwait ( false ) ;
276
268
277
269
// Shutdown the pipeline
278
270
this . Reader = null ;
@@ -284,7 +276,7 @@ internal bool CloseSocket(bool? shutdownPipeline = true)
284
276
{
285
277
// Dispose of the Stream
286
278
this . Stream . Close ( ) ;
287
- this . Stream . Dispose ( ) ;
279
+ await this . Stream . DisposeAsync ( ) . ConfigureAwait ( false ) ;
288
280
this . Stream = null ;
289
281
}
290
282
@@ -300,4 +292,64 @@ internal bool CloseSocket(bool? shutdownPipeline = true)
300
292
301
293
return true ;
302
294
}
295
+
296
+ /// <summary>
297
+ /// Cancel all background tasks.
298
+ /// </summary>
299
+ /// <returns>A task representing the asynchronous operation.</returns>
300
+ internal async Task CancelBackgroundTasksAsync ( )
301
+ {
302
+ // Don't use CancelAsync here to maintain backwards compatibility
303
+ // with >=.net6.0. CancelAsync was introduced in .net8.0
304
+ this . cancellationTokenSource . Cancel ( ) ;
305
+
306
+ // Delay for a short period to allow the tasks to cancel
307
+ await Task . Delay ( 1000 ) . ConfigureAwait ( false ) ;
308
+
309
+ // Reset the tasks
310
+ if ( this . ConnectionPublishWriterTask is not null && this . ConnectionPublishWriterTask . IsCompleted )
311
+ {
312
+ this . ConnectionPublishWriterTask = null ;
313
+ }
314
+ else
315
+ {
316
+ Logger . Error ( "ConnectionPublishWriterTask did not complete" ) ;
317
+ }
318
+
319
+ if ( this . ConnectionWriterTask is not null && this . ConnectionWriterTask . IsCompleted )
320
+ {
321
+ this . ConnectionWriterTask = null ;
322
+ }
323
+ else
324
+ {
325
+ Logger . Error ( "ConnectionWriterTask did not complete" ) ;
326
+ }
327
+
328
+ if ( this . ConnectionReaderTask is not null && this . ConnectionReaderTask . IsCompleted )
329
+ {
330
+ this . ConnectionReaderTask = null ;
331
+ }
332
+ else
333
+ {
334
+ Logger . Error ( "ConnectionReaderTask did not complete" ) ;
335
+ }
336
+
337
+ if ( this . ReceivedPacketsHandlerTask is not null && this . ReceivedPacketsHandlerTask . IsCompleted )
338
+ {
339
+ this . ReceivedPacketsHandlerTask = null ;
340
+ }
341
+ else
342
+ {
343
+ Logger . Error ( "ReceivedPacketsHandlerTask did not complete" ) ;
344
+ }
345
+
346
+ if ( this . ConnectionMonitorTask is not null && this . ConnectionMonitorTask . IsCompleted )
347
+ {
348
+ this . ConnectionMonitorTask = null ;
349
+ }
350
+ else
351
+ {
352
+ Logger . Error ( "ConnectionMonitorTask did not complete" ) ;
353
+ }
354
+ }
303
355
}
0 commit comments