473,568 Members | 2,784 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 1612

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...
11
2201
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
2350
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...
18
5842
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...
3
6157
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
1524
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...
5
11191
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...
0
1305
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...
20
5046
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...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8117
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...
1
5498
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...
0
5217
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...
0
3651
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...
1
2101
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
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.