RavenDb Test Driver preventing AppDomain from unloading

54 views
Skip to first unread message

Daniel Pilon

unread,
Jun 18, 2018, 7:42:53 PM6/18/18
to RavenDB - 2nd generation document database
Hi, 

When running RavenDb using Test Driver inside an App Domain, a CannotUnloadAppDomainException is being thrown. The code snippet below reproduces the issue:

            {
            var domain = AppDomain.CreateDomain("RavenDb Client", null, AppDomain.CurrentDomain.SetupInformation);

            try
            {
                domain.DoCallBack(() =>
                {
                    using (var testDriver = new RavenTestDriverImpl())
                    {
                        var documentStore = testDriver.GetDocumentStore();
                        Console.WriteLine(string.Join("\n", documentStore.Urls));
                        testDriver.Dispose();
                    }
                });
            }
            finally
            {
                try
                {
                    AppDomain.Unload(domain);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

            Console.ReadKey();
        }

In the Parallel Stacks view, when the breakpoint reaches the AppDomain.Unload(domain); line I can see the following (can't say for sure that it is related):

The call stack says that this is coming from Sparrow.dll

Can someone help me with that? 

Thanks.

Oren Eini (Ayende Rahien)

unread,
Jun 18, 2018, 8:49:15 PM6/18/18
to ravendb
The root cause here is the memory notification thread.
We need that to be able to raise low memory events, such as when your HttpCache inside the DocumentStore is causing to much memory usage and should be cleared.
I have to admit that the isolated app domain scenario is interesting and not one that we considered. .NET is general seems to be going away from such usage.

What is the scenario you have here? 

Note that this is actually the client API that is causing this, not RavenTest.

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Pilon

unread,
Jun 18, 2018, 9:45:08 PM6/18/18
to RavenDB - 2nd generation document database
We have some degree of context isolation using AppDomains, in which we create Document Stores for each database that serves the current context. This wasn't happening while using 2.5. I know AppDomains are running out of steam, but can't remove from my application at this point. I tried to simulate this with a regular instance of Document Store, but had no issue. Only when using the Test Driver. 

Another question on top of this information is that we are disposing the Document Store and only then we invoke the AppDomain.Unload(). Is there a reason this thread wasn't or cannot be disposed together with the Document Store? 
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
Jun 18, 2018, 10:30:55 PM6/18/18
to ravendb
We keep it around, you can handle this using this method:

LowMemoryNotification.Initialize(Configuration.Memory.LowMemoryLimit, Configuration.Memory.MinimumFreeCommittedMemory, ServerShutdown);

The ServerShutdown is a cancellation token, when cancelled, it will 

At the end of you run, just call it with:

  var cts = new CancellationTokenSource();
cts.Cancel();
LowMemoryNotification.Initialize(Size.Zero, Size.Zero,  cts.Token );
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.

Daniel Pilon

unread,
Jun 20, 2018, 9:26:57 AM6/20/18
to RavenDB - 2nd generation document database
Oren, still not working... Changed my code to do it like this and exceptions are still being thrown while unloading the AppDomain.

static void Main(string[] args)
        {
            var domain = AppDomain.CreateDomain("RavenDb Client", null, AppDomain.CurrentDomain.SetupInformation);

            try
            {
                domain.DoCallBack(() =>
                {
                    using (var testDriver = new RavenTestDriverImpl())
                    {
                        var documentStore = testDriver.GetDocumentStore();
                        Console.WriteLine(string.Join("\n", documentStore.Urls));
                    }
                    var cts = new CancellationTokenSource();
                    cts.Cancel();
                    LowMemoryNotification.Initialize(Size.Zero, 0, cts.Token);

Oren Eini (Ayende Rahien)

unread,
Jun 20, 2018, 4:28:06 PM6/20/18
to ravendb
Can you send a full repro?
I'm not sure what RavenTestDriverImpl is doing exactly.
This works on my end, mind:

domain.DoCallBack(() =>
{
    LowMemoryNotification.Instance.RegisterLowMemoryHandler(null);
    var cts = new CancellationTokenSource();
    cts.Cancel();
    LowMemoryNotification.Initialize(Sparrow.Size.Zero, 0, cts.Token);
});

To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.

Daniel Pilon

unread,
Jun 20, 2018, 5:00:42 PM6/20/18
to RavenDB - 2nd generation document database

Paweł Pekról

unread,
Jun 21, 2018, 10:26:43 AM6/21/18
to RavenDB - 2nd generation document database
Hi Daniel,

we fixed the issue with unloading the domain on full .NET. Thanks for submitting the repro. If you are interested, it is available in nightly: https://myget.org/feed/ravendb/package/nuget/RavenDB.TestDriver/4.0.6-nightly-20180621-1359

Daniel Pilon

unread,
Jun 21, 2018, 12:43:54 PM6/21/18
to RavenDB - 2nd generation document database
Thanks, Pawel. Do you have an idea when you are going to release an official new version? Our CI doesn't accept prerelease packages.

Paweł Pekról

unread,
Jun 22, 2018, 4:04:02 AM6/22/18
to RavenDB - 2nd generation document database
Hi Daniel, we are planning to release first 4.0.6-patch in week or two.

P.

Daniel Pilon

unread,
Jun 23, 2018, 11:12:39 AM6/23/18
to RavenDB - 2nd generation document database
That's good. Thanks.
Reply all
Reply to author
Forward
0 new messages