Session leak in the Query Plan Cache

Description

It looks like whenever a session is used for the first, uncached execution of a query, the Query Plan Cache will hold a reference to that NhQueryable (and hence, that session) via the Expression Tree generated by the Linq extension methods.

Here is an example of a SOS.dll !gcroot dump:

Thread 2264:

  •  

    •  

      • WARNING: Unable to verify checksum for System.ni.dll
        0000000018e6ecb0 000007fef6638de6 System.Net.TimerThread.ThreadProc()
        r12: (interior)
        -> 00000006fff977e8 System.Object[]
        -> 00000003fff90658 System.Web.Hosting.ObjectCacheHost
        -> 00000006800b5cd0 System.Collections.Generic.Dictionary`2[[System.Runtime.Caching.MemoryCache, System.Runtime.Caching],[System.Web.Hosting.ObjectCacheHost+MemoryCacheInfo, System.Web]]
        -> 00000006800b5df0 System.Collections.Generic.Dictionary`2+Entry[[System.Runtime.Caching.MemoryCache, System.Runtime.Caching],[System.Web.Hosting.ObjectCacheHost+MemoryCacheInfo, System.Web]][]
        -> 00000004fffc3248 System.Runtime.Caching.MemoryCache
        -> 00000004fffc3298 System.Object[]
        -> 00000004fffc4410 System.Runtime.Caching.MemoryCacheStore
        -> 00000004fffc44e0 System.Runtime.Caching.CacheExpires
        [... removed repetitive cache timer instances ...]
        -> 00000003fff8da38 System.Web.RequestTimeoutManager
        -> 00000003fff8da70 System.Object[]
        -> 00000003fff8db20 System.Web.Util.DoubleLinkList
        -> 0000000403438450 System.Web.RequestTimeoutManager+RequestTimeoutEntry
        -> 0000000403436ee8 System.Web.HttpContext
        -> 0000000502f505b0 System.Collections.Hashtable
        -> 0000000502f856d8 System.Collections.Hashtable+bucket[]
        -> 0000000502f50660 Autofac.Core.Lifetime.LifetimeScope
        -> 0000000502f506e0 System.Collections.Generic.Dictionary`2[[System.Guid, mscorlib],[System.Object, mscorlib]]
        -> 0000000502f91678 System.Collections.Generic.Dictionary`2+Entry[[System.Guid, mscorlib],[System.Object, mscorlib]][]
        -> 0000000502f51e58 NHibernate.Impl.SessionImpl
        -> 00000001fff942e8 NHibernate.Impl.SessionFactoryImpl
        -> 00000001fff95588 NHibernate.Engine.Query.QueryPlanCache
        -> 00000001fff96918 NHibernate.Util.SoftLimitMRUCache
        -> 00000001fff969b0 NHibernate.Util.LRUMap
        -> 00000001fff969e0 NHibernate.Util.SequencedHashMap+Entry
        -> 00000001002beb80 NHibernate.Util.SequencedHashMap+Entry
        -> 00000001002b9930 NHibernate.Engine.Query.HQLExpressionQueryPlan
        -> 00000001002b9290 NHibernate.Linq.NhLinqExpression
        -> 00000001002b9410 System.Linq.Expressions.MethodCallExpressionN
        -> 00000001002b93f0 System.Runtime.CompilerServices.TrueReadOnlyCollection`1[[System.Linq.Expressions.Expression, System.Core]]
        -> 00000001002b93c0 System.Object[]
        -> 00000001002b91c0 System.Linq.Expressions.ConstantExpression
        -> 00000001002b9188 NHibernate.Linq.NhQueryable`1[[REDACTED]]
        -> 00000001002b91a8 NHibernate.Linq.DefaultQueryProvider
        -> 00000001002b3f08 NHibernate.Impl.SessionImpl

Further notes are available in the Google Groups postings:
https://groups.google.com/d/topic/nhusers/v_6WCod79XE/discussion
https://groups.google.com/d/topic/nhibernate-development/agsCK1cZpkg/discussion

I've got a test case and a candidate fix – I'll make a pull request soon.

Environment

None

Activity

Show:
Oskar Berggren
February 9, 2014 at 9:48 PM

PR 244 didn't work out. A different fix has been merged to master in e8edc0506507348edc1ae937bc3cf7cf29eb21a6.

Lauri Kotilainen
December 16, 2013 at 8:25 AM
Fixed

Details

Assignee

Reporter

Labels

Components

Fix versions

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created December 16, 2013 at 7:54 AM
Updated September 21, 2014 at 12:40 PM
Resolved February 9, 2014 at 9:48 PM
Who's Looking?