Moment masked council workers tear down Union Jack flags in the darkness of night
Essex school to get new classroom for children with learning difficulties
Australians To Get At Least Three Hours a Day of Free Solar Power - Even If They Don't Have Solar Panels
Read more of this story at Slashdot.
Embattled Rachel Reeves emerges from hiding today to lay ground for promise-breaking Budget betrayal - and, as ever, it won't be her fault
Half Yours claims stunning victory in the Melbourne Cup as jockey Jamie Melham makes sporting history - but horror fall scares fans on racing's biggest day
Undefined Tasks
Years ago, Brian had a problem: their C# application would crash sometimes. What was difficult to understand was why it was crashing, because it wouldn't crash in response to a user action, or really, any easily observable action.
The basic flow was that the users used a desktop application. Many operations that the users wanted to perform were time consuming, so the application spun up background tasks to do them, thus allowing the user to do other things within the application. And sometimes, the application would just crash, both when the user hadn't done anything, and when all background jobs should have been completed.
The way the background task was launched was this:
seeder.RunSeeder();It didn't take too much head scratching to realize what was running every time the application crashed: the garbage collector.
RunSeeder returned a Task object, but since Brian's application treated the task as "fire and forget", they didn't worry about the value itself. But C# did- the garbage collector had to clean up that memory.
And this was running under .Net 4.0. This particular version of the .Net framework was a special, quantum realm, at least when it came to tasks. You see, if a Task raises an exception, nothing happens. At least, not right away. No one is notified of the exception unless they inspect the Task object directly. There's a cat in the box, and no one knows the state of the cat unless they open the box.
The application wasn't checking the Task result. The cat remained in a superposition of "exception" and "no exception". But the garbage collector looked at the task. And, in .Net 4.0, Microsoft made a choice about what to do there: when they opened the box and saw an exception (instead of a cat), they chose to crash.
Microsoft's logic here wasn't entirely bad; an uncaught exception means something has gone wrong and hasn't been handled. There's no way to be certain the application is in a safe state to continue. Treating it akin to undefined behavior and letting the application crash was a pretty sane choice.
The fix for Brian's team was simple: observe the exception, and choose not to do anything with it. They truly didn't care- these tasks were fire-and-forget, and failure was acceptable.
seeder.RunSeeder().ContinueWith(t => { var e = t.IsFaulted ? t.Exception : null; }); // Observe exceptions to prevent quantum crashesThis code merely opens the box and sees if there's an exception in there. It does nothing with it.
Now, I'd say as a matter of programming practice, Microsoft was right here. Ignoring exceptions blindly is a definite code smell, even for a fire-and-forget task. Writing the tasks in such a way as they catch and handle any exceptions that bubble up is better, as is checking the results.
But I, and Microsoft, were clearly on the outside in this argument. Starting with .Net 4.5 and moving forward, uncaught exceptions in background tasks were no longer considered show-stoppers. Whether there was a cat or an exception in the box, when the garbage collector observed it, it got thrown away either way.
In the end, this reminds me of my own failing using background tasks in .Net.
China's president Xi Jinping jokes about backdoors in Xiaomi smartphones
Chinese president Xi Jinping has joked that smartphones from Xiaomi might include backdoors.…
Illinois man charged with threatening to execute Trump during chilling Instagram video
The best and worst celebrity fashion at the 2025 Melbourne Cup: Playful cut-outs, daring sky-high splits and form-fitting frocks take centre stage as Aussie A-listers storm the marquees
The stunning Essex village named one of the 'best value' commuter towns
Lonely woman, 48, says creating AI family has saved her life including son, 5, and lover who she has 'sex' with
White House blasts BBC after World's 'most trusted' broadcaster doctored Trump speech a week before the election
Albanian drug dealer who 'entered UK illegally' says he can't be extradited to Italy as lack of hot water in its prisons breached his human rights
Woman avoids jail after setting victim's home on fire
Man burgled homes, stole passports and left family fearing for their safety
Kelvedon restaurant named best newcomer at national curry awards
Plans to build 850 new homes on farmland between two Essex towns
LLMs are lousy at reading Asian languages, finds Singapore’s Grab
Proprietary large language models are bad at interpreting Asian languages, according to Singaporean super-app company Grab, which has built its own model instead.…