Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Fix incorrect exception thrown in HttpResponseStream.Write for a closed connection - #19219

Merged
stephentoub merged 5 commits into
dotnet:masterfrom
hughbe:httplistener-win
May 13, 2017
Merged

stephentoub merged 5 commits into
dotnet:masterfrom
hughbe:httplistener-win

Conversation

@hughbe

@hughbe hughbe commented May 2, 2017

Copy link
Copy Markdown

Contributes to #18128

public static void WaitForSocketShutdown(Socket socket)
{
while (SocketConnected(socket));
if (PlatformDetection.IsWindows)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These shenanigans are needed because the native implementation is dumb and doesn't know when a socket is closed, but the managed implementation uses sockets directly so this is fine

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and the old WaitForSocketShutdown implementation hanged forever.

@stephentoub stephentoub left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Thanks, @hughbe.

@stephentoub

stephentoub commented May 2, 2017 •

Copy link
Copy Markdown
Member

Looks like one of the tests is having trouble on macOS:

    System.Net.Tests.HttpResponseStreamTests.Write_HeadersToClosedConnectionSynchronously_ThrowsHttpListenerException(ignoreWriteExceptions: False) [FAIL]
        Assert.Throws() Failure
        Expected: typeof(System.Net.HttpListenerException)
        Actual:   (No exception was thrown)
        Stack Trace:
              at System.Net.Tests.HttpResponseStreamTests.<Write_HeadersToClosedConnectionSynchronously_ThrowsHttpListenerException>d__21.MoveNext()
           --- End of stack trace from previous location where exception was thrown ---
              at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
              at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
           --- End of stack trace from previous location where exception was thrown ---
              at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
              at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
           --- End of stack trace from previous location where exception was thrown ---
              at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
              at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     System.Net.Tests.HttpResponseStreamTests.Write_HeadersToClosedConnectionAsynchronously_ThrowsHttpListenerException(ignoreWriteExceptions: False) [FAIL]
        Assert.Throws() Failure
        Expected: typeof(System.Net.HttpListenerException)
        Actual:   (No exception was thrown)
        Stack Trace:
           --- End of stack trace from previous location where exception was thrown ---
              at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
              at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
              at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
              at System.Net.Tests.HttpResponseStreamTests.<Write_HeadersToClosedConnectionAsynchronously_ThrowsHttpListenerException>d__20.MoveNext()
           --- End of stack trace from previous location where exception was thrown ---
              at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
              at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
           --- End of stack trace from previous location where exception was thrown ---
              at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
              at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
           --- End of stack trace from previous location where exception was thrown ---
              at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
              at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

@hughbe

hughbe commented May 2, 2017

Copy link
Copy Markdown
Author

Write_HeadersToClosedConnectionSynchronously_ThrowsHttpListenerException
Write_HeadersToClosedConnectionAsynchronously_ThrowsHttpListenerException

This smells like a race condition again, so I'll need to investigate this more.

@stephentoub

Copy link
Copy Markdown
Member

This smells like a race condition again

Potentially, though "race condition" generally isn't my first instinct when the same test fails on all legs for a specific platform and only that platform.

@hughbe

hughbe commented May 3, 2017

Copy link
Copy Markdown
Author

I only mentioned race condition as I thought it's possibly similar to https://github.com/dotnet/corefx/issues/18188

But granted, it's odd that it's only OSX failing. I don't know enough about the S.Net or macos networking stack to say why, but I've pushed a commit that may fix this, or may hang forever if it has the same behaviour as Linux

@danmoseley

Copy link
Copy Markdown

@stephentoub I think @hughbe is stumped by the osx failures. I'm guessing he doesn't have a mac to debug. any thoughts?

@stephentoub

stephentoub commented May 5, 2017 •

Copy link
Copy Markdown
Member

@dotnet-bot test Innerloop OSX10.12 Debug x64 Build and Test please
@dotnet-bot test Innerloop OSX10.12 Release x64 Build and Test please
(logs were deleted)

@danmoseley

Copy link
Copy Markdown

@stephentoub you got your logs

    System.Net.Tests.HttpResponseStreamTests.Write_HeadersToClosedConnectionSynchronously_ThrowsHttpListenerException(ignoreWriteExceptions: False) [FAIL]
10:29:33         Assert.Throws() Failure
10:29:33         Expected: typeof(System.Net.HttpListenerException)
10:29:33         Actual:   (No exception was thrown)
10:29:33         Stack Trace:
10:29:33            /Users/dotnet-bot/j/workspace/dotnet_corefx/master/osx10.12_release_prtest/src/System.Net.HttpListener/tests/HttpResponseStreamTests.cs(453,0): at System.Net.Tests.HttpResponseStreamTests.<Write_HeadersToClosedConnectionSynchronously_ThrowsHttpListenerException>d__21.MoveNext()
10:29:33            --- End of stack trace from previous location where exception was thrown ---
10:29:33               at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
10:29:33               at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
10:29:33            --- End of stack trace from previous location where exception was thrown ---
10:29:33               at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
10:29:33               at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
10:29:33            --- End of stack trace from previous location where exception was thrown ---
10:29:33               at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
10:29:33               at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
10:29:33      

@hughbe

hughbe commented May 8, 2017

Copy link
Copy Markdown
Author

Friendly ping @stephentoub so I have time to potentially fix this before ZBB

@davidsh davidsh removed their assignment May 9, 2017
@danmoseley

Copy link
Copy Markdown

@dotnet-bot test Innerloop OSX10.12 Release x64 Build and Test (why not)

@stephentoub

Copy link
Copy Markdown
Member

@hughbe, to avoid holding up the whole PR, go ahead and disable this test on macOS.

@hughbe

hughbe commented May 9, 2017 •

Copy link
Copy Markdown
Author

Sure. Can't do that until tomorrow but I think it'll be OK as I'm 15rs forward or so, so this can still make XBB. Do you want me to make an issue?

@stephentoub

Copy link
Copy Markdown
Member

Can't do that until tomorrow

I'll take care of it.

@stephentoub

Copy link
Copy Markdown
Member

@dotnet-bot test Innerloop netfx Windows_NT Debug x86 Build and Test please

while (SocketConnected(socket));
if (PlatformDetection.IsWindows || PlatformDetection.IsOSX)
{
socket.Shutdown(SocketShutdown.Both);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the Shutdown call not done on Linux?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hughbe has commented before on previous commit "These shenanigans are needed because the native implementation is dumb and doesn't know when a socket is closed, but the managed implementation uses sockets directly so this is fine". I think he hypothesizes, that on windows we call into the native apis which does the connections, whereas in managed httplistener we use the socket apis, so the close socket behavior is taking long on windows to terminate connection on other side. But we can unify code and have shutdown and the while check on all platforms, i guess he just did the special case on the platform that was failing consistently.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the managed implementation on macOS as well, so that doesn't explain why the check is for IsWindows || IsOSX.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my Ubuntu VM these tests all hung without this code. I added OSX because I suspected that's what caused the OSX failures. (I was wrong)

I'm also now a bit nervous about these tests, as i dont know if they're going to fail in the CI due to timing etc
I've ran the tests on windows 1000 times and nothing failed but I'm not sure about ubuntu so I will test that before merging. Seems like the CI hates me

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the CI hates me

Sometimes we all feel that way :)

@stephentoub stephentoub May 11, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have expected on all platforms you could do:

socket.Shutdown(SocketShutdown.Both);
while (SocketConnected(socked));
socket.Close();

@CIPop CIPop removed their assignment May 11, 2017
hughbe and others added 3 commits May 13, 2017 08:49
I don't have a $2000 block of very well brushed aluminium to test this
on. This might hang, or it might work.
@stephentoub

Copy link
Copy Markdown
Member

@dotnet-bot test outerloop netfx Windows_NT Debug
@dotnet-bot test outerloop netcoreapp Windows_NT Debug
@dotnet-bot test outerloop netcoreapp Ubuntu16.10 Debug
@dotnet-bot test outerloop netcoreapp OSX10.12 Debug

@stephentoub

Copy link
Copy Markdown
Member

@ericstj, @erozenfeld illink seg faulted:
https://ci.dot.net/job/dotnet_corefx/job/master/job/ubuntu16.04_arm_cross_debug_prtest/2620/consoleText

  Segmentation fault (core dumped)
/opt/corefx/illink.targets(103,5): error MSB3073: The command "/opt/corefx/Tools/dotnetcli/dotnet "/opt/corefx/Tools/ILLink/illink.dll"  -r System.Diagnostics.FileVersionInfo -d /opt/corefx/bin/obj/Unix.AnyCPU.Debug/System.Diagnostics.FileVersionInfo/netcoreapp/PreTrim/ -d /opt/corefx/bin/ref/netcoreapp/ -d /opt/corefx/bin/AnyOS.AnyCPU.Debug/System.Reflection.Metadata/netcoreapp/ -d /opt/corefx/Tools/ -c skip -p skip netstandard -t -out /opt/corefx/bin/obj/Unix.AnyCPU.Debug/System.Diagnostics.FileVersionInfo/netcoreapp/ -b true -v true -h LdtokenTypeMethods,InstanceConstructors" exited with code 139. 

@stephentoub

Copy link
Copy Markdown
Member

@dotnet-bot test Innerloop Ubuntu16.04 arm Debug Cross Build please (seg fault in illink)
@dotnet-bot test OuterLoop netcoreapp Windows_NT Debug x64 please (https://github.com/dotnet/corefx/issues/19702)
@dotnet-bot test OuterLoop netfx Windows_NT Debug x64 please (https://github.com/dotnet/corefx/issues/11737)

@stephentoub
stephentoub merged commit 977712e into dotnet:master May 13, 2017
@hughbe
hughbe deleted the httplistener-win branch May 14, 2017 02:54
@erozenfeld

Copy link
Copy Markdown
Contributor

@swaroop-sridhar @sbomer @russellhadley
illink seg faulted: https://ci.dot.net/job/dotnet_corefx/job/master/job/ubuntu16.04_arm_cross_debug_prtest/2620/consoleText

  Segmentation fault (core dumped)
/opt/corefx/illink.targets(103,5): error MSB3073: The command "/opt/corefx/Tools/dotnetcli/dotnet "/opt/corefx/Tools/ILLink/illink.dll"  -r System.Diagnostics.FileVersionInfo -d /opt/corefx/bin/obj/Unix.AnyCPU.Debug/System.Diagnostics.FileVersionInfo/netcoreapp/PreTrim/ -d /opt/corefx/bin/ref/netcoreapp/ -d /opt/corefx/bin/AnyOS.AnyCPU.Debug/System.Reflection.Metadata/netcoreapp/ -d /opt/corefx/Tools/ -c skip -p skip netstandard -t -out /opt/corefx/bin/obj/Unix.AnyCPU.Debug/System.Diagnostics.FileVersionInfo/netcoreapp/ -b true -v true -h LdtokenTypeMethods,InstanceConstructors" exited with code 139. 

@swaroop-sridhar

Copy link
Copy Markdown
Contributor

I'll take a look, I'm trying to repro the build failure locally now.

@swaroop-sridhar

Copy link
Copy Markdown
Contributor

All of the recent runs have passed in this leg:
https://ci.dot.net/job/dotnet_corefx/job/master/job/ubuntu16.04_arm_cross_debug_prtest/

I can try to take down a jenkins node and repro the failure -- but I doubt if it'll repro.

@karelz karelz modified the milestone: 2.0.0 May 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants