ASP.NET application throws System.NullReferenceException from Session.Remove implementation

穿精又带淫゛_ 提交于 2019-12-05 08:40:38

The issue may occur when the requests made by the same user (from the same session ID) are processed concurrently.

This is because System.Web.SessionState.SessionStateItemCollection doesn't check the entry to be null when removing it.

In the System.Web.SessionState.SessionStateItemCollection.Remove(string name) method the lock is set on the private collection _serializedItems so that Remove operation is thread safe. Yet this private collection doesn't get locked during Add operations. Considering that _serializedItems behaves as a resizing array (when entries count is >= 1/2 of its capacity the capacity is doubled and new entries are filled with nulls) concurrent addition may result into situations when null values remain in the middle of the collection. The entries only get removed from [0; entries count] range of the _serializedItems collection. When removing the entry its Key property gets accessed without null checking. Thus if null entry form the middle of the collection is removed the NullReferenceException gets cast.

By default all requests with same session ID are processed sequentially unless System.Web.Configuration.PagesSection.EnableSessionState is explicitly set to ReadOnly. In this case any operation that involves adding values into System.Web.SessionState.HttpSessionState (which internally uses System.Web.SessionState.HttpSessionStateContainer which in turn internally uses System.Web.SessionState.SessionStateItemCollection) should be properly locked to assure thread safety.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!