What might cause a Stack Overflow during linq iteration of Dictionary?

I have the following dictionary:

Dictionary<long, ChangeLogProcess> _changeLogProcesses = new Dictionary<long, ChangeLogProcess>();

I have a method that attempts to get the next changelogprocess in the dictionary of a particular status (If there are no items of a particular status it returns null):

var changeLogProcesses = from entry in _changeLogProcesses where (entry.Value.Status == status) select entry.Value; changeLogProcess = changeLogProcesses.FirstOrDefault<ChangeLogProcess>();

However, during execution it is throwing a stack overflow exception during the linq query? I have done numerous tests to make sure that there are items in teh list and so on but the problem persists?

It's worth noting that this method is part of a service that is running in a multi threaded environment. The linq query above (and all access to it, such as items added/removed to the list, or status changes to the items in the list) are all wrapped in ReaderWriterLockSlim write locks. Again, I have debugged it extensively to make sure there is never anymore than a single thread accessing the list at any time.

What might cause it to stack overflow, as apposed to some possible other errors such as a modification of the list during the query? (again I'm there is only a single thread accessing the list at any one time)

EDIT: as requested the getter and setter code:

public ChangeLogProcessStatus Status { get { return _status; } set { //more that one place can initiate a retry now, so retry count is handled in the property setter if (PreviousStatus <= ChangeLogProcessStatus.Waiting && value >= ChangeLogProcessStatus.RetryWaiting) { this.ChangeLog.Tries++; //If it's retry waiting, remove this last service machine from the //list so it can try it again because it's not an error if (value == ChangeLogProcessStatus.RetryWaiting && _previousServiceMachineIds.Count > 0) { _previousServiceMachineIds.RemoveAt(_previousServiceMachineIds.Count() - 1); } } PreviousStatus = _status; _status = value; } }

LAST EDIT - I've removed the previous examples as the problem did not exist in that code.

It turns out it was in a different part of the application, and it was a very hard to find piece of recursion. It was a coincidence that the stack overflow error was raised duringthe linq query, which as a result was being called 420000+ times recursively.

All answers below were helpfull and on the right path to finding the problem in multi-threaded apps, however the first answer definitly emphasized recursion as the problem which is what it turned out to be (although it wasn't one of the property accessors as seemed obvious).

THanks again


-------------Problems Reply------------

Check the property on the ChangeLogProcess class to make sure that it isn't self-referential. This is, I think, the most likely cause of a stack overflow exception in this case.


private ChangeLogStatus status;
public ChangeLogStatus Status
get { return this.Status; } // instead of this.status
set { this.status = value }

Another possible alternative is in the equality check for status. Have you overridden Equals() for ChangeLogStatus? Check there to make sure you don't have any self-referential code (or at least a way of terminating the recursion).

I've noticed that some collections behave very badly when touched by two threads at the same time.

You are actually allowing more than one thread to touch the collection at the same time. RWLS allows multiple threads to access during read operations and lock on a write operation. So two threads could be reading, i.e. touching, the collection at the same time.

My suggestion would be to change the RWLS to a simple lock() and try to repro the stack overflow.

If this fixes your issue, I'd suggest thinking about moving to 4.0 to take advantage of the concurrent collections. Alternatively, you might want to construct your own thread-safe collection cough reflector cough so you can control this situation better.


Thread Safety

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

A Dictionary<(Of <(TKey, TValue>)>) can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

Emphasis mine.

Category:c# Views:0 Time:2010-02-12

Related post

  • Tracking down a stack overflow error in my LINQ query 2010-04-20

    I've written the following LINQ query: IQueryable<ISOCountry> entries = (from e in competitorRepository.Competitors join c in countries on e.countryID equals c.isoCountryCode where !e.Deleted orderby c.isoCountryCode select new ISOCountry() { i

  • LINQ stack overflow selecting many 2010-11-26

    I have a scenario where I have a table of "batch" and a table of "test" where "test" contains an FK to "batch" and a many tests can belong to a batch. I want to be able to select multiple batches and find all tests that belong to them. I do this by p

  • Without using recursion how can a stack overflow exception be thrown? 2009-10-17

    Without using recursion how can a stack overflow exception be thrown? --------------Solutions------------- If you call enough methods, a stack overflow can occur anytime. Although, if you get stack overflow errors without using recursion, you may wan

  • ASP.NET MVC 2.0 Stack Overflow when loading stored procedure in partial view 2010-03-10

    Please forgive any newbie mistakes. I'm writing a receipt detail lookup tool in MVC 2.0 to pull up receipt details from our POS database. I have the stored procedures written that return the data that I need. I have my sprocs loaded in a repository.

  • LinqKit stack overflow exception using predicate builder 2010-06-08

    I am writing an application in C# using LINQ and LINQKit. I have a very large database table with company registration numbers in it. I want to do a LINQ query which will produce the equivalent SQL: select * from table1 where regno in('123','456') Th

  • What OpenID solution is really used by Stack Overflow? 2011-02-26

    I know there are other questions about this, but they are outdated, and I cannot find the answer that would be reliable anywhere. What is really used by Stack Overflow to autentificate users? The site DotNetOpenAuth claims it is. But to me the most (

  • How do I change the Rx Builder implementation to fix the stack overflow exception? 2011-05-28

    I'm trying to come up with an Rx Builder to use Reactive Extension within the F# Computation Expression syntax. How do I fix it so that it doesnt blow the stack? Like the Seq example below. And is there any plans to provide an implementation of the R

  • Debugging C# - stack overflow exception? 2011-06-06

    Below is my code for a Battleship game. I keep getting the following error: Process terminated due to StackOverflowException. . It keeps pointing to char[,] Grid = new char[10, 10]; How can this be fixed? using System; using System.Collections.Generi

  • Stack overflow with ToArray() 2011-07-14

    I get a stack overflow error from the last line of the below code. I cant see why. Any ideas? var slots = from a in db.AvailableAppointments where a.RequestID == reqId select new DataLayer.DateAndTimeslot { date = a.Date.ToShortDateString(), timeSlot

  • Why am I getting this Stack Overflow exception? 2011-10-24

    I am writing a Tetris clone, which is the largest project I have really done. In implementing the row deletion code, I have begun recieving a stack overflow exception. I think it probably has something to do with my huge collections or horribly ineff

  • Guarding against stack overflows in APM 2012-01-05

    The Good Book says, that when implementing the asynchronous programming model, there is always a possibility of the callback to be called synchronously many times in a row causing stack overflow. The problem is that I can't even come close to reprodu

  • How Does A Stack Overflow Occur and How Do You Prevent It? 2008-08-25

    How does a stack overflow occurs and what are the best ways to make sure it doesn't happen, or ways to prevent one - particularly on web servers, but other examples would be interesting, as well. --------------Solutions------------- Stack A stack, in

  • Which CMS or other framework should I use to develop a Stack Overflow-like site? 2008-08-28

    I want to develop a Q/A site with features substantially similar to Stack Overflow, but targeted to a different niche (electrical/hardware engineering as opposed to software engineering). The code that runs Stack Overflow is not open-source, and from

  • Are tags useful for navigation (on Stack Overflow or otherwise)? 2008-09-17

    I've done some research on using tags from social bookmarking sites for web search, but I'd like to learn more about other ways in which users might use tags for information retrieval. Do you use the tags on sites like Stack Overflow for navigation?

  • Segfault on stack overflow 2008-09-17

    Why does the linux kernel generate a segfault on stack overflow? This can make debugging very awkward when alloca in c or fortran creation of temporary arrays overflows. Surely it mjust be possible for the runtime to produce a more helpful error. ---

  • Stack overflow from .NET code in IIS, but not in Winforms 2008-09-18

    So I have a nasty stack overflow I have been trying to track down / solve for the past 8 hours or so, and I'm at the point where i think i need advice. The details: Interestingly enough this code runs fine when called in the context of our regular wi

  • What is causing a stack overflow? 2008-09-19

    You may think that this is a coincidence that the topic of my question is similar to the name of the forum but I actually got here by googling the term "stack overflow". I use the OPNET network simulator in which I program using C. I think I am havin

  • How can I search through Stack Overflow questions from a script? 2008-10-13

    Given a string of keywords, such as "Python best practices", I would like to obtain the first 10 Stack Overflow questions that contain that keywords, sorted by relevance (?), say from a Python script. My goal is to end up with a list of tuples (title

  • How to detect possible / potential stack overflow problems in a c / c++ program? 2008-10-14

    Is there a standard way to see how much stack space your app has and what the highest watermark for stack usage is during a run? Also in the dreaded case of actual overflow what happens? Does it crash, trigger an exception or signal? Is there a stand

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.151 (s). 11 q(s)