Skip to content

Commit 64e1ef0

Browse files
Copilotstephentoubrzikm
authored
Fix SmtpClientTest.SendAsync_CanBeCanceled_SendAsyncCancel test failure (#116042)
* Initial plan for issue * Fix SmtpClientTest.SendAsync_CanBeCanceled_SendAsyncCancel test by using IsAssignableFrom Co-authored-by: stephentoub <[email protected]> * Fix ThrowsAsync to ThrowsAnyAsync for TaskCanceledException check Co-authored-by: stephentoub <[email protected]> * Fix ThrowsAnyAsync<TaskCanceledException> to handle both direct and wrapped exceptions Co-authored-by: stephentoub <[email protected]> * Fix assertion and implementation for SmtpClient cancellation Co-authored-by: rzikm <[email protected]> * Move Abort() call and fix nullability annotations for ProcessException Co-authored-by: rzikm <[email protected]> * Fix cancellation handling for SmtpClient: prevent wrapping OperationCanceledException and fix test race condition Co-authored-by: rzikm <[email protected]> * Remove unused serverMre variable and wrap SendMailAsync in Task.Run Co-authored-by: rzikm <[email protected]> * Apply suggestions from code review --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: stephentoub <[email protected]> Co-authored-by: rzikm <[email protected]> Co-authored-by: Stephen Toub <[email protected]>
1 parent afa0f6b commit 64e1ef0

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,9 @@ Exception ProcessException(Exception e, ref bool canceled, bool forceWrapExcepti
556556

557557
if (!forceWrapExceptions ||
558558
// for compatibility reasons, don't wrap these exceptions during sync executions
559-
(typeof(TIOAdapter) == typeof(SyncReadWriteAdapter) &&
560-
(e is SecurityException || e is AuthenticationException)) ||
561-
e is SmtpException)
559+
(typeof(TIOAdapter) == typeof(SyncReadWriteAdapter) && (e is SecurityException or AuthenticationException)) ||
560+
e is SmtpException ||
561+
e is OperationCanceledException)
562562
{
563563
return e;
564564
}
@@ -574,7 +574,7 @@ Exception ProcessException(Exception e, ref bool canceled, bool forceWrapExcepti
574574
// SendCompleted event should ever be invoked only for asynchronous send completions.
575575
if (invokeSendCompleted && !synchronous)
576576
{
577-
AsyncCompletedEventArgs eventArgs = new AsyncCompletedEventArgs(exception, canceled, userToken);
577+
AsyncCompletedEventArgs eventArgs = new(canceled ? null : exception, canceled, userToken);
578578
OnSendCompleted(eventArgs);
579579
}
580580
}

src/libraries/System.Net.Mail/tests/Functional/SmtpClientTest.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,20 +314,15 @@ public async Task SendMailAsync_CanBeCanceled_CancellationToken()
314314
server.ReceiveMultipleConnections = true;
315315

316316
// The server will introduce some fake latency so that the operation can be canceled before the request completes
317-
ManualResetEvent serverMre = new ManualResetEvent(false);
318-
server.OnConnected += _ => serverMre.WaitOne();
319-
320317
CancellationTokenSource cts = new CancellationTokenSource();
318+
319+
server.OnConnected += _ => cts.Cancel();
321320

322321
var message = new MailMessage("[email protected]", "[email protected]", "Foo", "Bar");
323322

324323
Task sendTask = Task.Run(() => client.SendMailAsync(message, cts.Token));
325324

326-
cts.Cancel();
327-
await Task.Delay(500);
328-
serverMre.Set();
329-
330-
await Assert.ThrowsAsync<TaskCanceledException>(async () => await sendTask).WaitAsync(TestHelper.PassingTestTimeout);
325+
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await sendTask).WaitAsync(TestHelper.PassingTestTimeout);
331326

332327
// We should still be able to send mail on the SmtpClient instance
333328
await Task.Run(() => client.SendMailAsync(message)).WaitAsync(TestHelper.PassingTestTimeout);
@@ -369,8 +364,7 @@ public async Task SendAsync_CanBeCanceled_SendAsyncCancel()
369364
client.SendAsync(message, null);
370365
AsyncCompletedEventArgs e = await tcs.Task.WaitAsync(TestHelper.PassingTestTimeout);
371366
Assert.True(e.Cancelled, "SendAsync should have been canceled");
372-
_output.WriteLine(e.Error?.ToString() ?? "No error");
373-
Assert.IsType<OperationCanceledException>(e.Error.InnerException);
367+
Assert.Null(e.Error);
374368

375369
// We should still be able to send mail on the SmtpClient instance
376370
await client.SendMailAsync(message).WaitAsync(TestHelper.PassingTestTimeout);

0 commit comments

Comments
 (0)