Align HttpListener.GetContext() exception across platforms for Stop/Abort/Close#123360
Align HttpListener.GetContext() exception across platforms for Stop/Abort/Close#123360
Conversation
…r Stop() Change the error code from HttpStatusCode.InternalServerError (500) to SocketError.OperationAborted (995) when GetContext() is called after Stop() on Linux to match Windows behavior. Add test to verify the error code is consistent across platforms. Co-authored-by: wfurt <14356188+wfurt@users.noreply.github.com>
|
/azp list |
|
/azp run runtime-libraries-coreclr outerloop-linux |
|
Azure Pipelines successfully started running 1 pipeline(s). |
src/libraries/System.Net.HttpListener/tests/HttpListenerTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.HttpListener/src/System/Net/Managed/ListenerAsyncResult.Managed.cs
Show resolved
Hide resolved
|
/azp run runtime-libraries-coreclr outerloop-windows |
|
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Pull request overview
This PR aligns the exception behavior of HttpListener.GetContext() across platforms. Previously, when called after Stop(), Windows threw HttpListenerException with ErrorCode = 995 (SocketError.OperationAborted), while Linux threw ErrorCode = 500 (HttpStatusCode.InternalServerError). The fix ensures both platforms consistently use SocketError.OperationAborted.
Changes:
- Updated Linux implementation to throw
HttpListenerExceptionwithSocketError.OperationAbortederror code (995) instead ofHttpStatusCode.InternalServerError(500) - Added test to verify cross-platform consistency of error code
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| ListenerAsyncResult.Managed.cs | Changed error code from HttpStatusCode.InternalServerError to SocketError.OperationAborted when ObjectDisposedException is caught; added required using directive |
| HttpListenerTests.cs | Added test to verify GetContext() throws HttpListenerException with error code 995 when called after Stop() |
Use TaskCompletionSource to ensure GetContext() is called before Stop() to avoid race condition where Stop() could be called first, causing InvalidOperationException instead of HttpListenerException. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Head branch was pushed to by a user without write access
Add comprehensive tests to verify that GetContext() throws HttpListenerException with SocketError.OperationAborted for all termination scenarios: Stop(), Abort(), and Close(). All three methods call Cleanup() which creates ObjectDisposedException for pending async operations, which is now consistently converted to OperationAborted. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
Consolidated three nearly identical tests into a single parameterized test using xUnit's [Theory] and [InlineData] attributes to test Stop(), Abort(), and Close() scenarios with the same logic. Co-authored-by: wfurt <14356188+wfurt@users.noreply.github.com>
Description
When
HttpListener.GetContext()is called afterStop(),Abort(), orClose(), Windows throwsHttpListenerExceptionwithErrorCode = 995(SocketError.OperationAborted), but Linux throwsErrorCode = 500(HttpStatusCode.InternalServerError). This breaks cross-platform code that handles these scenarios.Changes
ListenerAsyncResult.Managed.csHttpStatusCode.InternalServerErrortoSocketError.OperationAbortedwhenObjectDisposedExceptionis caught duringGetContext()(line 71)using System.Net.Sockets;directiveHttpListenerTests.cs[Theory]and[InlineData]to verify error code is995on all platforms for all termination scenarios (Stop, Abort, Close)GetContext_TerminationMethodCalled_ThrowsHttpListenerExceptionWithOperationAbortedTaskCompletionSourcefor proper test synchronization to avoid race conditions in CI where termination could be called beforeGetContext()enters its blocking stateCode Path Analysis
Stop(),Abort(),Close()) callCleanup()which creates a singleObjectDisposedException("listener")for pending operations_inGetflag ensures the fix only affects synchronousGetContext()calls, not asyncBeginGetContext/EndGetContextExample
Microsoft Reviewers
[CC @wfurt as requested]
Checklist
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.