Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 139301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:24:37+00:00 2026-05-11T07:24:37+00:00

I have an ASP.NET application with a lot of dynamic content. The content is

  • 0

I have an ASP.NET application with a lot of dynamic content. The content is the same for all users belonging to a particular client. To reduce the number of database hits required per request, I decided to cache client-level data. I created a static class (‘ClientCache’) to hold the data.
The most-often used method of the class is by far ‘GetClientData’, which brings back a ClientData object containing all stored data for a particular client. ClientData is loaded lazily, though: if the requested client data is already cached, the caller gets the cached data; otherwise, the data is fetched, added to the cache and then returned to the caller.

Eventually I started getting intermittent crashes in the the GetClientData method on the line where the ClientData object is added to the cache. Here’s the method body:

public static ClientData GetClientData(Guid fk_client) {     if (_clients == null)         _clients = new Dictionary<Guid, ClientData>();      ClientData client;     if (_clients.ContainsKey(fk_client))     {         client = _clients[fk_client];     }     else     {         client = new ClientData(fk_client);         _clients.Add(fk_client, client);     }     return client; } 

The exception text is always something like ‘An object with the same key already exists.’ Of course, I tried to write the code so that it just wasn’t possible to add a client to the cache if it already existed.

At this point, I’m suspecting that I’ve got a race condition and the method is being executed twice concurrently, which could explain how the code would crash. What I’m confused about, though, is how the method could be executed twice concurrently at all. As far as I know, any ASP.NET application only ever fields one request at a time (that’s why we can use HttpContext.Current).

So, is this bug likely a race condition that will require putting locks in critical sections? Or am I missing a more obvious bug?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:24:38+00:00Added an answer on May 11, 2026 at 7:24 am

    If an ASP.NET application only handles one request at a time all ASP.NET sites would be in serious trouble. ASP.NET can process dozens at a time (typically 25 per CPU core).

    You should use ASP.NET Cache instead of using your own dictionary to store your object. Operations on the cache are thread-safe.

    Note you need to be sure that read operation on the object you store in the cache are threadsafe, unfortunately most .NET class simply state the instance members aren’t thread-safe without trying to point any that may be.

    Edit:

    A comment to this answer states:-

    Only atomic operations on the cache are thread safe. If you do something like check
    if a key exists and then add it, that is NOT thread safe and can cause the item to
    overwritten.

    Its worth pointing out that if we feel we need to make such an operation atomic then the cache is probably not the right place for the resource.

    I have quite a bit of code that does exactly as the comment describes. However the resource being stored will be the same in both places. Hence if an existing item on rare occasions gets overwritten the only the cost is that one thread unnecessarily generated a resource. The cost of this rare event is much less than the cost of trying to make the operation atomic every time an attempt to access it is made.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 58k
  • Answers 58k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I decided to write my own ReadLine parser-ish kinda thing.… May 11, 2026 at 8:49 am
  • added an answer Try giving the full path to apache2ctl. May 11, 2026 at 8:49 am
  • added an answer I purchased Microsoft Visual Studio 2005 Professional Edition (C5E-00001). Included… May 11, 2026 at 8:49 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.