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
Thanks