Reputation: 2380
I have a .Net Core 3.1 web application and I keep getting "Accessing expired session" exception from "Microsoft.AspNetCore.Session.DistributedSession", which prevents my website from loading.
Steps to reproduce:
At this point the exception just loads up my logs until the web server crashes. The only way to get it to stop is to clear the session cookie in the browser, which will allow it to generate a fresh session cookie.
Why does the session mechanism, which I enabled using AddDistributedMemoryCache and AddSession, not just create a fresh session when it gets this request using the old cookie? If I could catch the exception somewhere, maybe I could deal with it in my code, but I do not even know how to trap the exception. Can anyone offer a way to prevent this error without manually deleting the browser cookie?
Additional Info: I found the source code for this, and apparently, it is not actually an error, but yet it is preventing my page from rendering and it is crashing my webserver with an out of memory exception because it logs the message 1000's of times. Any suggestions for how to get around this?
Upvotes: 4
Views: 1163
Reputation: 2380
After months of digging into this issue, I finally discovered what is going on. I have written a custom ILoggerProvider so that I can control how things going through ILoggerFactory are getting logged. Inside my ILoggerProvider, my code references Session. Every time that Session is referenced, Microsoft's code writes another "Accessing expired session" message to the log, which triggers my code, which accesses Session, and so on, causing an infinite loop. Now, I just need to refactor my ILoggerProvider to be smart enough to avoid causing infinite loops.
Upvotes: 3