I want to throw this out because I’ve been struggling with this particular problem for a couple of weeks off and on now. A couple of weeks ago I switched over this WebLog to a custom weblog engine that I created. For the most part things have been smooth. But I continue to have a big problem with the CAPTCHA control on the form that I use.
The control I use is Jeff Atwood’s CAPTCHA Control from CodeProject and I like the control for its interface and ease of drop and forget operation. Unfortunately I’ve run into some problems with the control not properly serving the images after the app's been runing for a while. I’ve looked over the code very carefully and frankly I don’t see anything wrong with it and of course it doesn’t fail during development even when putting it under load test.
I had the control also running with .Text for a couple of weeks or so, but I didn’t really pay close attention whether it failed or not – I just assumed it worked - it mighta been failing there too occasionally. <shrug>
Here’s what’s happening: When the App first starts everything’s fine. The Captcha displays and everything is peachy. I can access the page with the Captcha on it and it shows. However, after the App’s been running for a few hours the Captcha starts failing intermittently. It shows up most of the time, but not all.
You can click on the bottom of any post on the Add Comment button to display the Captcha and then press Post without any data (Alt-S) to see it fail. When it fails, it seems to cycle and work just about every other time which is also very odd.
I looked over Jeff's code carefully and what the control does is store some information about the image to generate into the Cache object as part of the control, which is then linked to an image that calls an HTTP Handler, which picks up the image data, generates the image and serves it. Very straight forward and I don’t see anything wrong with it.
When this first started happening I was actually able to repro this in my dev environment and what I saw happening is that the item got added to the cache, but when the handler is called the item dropped out of the cache. The cache entry returns NULL (actually nothing since this is VB code <s>).
The item goes into the cache with:
HttpContext.Current.Cache.Add(LocalGuid, _captcha, Nothing, DateTime.UtcNow.AddSeconds(25.0), System.Web.Caching.Cache.NoSlidingExpiration, Caching.CacheItemPriority.Normal, Nothing)
And I could see that this was definitely happening. The handler then retrieves with:
TCImage = app.Context.Cache.Get(strGuid)
ci = CType(TCImage, CaptchaImage)
And it turns out that ci comes back nothing occasionally.
I’ve been going over this over and over varying the way things are going in the cache, changing the timeout, changing priority but I continue to see the failures on my site.
So yesterday I started looking at cache entries in the application with PerfMon on the server and the number of cache entries is very low and steady. It’s not like Cache entries are accumulating rapidly.
But it appears as if ASP.NET is simply not adding the item to the cache or adding it and quickly dumping the value back out. As I said the app doesn’t make heavy use of Cache and the memory usage looks good as well (it’s running about 40 megs steady and I moved it to its own AppPool to cut down on any memory starvation from the busy AppPool it's been running in).
The machine does not have a ton of memory and there’s a bunch of stuff running on it, but it’s not critically low on memory and certainly not low enough that I would think adding a couple of cache entries would cause removing of entries nearly instantly.
I’m running out of ideas. I can’t duplicate any longer in development mode after making a few minor changes to cache priority, but it continues to fail on the server, but only after the apps been running for a while. Restart the app and all is fine again for quite a while which does seem to indicate a memory problem. However, I find that REALLY hard to believe given this is a small object and they’re not being created in large quantities since you have to click an Add Comment button first before the Captcha shows.
Has anybody seen behavior like this before in Caching? Has anybody run into this with Jeff’s control and figured out what the issue is? I actually suspect there’s nothing wrong with Jeff’s code – I suspect my environment, but you never know. The code looks fine to me (and I went over it carefully <s>).
Incidentally I musta driven Jeff mad with my notes since I thought I had it fixed a few times after tweaking the code slightly and then turning around and going – nope still failing. Sorry dude <g>…
Other Posts you might also like