Java Forum / General / October 2005
Thread.stop makes thread unabled to be signalled
Benji - 24 Oct 2005 01:42 GMT I'm using Thread.stop() and running into problems. Just to get it out of the way now, yes, I know it's deprecated, no, I can't use inturrupt. I'm writing a student code autograder, so I can't guarentee anything about the student code. This is adversarial programming. =)
The problem is that I can call stop() on a thread, and catch the ThreadDeath that gets generated, and the thread *seems* to be just fine, until I wait on a monitor. The object's monitor is definitely signalled, but the thread never wakes up. It seems that Thread.stop() does more than it says, because there is nothing about the *monitor* state that is corrupted, it is that individual thread. (The other threads in the system can get notified just fine - it's just the thread that was stopped) The API makes it sound like the only bad state the thread gets in to is that an unexpected exception can cause inconsistent state in the program - but the exception isn't even propagating into my code - it's entirely in student code, so at least according to the documentation, nothing should be in a bad state.
Is there any way to get around this, or does Thread.stop() make the thread unusable, and should I just respawn the thread? (I've already done the latter - I was just curious as to what was going on)
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Andrew Thompson - 24 Oct 2005 02:27 GMT > I'm using Thread.stop() and running into problems. Just to get it out of > the way now, yes, I know it's deprecated, no, I can't use inturrupt. > I'm writing a student code autograder, so I can't guarentee anything > about the student code. This is adversarial programming. =) You might 'exec' them in a different JVM, then kill the process. I suspect it would be far more 'complete' than calling Thread.stop().
(Sorry, I have no actual experiences with processes, just going on what I've seen/heard about the group)
Benji - 24 Oct 2005 03:29 GMT > You might 'exec' them in a different JVM, then kill the > process. I suspect it would be far more 'complete' than > calling Thread.stop().
> (Sorry, I have no actual experiences with processes, just > going on what I've seen/heard about the group) Well, I'm getting fine "stop this thread from running" behavior. What I was really hoping that I could do was be able to catch the ThreadDeath before it propagates all the way up the stack, and continue execution without having the thread damaged (as it apparently is)
I suppose it's not a very common occurrence, so it doesn't hurt to spawn another thread in the case that I have to kill off a thread, but the reason it concerned me was that I'm thinking that I may run in to problems later with the way I'm currently doing it. I still have the thread do some work when it catches the ThreadDeath, and I'm afraid that there may be a bug introduced because of whatever this inconsistency is. (So I was really more looking for someone to shed light on why exactly the thread will not receive a notifyAll, and if that's the only strange behavior I would see from a thread that has had stop called on it)
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Andrew Thompson - 24 Oct 2005 03:40 GMT > ..(So I was really more looking for someone to shed light on why > exactly the thread will not receive a notifyAll, and if that's the only > strange behavior I would see from a thread that has had stop called on > it) Cool. I'll hush up and see what the thread gurus have to add..
[ I have done a few projects that involved running other people's code (of untrusted origin - guaranteeing no special interface), so I appreciate that you sometimes need to do 'unconventional/unusual' things in those cases. ;-) ]
Roedy Green - 24 Oct 2005 03:22 GMT >I'm using Thread.stop() and running into problems. Just to get it out of >the way now, yes, I know it's deprecated, no, I can't use inturrupt. see http://mindprod.com/jgloss/thread.html and look for the StoppableThread download.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Benji - 24 Oct 2005 03:37 GMT > see http://mindprod.com/jgloss/thread.html and look for the > StoppableThread download. I'm pretty sure that won't work. The whole reason I need to be stopping the thread is that a student's code may go into an infinite loop. From the description, it's basically just using good design to make stop unnecessary. I can't really use good design, since I'm running arbitrary code that I haven't written.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Roedy Green - 24 Oct 2005 05:07 GMT You might find that cute. I don't.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Benji - 24 Oct 2005 05:34 GMT > You might find that cute. I don't. Well, I didn't find your attribution line either, but you don't see me getting beligerant about it.
If you thought I was actually saying that you were high on whiteboard markers, as opposed to it just being my normal attribution line, I'm sorry that I offended you, and hope that whatever else it is that's bothering you and making you cranky right now goes better for you. :o)
Hope you have a good day.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Benji - 24 Oct 2005 05:35 GMT > You might find that cute. I don't. Well, I didn't find your attribution line cute either, but you don't see me getting beligerant about it.
If you thought I was actually saying that you were high on whiteboard markers, as opposed to it just being my normal attribution line, I'm sorry that I offended you, and hope that whatever else it is that's bothering you and making you cranky right now goes better for you. :o)
Hope you have a good day.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Andrew Thompson - 24 Oct 2005 05:55 GMT >> You might find that cute. I don't. > > Well, I didn't find your attribution line cute either, but you don't > see me getting beligerant... ...LOL!
Are you new to usenet newsgroups?
[ As an aside, Roedy commonly has little knowledge of who wrote what. For that reason, he uses 'wrote or quoted', since people can get quite irate on the occasions he incorrectly attributes something to a person.
I suspect Roedy would really prefer to just quote the text to which he is replying. But other people dislike that very much. ]
Benji - 24 Oct 2005 19:07 GMT > Are you new to usenet newsgroups? Nope. First time in 4 years that I've had anyone get upset about that attribution line. I had a professor pretend he was upset about it just to see how I would react. =) (not *that* was funny...)
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Luc The Perverse - 24 Oct 2005 21:58 GMT >> Are you new to usenet newsgroups? > > Nope. First time in 4 years that I've had anyone get upset about that > attribution line. I had a professor pretend he was upset about it just > to see how I would react. =) (not *that* was funny...) I personally was very offended, which I consider just cause for petitioning for an amendment to The Constitution of the United States of America to illegalize diminuitive usenet attribution lines. Usenet respect ~is~ the foundation of our society; my home bible teacher told me so; and I feel that in order for our society to not crumble, we need to take steps now to end this insult to our very existence..
--
LTP
Roedy Green - 25 Oct 2005 04:38 GMT On Mon, 24 Oct 2005 14:58:39 -0600, "Luc The Perverse" <sll_noSpamlicious_z_XXX_m@cc.usu.edu> wrote, quoted or indirectly quoted someone who said :
>> Nope. First time in 4 years that I've had anyone get upset about that >> attribution line. In my country when you up to a stranger and insult him it means that you want to scuffle. Given the distance limitation, I can't interpret it that way.
It is perhaps a cultural thing. My dad was waiting in a queue in England and lightly brushed the person next him in line with his elbow, something of no importance in Canada. The person turned and in icy tones said, "There is no need to jostle".
Whether Benji considers it offensive is irrelevant. I do, and he is addressing his insult to me, and he still expects me to do him a favour even after I asked him to desist, and he made a point of repeating the insult.
It is terminal stupidity. He gains nothing by his behaviour. It is not as if he were a troll TRYING to annoy others. He is trying to elicit aid from them.
Perhaps it is a habit he picked up from American sitcoms who rely on cheap insult humour because can't afford writers who can think of dialog that is truly funny. On the other paw, there is a saying by the late Ken Keyes.
"You add suffering to the world just as much when you take offense as when you give offense."
So I think my optimal strategy is to refuse to help Benji in future, explain why, and not sweat it.
There is another poster who uses a similarly insulting intro. I asked him to stop. He refused. I dropped the issue, since he is the one giving me information most of the time, not the reverse.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
steve - 25 Oct 2005 10:11 GMT > On Mon, 24 Oct 2005 14:58:39 -0600, "Luc The Perverse" > <sll_noSpamlicious_z_XXX_m@cc.usu.edu> wrote, quoted or indirectly [quoted text clipped - 36 lines] > him to stop. He refused. I dropped the issue, since he is the one > giving me information most of the time, not the reverse.
:-) A "scuffle", is that the polite way to say a "smack on the nose". Well I'm from England and I can say we take Queueing very seriously, any sort of body contact is classed as a "jostle". whereas here in China there is no such thing as a queue, you cannot even wait in line to get OFF a bus ,without some MOFO trying to climb over you. Queues only exist here as enforced structures marked out with Electric Battons.
Steve
Roedy Green - 24 Oct 2005 06:05 GMT >If you thought I was actually saying that you were high on whiteboard >markers, as opposed to it just being my normal attribution line, I'm >sorry that I offended you, and hope that whatever else it is that's >bothering you and making you cranky right now goes better for you. :o) You would not talk to a police officer, your prof, even a grocery clerk that way you little twerp.
You will not get any help from me again unless you trick my killfile filter and I fail to notice.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Benji - 24 Oct 2005 19:38 GMT > You would not talk to a police officer, your prof, even a grocery > clerk that way you little twerp. But, then again, police officers, my professors, and even my grocery clerks have better understandings of social situations, and wouldn't have gotten upset by an attribution line in the first place. :o)
> You will not get any help from me again unless you trick my killfile > filter and I fail to notice. You know, when I was a freshmen in college, and had just discovered what a killfile was, I too thought it was cool to make one last "I'm putting you in my killfile" post before kfing them.
Sorry if I upset you in all of this, but you have to realize that at your age, you should learn a little more grace in how to handle situations that upset you. For example, if you had e-mailed me rather than posting to the newsgroup, I might have taken it a little more seriously - but as it is, your post was clearly intended to get attention and to make you look cool, although I'm afraid that you were mistaken on the last point.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
steve - 24 Oct 2005 22:22 GMT > You might find that cute. I don't. When I was teaching on a government funded project to teach programming to the jobless , drug addicts , and other under performing social groups. White board markers were the highlight of everyones day , including mine. Ahhh, fond memories.
But rodders has a point, that as part of the course, if your students cannot follow a simple task , such as a call out ,then they are not going to be very good programmers in a team. The other solution is to allocate marks , and force them to to extend one of your classes. Then make them implement the exit function , if they do not fit into your framework the program will not run ,so they fail the task.
Seve
Benji - 24 Oct 2005 23:10 GMT > But rodders has a point, that as part of the course, if your students cannot > follow a simple task , such as a call out ,then they are not going to be very [quoted text clipped - 3 lines] > Then make them implement the exit function , if they do not fit into your > framework the program will not run ,so they fail the task. Well, again, I would love to be able to force them to do this, but it's not really that simple. I'm looking for a good technical solution that allows the professor to decide what the policy should be.
My current idea is that instead of calling stop, I should 1) suspend the thread 2) get a stack trace, make sure it's in student code (not system code) 3) if it's in student code, kill it. Otherwise, resume the thread and try again a set number of times. (I doubt that the deadlock situation really applies if all I'm doing is calling suspend and getStackTrace)
As long as it's in student code, I only risk screwing up their state, not system state.
any thoughts?
 Signature Of making better designs there is no end, and much refactoring wearies the body.
steve - 25 Oct 2005 10:03 GMT >> But rodders has a point, that as part of the course, if your students >> cannot [quoted text clipped - 22 lines] > > any thoughts? if you are going to try to persist with this , the only other things I can suggest are: 1. "reflection & "extend" their class then run it in a thread with a timeout. 2. run their program in debug mode in the JVM, then use the pre-provided hooks in the JVM, the same way a debugger written in java does.
But trying to use "stop" or other depreciated methods , is really asking for it. if not now then some time in the future.
Steve
Chris Uppal - 25 Oct 2005 11:12 GMT > My current idea is that instead of calling stop, I should > 1) suspend the thread [quoted text clipped - 3 lines] > situation really applies if all I'm doing is calling suspend and > getStackTrace) [...]
> any thoughts? It sounds as if you are contemplating a system that is a) overcomplex b) unreliable c) less safe than it could be
-- chris
Roedy Green - 24 Oct 2005 05:59 GMT You can insult your friends that way and get away with it, but if you do it to random strangers, you are asking for trouble.
Hint: don't go into a black neighbourhood wearing a "niggers are fags" tee shirt.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Mike Schilling - 24 Oct 2005 06:07 GMT > You can insult your friends that way and get away with it, but if you > do it to random strangers, you are asking for trouble. > > Hint: don't go into a black neighbourhood wearing a "niggers are fags" > tee shirt. Or a redneck gay neighborhood.
Roedy Green - 24 Oct 2005 03:42 GMT >see http://mindprod.com/jgloss/thread.html and look for the >StoppableThread download. tell your students as part of the exercise they must call some stopping method you provide in the innermost loop.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Benji - 24 Oct 2005 04:46 GMT > tell your students as part of the exercise they must call some > stopping method you provide in the innermost loop. well...
If you have a class with 900 students in it, it's a pretty good bet that enough of them are going to disobey anything you tell them to make it annoying. I'm writing an API that would ideally be able to be included in a web submission framework. Wouldn't want to have a hung thread for every annoying student.
plus, this is an intro to CS class...they don't even teach them what methods are until a bit in to the class - it's not going to fly to make the students have correct threading code in their program. I can't even get the professor to let me force the students to conform to enough of an interface to guarentee that I won't have to reload their class every time between runs of their main method.
So, that being said, there's no clean way I can get around this. My options are really either using Thread.stop(), or creating another JVM.
does anyone know if there's a clean way to invoke a child JVM? Like... maybe a way to get a lighter weight solution than invoking a separate JVM...but cleaner than stopping a thread?
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Roedy Green - 24 Oct 2005 06:01 GMT >If you have a class with 900 students in it, it's a pretty good bet that >enough of them are going to disobey anything you tell them to make it >annoying. Of course. But it does not matter. You give them a low grade. Your system can still recover, albeit more awkwardly by killing the entire jvm.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Benji - 24 Oct 2005 19:14 GMT > Of course. But it does not matter. You give them a low grade. Your > system can still recover, albeit more awkwardly by killing the entire > jvm. Ok, well, while I might agree with you, I'm a TA trying to write a system for professors who may or may not agree with that grading policy. I'm not looking for an argument against their grading philosophy, I'm looking for a technical solution to the problem.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Chris Uppal - 24 Oct 2005 08:56 GMT > does anyone know if there's a clean way to invoke a child JVM? Yes.
> Like... > maybe a way to get a lighter weight solution than invoking a separate > JVM...but cleaner than stopping a thread? There isn't any way that isn't inherently as expensive as starting java.exe (or equivalent). Not even if you drop down to the JNI level.
BTW if you were serious about "adversarial programming" (or sufficiently intimidated by the terrors of the unknown) then it might be worth running the students' code in a sandbox of some kind -- if you aren't doing so already...
As to your original question. I don't know the specific answer, but I suggest that Thread.stop() is more dangerous than you seem to think. Think of it as leaving /the entire system/ in an undefined state, since that (in the absence of detailed technical analysis of the actual implementation[*]) is what it does.
([*] The actual implementation, of course, being subtle and complex, and by no means susceptible to "common sense" intuition.)
-- chris
Roedy Green - 24 Oct 2005 03:27 GMT >I'm writing a student code autograder, so I can't guarentee anything >about the student code. This is adversarial programming. =) for this I think you would need two jvms. One jvm just monitors the other to make sure it is still alive and sane and grading yet another student program. The interface could be as simple as a project number now grading on disk. If the student grader JVM dies, it spawns a new one, telling it to pick up AFTER the project was last grading.
Every project gets a fresh thread. If you want to judge speed, things get more complicated to make it fair.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts.
Chris Smith - 24 Oct 2005 17:37 GMT > I'm using Thread.stop() and running into problems. Just to get it out of > the way now, yes, I know it's deprecated, no, I can't use inturrupt. [quoted text clipped - 5 lines] > until I wait on a monitor. The object's monitor is definitely signalled, > but the thread never wakes up. Yep. If you read about why Thread.stop() is deprecated, you'll see that its unavoidable potential to cause deadlocks in native code is one major reason. Ending that thread is not necessarily good enough. You're still taking a big chance.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Benji - 24 Oct 2005 19:17 GMT > Yep. If you read about why Thread.stop() is deprecated, you'll see that > its unavoidable potential to cause deadlocks in native code is one major > reason. Ending that thread is not necessarily good enough. You're > still taking a big chance. Alright. Well, thanks for the reply. *sigh* =)
 Signature Of making better designs there is no end, and much refactoring wearies the body.
pkriens - 25 Oct 2005 10:16 GMT I am not sure I fully understand your problem. This "thread" goes off in all kind of directions (which is not uncommon here) except discussing the problem you raise :-(
Though stop is deprecated, I think the reasons are overly politically correct. If the reasons were valid, then any unchecked exception are mortal. From my experience, this leaves very few systems. One of the good parts of Java is that it does not die when it gets in a bad state. Especially in cases like you discuss where no lifes or great finanical risks are at stake, Thread.stop can have a role. I think SUN, as a litigatious sensitive company, deprecated it (though it has NOT removed it!).
I understand your key problem is that your notification is not waking up the wait. I deduce that you you run the student code in a try/finally clause and notify in the finally clause you so your main thread wakes up? I.e. something like this:
public class Runner extends Thread { Runnable student; static Timer timer = new Timer();
Runner(Runnable student) { this.student = student; }
public void run() { try { student.run(); } finally { synchronized (this) { System.out.println("Notifying"); notifyAll(); } } }
public static void main(String args[]) throws Exception { Runnable student = new BadStudent();
final Runner runner = new Runner(student); TimerTask task = new TimerTask() { public void run() { System.out.println("Timed out, stopping"); runner.stop(); } }; timer.schedule(task, 5000); System.out.println("Starting runner"); runner.start(); synchronized (runner) { runner.wait(); System.out.println("Received notification"); } task.cancel(); } }
class BadStudent implements Runnable { double sum; public void run() { System.out.println("Starting bad student"); try { double start=1; while (true ) { start /= 2; sum += start; } } finally { System.out.println("Leaving bad student"); } } } Running it: Starting runner Starting bad student Timed out, stopping Leaving bad student Notifying Received notification
I tried this on my system and it works ok. Can you elucidate what you do different? Show some code? Did I misunderstand you?
Running the code in a Threadgrouped Thread, its own classloader, and then using stop for this example should give you a system that can handle most of the student's abuses. You could even deny catching ThreadDeath which is an easy trick.
An alternative is to use Aspect Oriented Programming. You could write aspects that insert checks in the students code to quit in every method and loop (I am not sure if you write an advice for a loop though). You could also use ASM and just insert a check before every jump I guess.
Kind regards,
Peter Kriens OSGi Evangelist www.osgi.org and www.aqute.biz
> I'm using Thread.stop() and running into problems. Just to get it out of > the way now, yes, I know it's deprecated, no, I can't use inturrupt. [quoted text clipped - 21 lines] > Of making better designs there is no end, > and much refactoring wearies the body. Benji - 25 Oct 2005 12:04 GMT > <snip> Well, darn. I typed up a fairly long response to your post, and vim went and barfed on itself. Anyway, I was just going to say that I found the problem from trying to modify your code. The thread that is stopped throws an InterruptedException if it tries to wait(). However, that only gets thrown once, so after that I think I can carry on as usual.
As for your suggestions about threadgrouping, classloaders, and denying catches, I've done the last two - but of what importance is threadgrouping?
Also, I think your comment about injecting checks before jumps and method calls is a darn good idea, and I will definitely look in to doing that instead of my previous plan.
Thanks for your time. =)
 Signature Of making better designs there is no end, and much refactoring wearies the body.
pkriens - 28 Oct 2005 09:49 GMT Threadgrouping is necessary so you can catch any student that tries to spawn other threads. Any thread in a thread group can only create threads in that group so you can kill ALL their threads.
Kind regards,
Peter Kriens OSGi Evangelist www.osgi.org and www.aqute.biz
Thomas Hawtin - 25 Oct 2005 16:28 GMT > Though stop is deprecated, I think the reasons are overly politically > correct. If the reasons were valid, then any unchecked exception are [quoted text clipped - 4 lines] > litigatious sensitive company, deprecated it (though it has NOT removed > it!). Quite often unchecked exceptions don't result in a stable state. You can claim this is a code quality issue, but there are a couple cases of exception-unsafety in the collection classes. If Joshua can't ensure it is correct, then there isn't much hope for the rest of us.
Thread.stop is worse than almost all other unchecked throwings in that it is an asynchronous throw. It can occur where really no exception should be possible. In some situations it is very handy to be able to copy over multiple references, say, and it either succeeding fully or failing without side-effect.
Other than virtual machine bugs, the only other situation I can think of where this happens is involved with NIO buffers. Under Linux it just seg faults instead.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
pkriens - 28 Oct 2005 10:02 GMT I agree that the sync nature of stop can cause havoc. However, I think this is a quite normal state for virtually all larger applications I have seen. I think it is a fallacy that type safe applications are safe; there is just too much that can go wrong in software. I have been working in this industry for over 25 years working for many large and small companies as consultant, often having to do reviews of large systems. It is amazing how bad most software is while it still works for the user.
As always, there are no easy rules; it depends on the risk you run when it fails. if you do the brakes of a car, do not use java and if you are dumb enough to do this stop() is not going to make a big difference overall because you are already screwed. If you write an application evaluator like Benji, the risk of something bad happening is ok, nobody will get harmed. Note that there is already something bad happening because the application does not quit in time, which is part of its contract.
Concluding: The concerns of stop() are valid for application areas where you better know the consequences of a myriad of details. This is not the area where Java shines.
Peter Kriens OSGi Evangelist www.osgi.org and www.aqute.biz
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|