Wired async behavior under ASP.NET

107 views
Skip to first unread message

Fitzchak Yitzchaki

unread,
May 22, 2013, 6:43:23 AM5/22/13
to <ravendb@googlegroups.com>
I'm experiencing a wired condition which leads to a deadlock.

In order to reproduce, please create an MVC4 project which is targeting the .NET framework 4.0 (this is happens also with 4.5), and use the following code:

public class BulkController : Controller
{
public class User
{
public string Id { get; set; }
public string Name { get; set; }
}

public object Users()
{
using (var store = new DocumentStore { Url = "http://localhost:8080", DefaultDatabase = "AsyncTest", }.Initialize())
{
for (int i = 0; i < 1000; i++)
{
using (var s = store.BulkInsert())
{
for (int j = 0; j < 1000; j++)
{
s.Store(new User { Name = "User-" + i + "-" + j });
}
}
}
}
return Json("ok");
}
}

The thing is that the code is the code is hang here, waiting for the task to finish, but what that interesting that the task if finished. Try it by yourself.

And more then that, when putting some break points in the above code, the continue and hands in a different place. This is probably related to the way the scedualler works when using the VS, but I really do not understand what is going on.

This is happening just when using ASP.NET, I wasn't able to reproduce this when running in a console.

Looking in the concurrent stacks window I see something interesting - LegacyAspNetSynchronizationContext:

Inline image 1

Not sure what it is but a quick search found this:
image.png

Kijana Woodard

unread,
May 22, 2013, 8:34:42 AM5/22/13
to rav...@googlegroups.com

Hmmmm. I've had idle thoughts about why my bulk insert code want as async as in thought it would be. In my case the behavior is desirable so I didn't give it much thought. After reading your post, I realize exciting the using block is waiting for the insert to finish.

In my case, I'm in a console app, so all is well. In a web app, you have the problem of background threads deadlocking the request thread. There is a technique for dealing with this that I can't remember atm. Will look for it in a bit.

P.S.
Did you mean 'Weird' async behavior?

--
You received this message because you are subscribed to the Google Groups "ravendb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
image.png

Fitzchak Yitzchaki

unread,
May 22, 2013, 8:40:10 AM5/22/13
to <ravendb@googlegroups.com>
Ah.. I meant weird of course, yes.

I tried setting the bellow code in a ASP.NET 4.5 application, but I got the same result.

<appSettings>
   <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
image.png

Fitzchak Yitzchaki

unread,
May 22, 2013, 9:50:32 AM5/22/13
to <ravendb@googlegroups.com>
SynchronizationContext.SetSynchronizationContext(null);
If I set the SynchronizationContext to null, as what the SynchronizationContext is in a console app, the problem is fixed.
image.png

Kijana Woodard

unread,
May 22, 2013, 9:59:35 AM5/22/13
to rav...@googlegroups.com
This is the article I was thinking of: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

Does that explain the situation you are seeing?

I took a quick peek at the source for bulk insert. 

Does the article point out any improvements that could go there?
image.png

Oren Eini (Ayende Rahien)

unread,
May 23, 2013, 4:53:36 AM5/23/13
to ravendb
I think we got it, next build will have all the fixes.
image.png
Reply all
Reply to author
Forward
0 new messages