Repository navigation
Description
Describe the bug 🐞
Create two observables that call ToObservableChangeSet with expireAfter and they may end up in deadlock.
The TaskPoolScheduler thread that handles the item expiration can end up with both scheduled expirations nested on the stack holding locks that deadlock the adding threads.
Step to reproduce
Create a console project and add this code to Program.cs
using DynamicData;
using System.Reactive.Linq;
namespace DynamicDataDeadlock;
internal class Program
{
static void Main(string[] args)
{
Observable.Interval(TimeSpan.FromMilliseconds(250))
.Select(x => new Item(x % 100))
.ToObservableChangeSet(x => x.Value, expireAfter: _ => TimeSpan.FromSeconds(1))
.Subscribe(x => Console.WriteLine($"{string.Join(',', x)}"));
Observable.Interval(TimeSpan.FromMilliseconds(250))
.Select(x => new Item2(x % 71))
.ToObservableChangeSet(x => x.Value, expireAfter: _ => TimeSpan.FromSeconds(1))
.Subscribe(x => Console.WriteLine($"{string.Join(',', x)}"));
Console.ReadKey();
}
}
internal class Item
{
public Item(long x) => Value = x;
public long Value { get; }
}
internal class Item2
{
public Item2(long x) => Value = x;
public long Value { get; }
}
Run the program in the debugger and it will eventually stop outputting lines. Pause the program and inspect the parallel threads to observe the deadlock.
Removing the console debug will reach deadlock faster.
Reproduction repository
https://github.com/aguahombre/DynamicDataDeadlock
Expected behavior
The two subscriptions run independently and never deadlock.
Screenshots 🖼️
Here is the parallel threads window showing threads 27772 and 12496 in deadlock.
IDE
Visual Studio 2022
Operating system
Windows 10, Linux Bookworm
Version
17.13.6
Device
PC, RPI4B
DynamicData Version
9.2.1
Additional information ℹ️
No response

Activity
JakenVeina commentedon May 4, 2025
Oh, thaaaaat is distressing.
On my end, the second subscription seems to be a necessity to trigger this deadlock. Can you confirm? These are two completely independent streams, so that suggests there's some kind of
static,ThreadLocalorAsyncLocalstate being shared by separate instances of this operator.JakenVeina commentedon May 5, 2025
Alright, I think I've got a proof-of-concept of the issue. It is indeed a shared-state issue, that shared state being wrapped up inside the shared
ISchedulerthat both operators are implicitly using, viaGlobalConfig.DefaultScheduler(which is aTaskPoolScheduler, by default).The issue stems from the fact that the
TaskPoolSchedulerhas some optimizations built-in, which means it doesn't really do what its name suggests. Instead of always invoking scheduled actions via aTask, pulled from a pool, it will sometimes invoke them on the scheduling thread. I.E. when you make anIScheduler.Schedule()call, the scheduler doesn't just schedule your action and return, it will immediately invoke any actions that were previously-scheduled that are now due, similar to theImmediateScheduler. This is not a behavior I accounted for when building this operator. It was my assumption thatIScheduler.Schedule()would be a "fire-and-forget" operation, so long as you're not usingImmediateSchedulerorCurrentThreadScheduler, which require a single-threaded environment anyway.I proved the concept by inserting a
Task.Run()within the.Schedule()d actions that were deadlocking, to force the scheduler to behave the way I thought it would, and the deadlock disappears. I was able to run continuously for about 10 minutes with this adjustment, whereas the deadlock normally appears in 1-2 seconds, after I reduced the interval and expiration timings to 1ms and 4ms, respectively. Just dropping aTask.Run()in here is definitely not a proper solution, so I'll need to do some more thinking, and probably talk to the RX.NET folks.In the meantime, the other thing I verified is that passing
ThreadPoolScheduler.Instanceas theschedulerparameter of the operator, instead of leaving itnull, is a viable workaround, until we get a proper fix. Same deal as above, I was able to run for several minutes with no deadlock. If you're using this in a severely-hot path, you might get reduced performance with this scheduler, so keep that in mind.Thanks for the extremely-detailed write-up, BTW.
aguahombre commentedon May 6, 2025
Thanks for looking into this so quickly. I had also found that changing the scheduler to
NewThreadScheduler.Defaultseems to avoid the deadlock. I wonder if there has been a recent breaking change inTaskPoolScheduler, as my code hasn't changed in that area recently and I've only just come across the problem.JakenVeina commentedon May 7, 2025
I dug into the blame history within
TaskPoolScheduler, the "problematic" part (usingTaskContinuationOptions.ExecuteSynchronouslywhen creatingTasks for scheduled actions) hasn't changed since its inception. Or, at least, since they migrated all the source to GitHub, they didn't migrate any of the repo history from before that.Looking at our OWN history, there are two changes jointly responsible for introducing this issue.
Version 8.3.93 included #862, which centralized scheduler usage across the entire library into a configurable
GlobalConfig.DefaultSchedulerfield, for consistency. However, this also changed the scheduler that's actually USED as the default from aDefaultSchedulerto aTaskPoolScheduler.DefaultScheduleris actually an entirely different implementation ofISchedulerthat is built to achieve platform-independence through anIConcurrencyAbstractionLayerimplementation that's automatically chosen per-platform. I'd have to spend a lot more time digging to reverse-engineer all the layers of indirection that are involved here, but at a glance, it looks like scheduling is all timer-based instead ofTask-based.Version 8.3.25 included #771, in which the entire
.ToObservableChangeSet()operator was re-written from scratch.Rolling back either of these two changes eliminates the deadlock, so we have a fun little marriage issue. Essentially, #862, which is possibly a defect we should roll back, revealed a probable defect in #771.
So, what you should REALLY do, for now, is use
Scheduler.Defaultrather thanThreadPoolScheduler.InstanceI'll keep digging and figure out what the proper resolution is on our end.
JakenVeina commentedon May 7, 2025
Also going to mention #854 for reference. This is where the decision to change our default scheduler originated.
aguahombre commentedon May 8, 2025
Will this problem also affect the
ExpireAftermethod?JakenVeina commentedon May 9, 2025
Sure looks like it.
Re-wrote .ToObservableChangeSet() operators for both Cache and List, …
Re-wrote .ToObservableChangeSet() operators for both Cache and List, …
github-actions commentedon Jul 3, 2025
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.