Article 4CVHF CodeSOD: It Just Won't Stop

CodeSOD: It Just Won't Stop

by
Remy Porter
from The Daily WTF on (#4CVHF)

Kara's ongoing quest to tame her codebase continues. First it was serialization, then more strings, then some serial communication.

Today, it's threads.

The original developer was a physicist and an extremely smart person. They never learned too much about databases, but they knew their way around XML, so they had a simple solution to storing data: any time you wanted to save something, rewrite an entire XML file onto disk.

Now, in this case, the "something" being saved is data collected from a piece of scientific equipment. That data is collected by sending commands to the device, changing its state, and asking it for information about readings in that new state.

Since this is a complex, long-running process, the developer used threads. Since it's complex and long running, it needs to be stoppable. And this is how they chose to stop it:

 System.Threading.Thread RunThread; enum StopReason { Completed, Aborted, Error } void StopRun(StopReason why) { try { if (RunThread != null) { RunThread.Abort(); } } catch (ThreadAbortException ex) { } finally { if (Thread.CurrentThread != RunThread) { RunThread.Join(); } OnRunStopped(why); } }

You'll note the catch block in there, the catch with absolutely no body. Fun fact: C# lets you omit the catch and do a simple try/finally pair.

Of course, the exception only is an issue because they're calling Abort. In .NET, an aborted thread has no chance to do any cleanup or shutdown. It just ends.

If you were, say, a piece of software that was interacting with a scientific instrument and writing data directly to an XML file, aborting the thread could leave both the instrument and the XML file in unknown, incoherent states.

Which is exactly what happened. Kara changed the stop behavior to actually stop the thread, gracefully.

raygun50.png [Advertisement] Forget logs. Next time you're struggling to replicate error, crash and performance issues in your apps - Think Raygun! Installs in minutes. Learn more. TheDailyWtf?d=yIl2AUoC8zAugC_Hhdzok4
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments