www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What reasons are known a thread stops suddenly?

reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
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?
Feb 04 2016
next sibling parent ikod <geller.garry gmail.com> writes:
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.
Feb 04 2016
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Feb 04 2016
parent reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
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?
Feb 04 2016
next sibling parent reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
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.
Feb 04 2016
parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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.
Feb 04 2016
prev sibling parent reply Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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?
Feb 04 2016
parent reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
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.
Feb 04 2016
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Feb 04 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Feb 05 2016
parent reply Kagamin <spam here.lot> writes:
https://dlang.org/phobos/core_thread.html#.Thread.join
Feb 05 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/5/16 12:06 PM, Kagamin wrote:
 https://dlang.org/phobos/core_thread.html#.Thread.join
Yeah, but in the meantime, the thread state is not cleaned up, and it's in the same memory space as everywhere else. I don't see why if you have an out-of-bounds error in the main thread, it should kill the program, but if you have one in another thread, it should go on working, possibly with a corrupt state. At least, I think you should be able to opt-in to this behavior. -Steve
Feb 05 2016
parent Kagamin <spam here.lot> writes:
Yep, munching an Error by default is pretty nasty.
Feb 05 2016