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

Fix concurrency problem in FileWebRequest resulting in sporadic test failures - #12231

Merged
stephentoub merged 2 commits into
dotnet:masterfrom
stephentoub:fix_fwr_concurrency
Oct 1, 2016
Merged

stephentoub merged 2 commits into
dotnet:masterfrom
stephentoub:fix_fwr_concurrency

Conversation

@stephentoub

Copy link
Copy Markdown
Member

When I ported FileWebRequest/Response to corefx, I added a test that invokes GetRequestStreamAsync and GetResponseAsync to run concurrently with each other, as there's logic in the implementation (for reasons I'm not clear about) that suggests this should work, by blocking the latter until the former has completed. However, there's a race condition in the product that's causing the test to fail.

FileWebRequest overrides the base's Begin/EndXx methods. It does not override the XxAsync methods. The base XxAsync implementations just wrap the Begin/End calls, but for whatever reason they also wrap it in a Task.Run. When FileWebRequest inherits that implementation, it causes problems for the test, as the marking that operations are in progress happens in the Begin overrides, which means they happen as part of the queued tasks from the base implementation. In rare cases where the BeginGetResponse task runs first, or where it runs in parallel with the BeginGetRequestStream task, problems occur.

Three (simple) parts to the fix:

  1. Override GetRequestStreamAsync/GetResponse so as to avoid the extra task the base class queues; it's unnecessary, causes this race condition, and adds a bit of overhead. I did some small refactoring to reuse all of the other logic between the XxAsync and Begin/EndXx overrides.
  2. Make sure this concurrency test doesn't run with the synchronous methods, which would potentially manifest the same problem.
  3. Since with (1) the Task-based APIs no longer wrap the Begin/End APIs, make sure all of the tests also run with the Begin/End APIs.

cc: @ericeil

@ericeil ericeil left a comment

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.

LGTM; just one question about the test.

[Fact]
public async Task ConcurrentReadWrite_ResponseBlocksThenGetsNullStream()
{
if (!EnableConcurrentReadWriteTests) return;

@ericeil ericeil Sep 30, 2016 •

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.

Kind of odd to have a test that does nothing. Can we move this test method into the derived class(es) that do support concurrent tests?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, I could create an intermediate base abstract class for the async ones and put it there.

…failures

When I ported FileWebRequest/Response to corefx, I added a test that invokes GetRequestStreamAsync and GetResponseAsync to run concurrently with each other, as there's logic in the implementation (for reasons I'm not clear about) that suggests this should work, by blocking the latter until the former has completed.  However, there's a race condition in the product that's causing the test to fail.

FileWebRequest overrides the base's Begin/EndXx methods.  It does not override the XxAsync methods.  The base XxAsync implementations just wrap the Begin/End calls, but for whatever reason they also wrap it in a Task.Run.  When FileWebRequest inherits that implementation, it causes problems for the test, as the marking that operations are in progress happens in the Begin overrides, which means they happen as part of the queued tasks from the base implementation.  In rare cases where the BeginGetResponse task runs first, or where it runs in parallel with the BeginGetRequestStream task, problems occur.

Three (simple) parts to the fix:
1. Override GetRequestStreamAsync/GetResponse so as to avoid the extra task the base class queues; it's unnecessary, causes this race condition, and adds a bit of overhead.  I did some small refactoring to reuse all of the other logic between the XxAsync and Begin/EndXx overrides.
2. Make sure this concurrency test doesn't run with the synchronous methods, which would potentially manifest the same problem.
3. Since with (1) the Task-based APIs no longer wrap the Begin/End APIs, make sure all of the tests also run with the Begin/End APIs.
@stephentoub
stephentoub merged commit 344c008 into dotnet:master Oct 1, 2016
@stephentoub
stephentoub deleted the fix_fwr_concurrency branch October 1, 2016 11:11
@karelz karelz modified the milestone: 1.2.0 Dec 3, 2016
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…rrency

Fix concurrency problem in FileWebRequest resulting in sporadic test failures

Commit migrated from dotnet/corefx@344c008
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants