473,698 Members | 2,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread.ResetAbo rt leaving the thread in a broken state

Tum
Hi,

I've been working on DotGNU trying to implement the correct semantics for
monitors, thread.abort (etc) in the runtime.

Russell Stuart posted an example a few days ago which we discovered would
let two locked blocks to execute simulatenously.

As I moved onto implemeting Abort, I wondered how Thread.Abort() could
possibly abort a thread but yet maintain monitor consistancy. For example,
if a thread is aborted while it is waiting on a monitor and calls
ResetAbort, the thread will reenter a synchronized block. It wouldn't make
sense for the aborted thread to own the monitor because it never got pulsed
or released by the thread that really owns the monitor. Naturally, if the
aborted thread doesn't own the monitor then it can't call Monitor.Exit on
the monitor right? I was suprised to find out that the aborted thread could
call Monitor.Exit on the monitor -- even though it didn't own it. My first
thought was that the runtime allowed the abort thread to have a "pretend"
count on the monitor so that it could call Monitor.Exit on the monitor the
appropriate amount of times it needed as the ThreadAbortExce ption got
propagated upwards. After a bit of testing it turns out that if a thread is
aborted the runtime simply ignores calls to Monitor.Exit and even worse, it
continues to ignore calls to Monitor.Exit even after you call
Thread.ResetAbo rt.

Here's some example code:

using System;

using System.Threadin g;

public class Test2

{

Thread thread1, thread2;
public void Run1()

{

Thread.Sleep(20 00);
lock (this)

{

Monitor.Pulse(t his);

thread2.Abort() ;

}

Console.WriteLi ne("1 Released Lock");

}
public void Run2()

{

lock(this)

{

try

{

Monitor.Wait(th is);

}

catch (ThreadAbortExc eption)

{

Thread.ResetAbo rt();

}
Monitor.Exit(th is);

Monitor.Exit(ty peof(string));

Monitor.Exit(ty peof(string));

Monitor.Exit(th is);

Monitor.Exit(th is);

Monitor.Exit(th is);

}

}

public void Go()

{

thread1 = new Thread(new ThreadStart(Run 1));

thread2 = new Thread(new ThreadStart(Run 2));
thread1.Start() ;

thread2.Start() ;

}
public static void Main()

{

new Test2().Go();

}

}

It should throw Synchronization LockExceptions but doesn't. In fact, it'll
happily let you call Monitor.Exit as many times as you like on *ANY* objects
you like. EWWWWW.

Hands up anyone who thinks Thread.Abort is a bad idea.

^Tum

Nov 22 '05 #1
0 1648

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
451
by: Tum | last post by:
Hi, I've been working on DotGNU trying to implement the correct semantics for monitors, thread.abort (etc) in the runtime. Russell Stuart posted an example a few days ago which we discovered would let two locked blocks to execute simulatenously. As I moved onto implemeting Abort, I wondered how Thread.Abort() could possibly abort a thread but yet maintain monitor consistancy. For example,
11
2212
by: Bob Rock | last post by:
Hello, coming from win32 API I recall an ExitThread() call to gently terminate a thread from inside the same thread .... but now all I can see is an Abort call which seems to me a wrapper on the TerminateThread() Win32 API which is a brutal way to end a thread. Is there any other more gentle way to close a thread??? Bob Rock
2
2376
by: cottonviking | last post by:
Greetings, all! I've been pondering the pitfalls of aborting a secondary thread in a service app I'm writing (VB, fx v1.1). Everything I've read so far pretty much dissuades one from aborting one thread from another, and I'm almost at the point of acquiescing but curiosity leads me on. Below is a procedure template that I wonder might guarantee behavior during an abort, and I'd like to get some feedback in case it's just plain nonsense.
18
5847
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this component becomes instabile, throwing a Windows like error (access violation on 0x00000002), not an framework exception. The component and all of its subcomponents are 100% managed code. What could go wrong with Thread.Abort()? Thanks for any hints.
3
6167
by: Raj Wall | last post by:
Hi, I have an application that uses a number of sub-threads. What is the best way to do some processing in each thread when the main application is shut down? Is the ThreadAbortException thrown automatically for each thread? Or is there some other event or exception automatically thrown that the thread can "grab" as it is shut down?
1
1535
by: Kingsley | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I've been having an intermittent problem with urllib. With an interval of around 15 minutes (i.e.: this is run every 15m) this code runs fine for about 1-2 weeks, but then gets it's knickers in a twist, and never seems to return, nor except.
6
1418
by: Robert Speck | last post by:
Hi there, Can anyone shed anymore light on why "Thread.Suspend()" has been deprecated by MSFT beyond what MSDN says about it. I'm not sure if I quite appreciate the various pitfalls they discuss but using it under certain circumstances still seems reasonable. For instance, I want to display a small modal dialog with a "Cancel" button which allows the user to abort a background thread. If the user clicks this button, I then want to prompt...
5
11223
by: redear | last post by:
Is there a way to immediately terminate a BackgroundWorker thread? My problem is that the BackgroundWorker starts with a call to My.Computer.FileSystem.GetFiles that can run for a very long time if it is pointing to a directory tree with many files. If the user requests cancellation during this time, the main thread can call CancelAsync and can post a "Cancellation Pending" message to the user, but the BackgroundWorker cannot respond to...
0
1310
by: anoop23 | last post by:
Hi, Im running a timer to Unzip a zip file using a 'ICSharpCode.SharpZipLib.dll'. It works well when I block the main thread indefinitely by 'thread.sleep(-1)'. But gets an ThreadAbort Exception if i run the timer without thread blocking. How can i avoid this error?Im not using thread.abort anywhere in my code.My code to unzip file is taking only 6 sec to complete. Below im showing the code.
20
5074
by: =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post by:
John Dohn wrote: When I do this, I put a special value in the queue (like None) and in the worker thread, check for the special value and exit if found. Threads can also be marked as "daemon threads" (see docs for threading.Thread objects). This will make the application terminate if only "daemon threads" are left. So best would probably be soemthing like
0
9026
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2328
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.