The Shared State Monster

January 21, 2025    Development .Net F#

The Shared State Monster

The Shared State Monster got me again.

Microsoft CoPilot generate an image of somewhat friendly but also scary monster that holds on to shared state (as in computer programs)

I had a .AddTransient class with a singleton/lazy loaded property called Updater. I was thinking that class got re-created each time so I added a lazy property similar to this

public int UserId { get { return _queue?.UserId ?? 0; } }
private Queue _queue;

private async Task<Queue> GetQueue(int queueId)
{
    if (_queue != null) return _queue;
    _queue = await _queueFacade.GetQueueById(queueId);
    return _queue;
}

🐛 The bug report said the wrong user id was getting sent to the queue in later code. Hmmmm…. It works while debugging…. oh wait! it has the wrong number the second time!!

The Issue

The Updater class was injected into the BackgroundService, that is only created on startup. Thus only one time creation of the other class. Thus, the _queue property wasn’t changed.

Moral of the story

The transient class should just be functions (it is now) with the data passed in.

Functional Programming

This is what Functional Programming is teaching me, but I forgot that message. Hopefully I’ll remember next time.

It reminds me of Gene Kim’s “who filled my cup?!?” in his introduction to Fabulous Fortunes, Fewer Failures, and Faster Fixes from Functional Fundamentals - Scott Havens

Who filled my cup!?!



Watch the Story for Good News
I gladly accept BTC Lightning Network tips at [email protected]

Please consider using Brave and adding me to your BAT payment ledger. Then you won't have to see ads! (when I get to $100 in Google Ads for a payout (I'm at $95.73!), I pledge to turn off ads)

Use Brave

Also check out my Resources Page for referrals that would help me.


Swan logo
Use Swan Bitcoin to onramp with low fees and automatic daily cost averaging and get $10 in BTC when you sign up.