Skip to content

HttpListener.Stop closes the request queue handle with CloseIoEx method - #684

Merged
alnikola merged 8 commits into
dotnet:masterfrom
alnikola:alnikola/28169-stop-dont-unblock-get-ctx
Dec 12, 2019
Merged

alnikola merged 8 commits into
dotnet:masterfrom
alnikola:alnikola/28169-stop-dont-unblock-get-ctx

Conversation

@alnikola

@alnikola alnikola commented Dec 9, 2019

Copy link
Copy Markdown
Contributor

Currently, if HttpListener.Stop is called while the other thread is blocked inside a synchronous GetContext cal, it has no effect because Stop cannot dispose the request queue handle. This is caused by a reference counting mechanism implemented in SafeHandle which prevents disposal of the native handle due to the following reason.

GetContext method invokes the native HttpReceiveHttpRequest to start listening for incoming requests. HttpReceiveHttpRequest is called through P/Invoke and accepts SafeHandle as a request queue handle. In such case, runtime injects a special call to SafeHandle.DangerousAddRef incrementing the ref counter to prevent an accidental disposal from the managed side. In general, it's a good safety measure, but in the given scenario it prevents SafeHandle.Dispose from actually closing the underlying OS handle thus GetContext gets blocked until a next request arrives.

To overcome this limitation, PR adds call to CloseIoEx before invoking Dispose to close the request queue and unblock GetContext.

@alnikola

alnikola commented Dec 9, 2019

Copy link
Copy Markdown
Contributor Author

/azp run libraries-outerloop

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

@alnikola

alnikola commented Dec 9, 2019

Copy link
Copy Markdown
Contributor Author

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Code style fixes
@alnikola

Copy link
Copy Markdown
Contributor Author

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@alnikola

Copy link
Copy Markdown
Contributor Author

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@alnikola

Copy link
Copy Markdown
Contributor Author

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@davidsh davidsh added this to the 5.0 milestone Dec 11, 2019
@alnikola

Copy link
Copy Markdown
Contributor Author

/azp run runtime-libraries innerloop

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

@alnikola

Copy link
Copy Markdown
Contributor Author

/azp run runtime-libraries

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@alnikola

Copy link
Copy Markdown
Contributor Author

/azp run runtime-libraries outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@alnikola
alnikola merged commit 2aeab34 into dotnet:master Dec 12, 2019
@alnikola
alnikola deleted the alnikola/28169-stop-dont-unblock-get-ctx branch December 12, 2019 12:22
@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
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.

6 participants