Jump to page: 1 2
Thread overview
What reasons are known a thread stops suddenly?
Feb 04, 2016
tcak
Feb 04, 2016
ikod
Feb 04, 2016
Ali Çehreli
Feb 05, 2016
tcak
Feb 05, 2016
tcak
Feb 05, 2016
Daniel Kozak
Feb 05, 2016
Daniel Kozak
Feb 05, 2016
tcak
Feb 05, 2016
Ali Çehreli
Feb 05, 2016
Kagamin
Feb 05, 2016
Kagamin
February 04, 2016
I have implemented a standalone HTTP server. So everything is in single executable. Requests come, for responding a new thread is started, etc.

To listen new socket connections, and socket events, a single thread is used (Call this event listener thread).

Everything works perfectly. Firefox asks for page, all HTML, JS, CSS, Image requests come and responded properly.

Now, when I start the executable, instead of calling the page on Firefox by pressing F5 for refresh, if I press Ctrl+Shift+F5, Firefox asks for same requests as always do, but that event listener thread suddenly stops.

You might say that is a programming error of mine, but problem is as follows:

void threadFunc(){
	scope(exit){
		writeln("Leaving 2: ", stopRequested);
	}


	while( !stopRequested ){
/* THERE IS NO "RETURN" HERE AT ALL */
	}

	writeln("Leaving 1: ", stopRequested);
}



While loop is running, suddenly "Leaving 2: false" is seen. Checked with exception, but there is nothing. GDB doesn't show any error. There is no "Leaving 1: .." message at all.

Is there any known reason for a thread to suddenly stop like this?
February 04, 2016
On Thursday, 4 February 2016 at 20:25:27 UTC, tcak wrote:

> Is there any known reason for a thread to suddenly stop like this?

Your listener object can be GC-ed. Check if you have any live ref to it.
February 04, 2016
On 02/04/2016 12:25 PM, tcak wrote:

> void threadFunc(){
>      scope(exit){
>          writeln("Leaving 2: ", stopRequested);
>      }
>
>
>      while( !stopRequested ){
> /* THERE IS NO "RETURN" HERE AT ALL */
>      }
>
>      writeln("Leaving 1: ", stopRequested);
> }
>
>
>
> While loop is running, suddenly "Leaving 2: false" is seen.

That would happen when there is an exception.

> Checked with
> exception, but there is nothing.

If a thread is terminated with an exception, its stack is unwound and unlike the main thread, the program will not terminate. I think this is due to an exception.

> GDB doesn't show any error.

I think putting a break point at exception construction would be helpful but it will be simpler to put a try-catch block that covers the entire body of threadFunc().

> There is no
> "Leaving 1: .." message at all.
>
> Is there any known reason for a thread to suddenly stop like this?

I am still betting on an exception. :)

Ali

February 05, 2016
On Thursday, 4 February 2016 at 22:27:31 UTC, Ali Çehreli wrote:
> On 02/04/2016 12:25 PM, tcak wrote:
>
> > void threadFunc(){
> >      scope(exit){
> >          writeln("Leaving 2: ", stopRequested);
> >      }
> >
> >
> >      while( !stopRequested ){
> > /* THERE IS NO "RETURN" HERE AT ALL */
> >      }
> >
> >      writeln("Leaving 1: ", stopRequested);
> > }
> >
> >
> >
> > While loop is running, suddenly "Leaving 2: false" is seen.
>
> That would happen when there is an exception.
>
> > Checked with
> > exception, but there is nothing.
>
> If a thread is terminated with an exception, its stack is unwound and unlike the main thread, the program will not terminate. I think this is due to an exception.
>
> > GDB doesn't show any error.
>
> I think putting a break point at exception construction would be helpful but it will be simpler to put a try-catch block that covers the entire body of threadFunc().
>
> > There is no
> > "Leaving 1: .." message at all.
> >
> > Is there any known reason for a thread to suddenly stop like
> this?
>
> I am still betting on an exception. :)
>
> Ali

Yup, it is exception it seems like, but with a weird result. Check the new codes:

void threadFunc(){
	scope(exit){
		writeln("Leaving 2: ", stopRequested);
	}

	scope(failure){
		writeln("Failure");
	}

	try{
		while( !stopRequested ){

		}

		writeln("Leaving 1: ", stopRequested);
	}
	catch( Exception ex ){
		writeln("Caught the exception");
	}
}

Now, the thread stops with:

Failure
Leaving 2: false


There is no "Caught the exception". And believe me other then the codes inside while loop, main structure as seen in the above code.

By testing many times, I understood that the problem occurs when too many requests are received suddenly (by pressing F5 many times again and again produces the exception).

But the question is why try-catch is not able to catch it, and just scope(failure) can?
February 05, 2016
On Friday, 5 February 2016 at 03:47:40 UTC, tcak wrote:
> On Thursday, 4 February 2016 at 22:27:31 UTC, Ali Çehreli wrote:
>> On 02/04/2016 12:25 PM, tcak wrote:
>>
>> > [...]
>>
>> That would happen when there is an exception.
>>
>> > [...]
>>
>> If a thread is terminated with an exception, its stack is unwound and unlike the main thread, the program will not terminate. I think this is due to an exception.
>>
>> > [...]
>>
>> I think putting a break point at exception construction would be helpful but it will be simpler to put a try-catch block that covers the entire body of threadFunc().
>>
>> > [...]
>> this?
>>
>> I am still betting on an exception. :)
>>
>> Ali
>
> Yup, it is exception it seems like, but with a weird result. Check the new codes:
>
> void threadFunc(){
> 	scope(exit){
> 		writeln("Leaving 2: ", stopRequested);
> 	}
>
> 	scope(failure){
> 		writeln("Failure");
> 	}
>
> 	try{
> 		while( !stopRequested ){
>
> 		}
>
> 		writeln("Leaving 1: ", stopRequested);
> 	}
> 	catch( Exception ex ){
> 		writeln("Caught the exception");
> 	}
> }
>
> Now, the thread stops with:
>
> Failure
> Leaving 2: false
>
>
> There is no "Caught the exception". And believe me other then the codes inside while loop, main structure as seen in the above code.
>
> By testing many times, I understood that the problem occurs when too many requests are received suddenly (by pressing F5 many times again and again produces the exception).
>
> But the question is why try-catch is not able to catch it, and just scope(failure) can?

Okay. The cause of problem has been solved with good-old writeln("DEBUG"); method :)

Cause is trying to access outside of array (e.g. array[$]). But I didn't like that fact that scope(failure) is called for that properly, but no other error was seen.

Environment: MonoDevelop, Linux x64, DMD 2.070, MonoD, running in Debug mode.
February 05, 2016
V Fri, 05 Feb 2016 05:48:27 +0000
tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
napsáno:

> On Friday, 5 February 2016 at 03:47:40 UTC, tcak wrote:
> > On Thursday, 4 February 2016 at 22:27:31 UTC, Ali Çehreli wrote:
> >> On 02/04/2016 12:25 PM, tcak wrote:
> >> 
> >> > [...]
> >>
> >> That would happen when there is an exception.
> >> 
> >> > [...]
> >>
> >> If a thread is terminated with an exception, its stack is
> >> unwound and unlike the main thread, the program will not
> >> terminate. I think this is due to an exception.
> >> 
> >> > [...]
> >>
> >> I think putting a break point at exception construction would
> >> be helpful but it will be simpler to put a try-catch block
> >> that covers the entire body of threadFunc().
> >> 
> >> > [...]
> >> this?
> >>
> >> I am still betting on an exception. :)
> >>
> >> Ali
> >
> > Yup, it is exception it seems like, but with a weird result. Check the new codes:
> >
> > void threadFunc(){
> > 	scope(exit){
> > 		writeln("Leaving 2: ", stopRequested);
> > 	}
> >
> > 	scope(failure){
> > 		writeln("Failure");
> > 	}
> >
> > 	try{
> > 		while( !stopRequested ){
> >
> > 		}
> >
> > 		writeln("Leaving 1: ", stopRequested);
> > 	}
> > 	catch( Exception ex ){
> > 		writeln("Caught the exception");
> > 	}
> > }
> >
> > Now, the thread stops with:
> >
> > Failure
> > Leaving 2: false
> >
> >
> > There is no "Caught the exception". And believe me other then the codes inside while loop, main structure as seen in the above code.
> >
> > By testing many times, I understood that the problem occurs when too many requests are received suddenly (by pressing F5 many times again and again produces the exception).
> >
> > But the question is why try-catch is not able to catch it, and just scope(failure) can?
> 
> Okay. The cause of problem has been solved with good-old writeln("DEBUG"); method :)
> 
> Cause is trying to access outside of array (e.g. array[$]). But I didn't like that fact that scope(failure) is called for that properly, but no other error was seen.
> 
> Environment: MonoDevelop, Linux x64, DMD 2.070, MonoD, running in Debug mode.

This is wierd, IMHO it is a bug.

February 05, 2016
V Fri, 05 Feb 2016 03:47:40 +0000
tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
napsáno:

> On Thursday, 4 February 2016 at 22:27:31 UTC, Ali Çehreli wrote:
> > On 02/04/2016 12:25 PM, tcak wrote:
> > 
> > > void threadFunc(){
> > >      scope(exit){
> > >          writeln("Leaving 2: ", stopRequested);
> > >      }
> > >
> > >
> > >      while( !stopRequested ){
> > > /* THERE IS NO "RETURN" HERE AT ALL */
> > >      }
> > >
> > >      writeln("Leaving 1: ", stopRequested);
> > > }
> > >
> > >
> > >
> > > While loop is running, suddenly "Leaving 2: false" is seen.
> >
> > That would happen when there is an exception.
> > 
> > > Checked with
> > > exception, but there is nothing.
> >
> > If a thread is terminated with an exception, its stack is
> > unwound and unlike the main thread, the program will not
> > terminate. I think this is due to an exception.
> > 
> > > GDB doesn't show any error.
> >
> > I think putting a break point at exception construction would
> > be helpful but it will be simpler to put a try-catch block that
> > covers the entire body of threadFunc().
> > 
> > > There is no
> > > "Leaving 1: .." message at all.
> > >
> > > Is there any known reason for a thread to suddenly stop like
> > this?
> >
> > I am still betting on an exception. :)
> >
> > Ali
> 
> Yup, it is exception it seems like, but with a weird result. Check the new codes:
> 
> void threadFunc(){
> 	scope(exit){
> 		writeln("Leaving 2: ", stopRequested);
> 	}
> 
> 	scope(failure){
> 		writeln("Failure");
> 	}
> 
> 	try{
> 		while( !stopRequested ){
> 
> 		}
> 
> 		writeln("Leaving 1: ", stopRequested);
> 	}
> 	catch( Exception ex ){
> 		writeln("Caught the exception");
> 	}
> }
> 
> Now, the thread stops with:
> 
> Failure
> Leaving 2: false
> 
> 
> There is no "Caught the exception". And believe me other then the codes inside while loop, main structure as seen in the above code.
> 
> By testing many times, I understood that the problem occurs when too many requests are received suddenly (by pressing F5 many times again and again produces the exception).
> 
> But the question is why try-catch is not able to catch it, and just scope(failure) can?

Did you try catch Throwable instead of Exception?

February 05, 2016
On Friday, 5 February 2016 at 06:23:09 UTC, Daniel Kozak wrote:
> V Fri, 05 Feb 2016 03:47:40 +0000
> tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
> napsáno:
>
>> [...]
>
> Did you try catch Throwable instead of Exception?

Undid the fix, and wrapped the problem causing function call with try-catch-Throwable, it is caught now. I always used Exception before, thinking that it was the base of all exceptions.
February 04, 2016
On 02/04/2016 10:41 PM, tcak wrote:
> On Friday, 5 February 2016 at 06:23:09 UTC, Daniel Kozak wrote:
>> V Fri, 05 Feb 2016 03:47:40 +0000
>> tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
>> napsáno:
>>
>>> [...]
>>
>> Did you try catch Throwable instead of Exception?

I was about to write same thing. :)

> Undid the fix, and wrapped the problem causing function call with
> try-catch-Throwable, it is caught now.

However, it is not recommended to catch Throwable (nor Error).

> I always used Exception before, thinking that it was the base of all
> exceptions.

Exception is topmost exception that makes sense to catch because it represents recoverable situations. Error on the other hand is for irrecoverable errors representing situations that may be so buggy that even try-catch may not work properly.

     Throwable
      /     \
Exception   Error

Imagine that the out-of-bounds error was due to some memory corruption. If so, all bets are off... We don't know what state the program is in. If we catch Throwable (or Error) and continue operating, we may produce completely wrong results.

Of course, in this case I think you are just logging that the thread is exiting. (Even then, in theory, the attempt to log can format the cloud data center. :p)

Ali

February 05, 2016
On 2/5/16 2:59 AM, Ali Çehreli wrote:
> On 02/04/2016 10:41 PM, tcak wrote:
>  > On Friday, 5 February 2016 at 06:23:09 UTC, Daniel Kozak wrote:
>  >> V Fri, 05 Feb 2016 03:47:40 +0000
>  >> tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
>  >> napsáno:
>  >>
>  >>> [...]
>  >>
>  >> Did you try catch Throwable instead of Exception?
>
> I was about to write same thing. :)
>
>  > Undid the fix, and wrapped the problem causing function call with
>  > try-catch-Throwable, it is caught now.
>
> However, it is not recommended to catch Throwable (nor Error).

Hm.. I wonder. If Error shouldn't be caught, shouldn't the program terminate?

-Steve
« First   ‹ Prev
1 2