2 Edited for clarity
source | link

According to the documentation, TimeConstrainedTimeConstrained generates an interrupt which interrupts the computation. This interrupt is treated just like an abort, at least in the sense that it respects AbortProtectAbortProtect.

I'd likeI want to intercept this abort/interrupt and write asee the contents of the stack trace to a log file, so that I can determine whether or notat the time out resultedthe abort is generated, before the aborted evaluation is removed from a bugthe stack. I tried this:

changeInterrupt[]changeAbort[] :=
 (
  Unprotect[Interrupt];Unprotect[Abort];
  testInterrupttestAbort = True;
  Interrupt[args___]Abort[args___] :=
   (
     Print["This is where I wouldPrint[Stack[_]]; write(* myin stackreal tracelife, to a log file."]; *)
     Block[{testInterrupttestAbort = False}, Interrupt[args]]Abort[args]]
   ) /; testInterrupttestAbort
  Protect[Interrupt];Protect[Abort];
 )

This works for aborts that I generate myself. For example,

While[True, Abort[]]
TimeConstrained[changeInterrupt[];

aborts and prints the following stack trace:

(* 
{While[True,Abort[]],Print[Stack[_]];Block[{testAbort=False},Abort[]],Print[Stack[_]]}
*)

Along (spiritually) similar lines, I can print the the stack at the time a message is displayed with this code:

Internal`AddHandler["Message", Print[Stack[_]]&];

Getting back to TimeConstrained, neither of the following give me a stack trace:

changeAbort[];TimeConstrained[While[True, Null], 1]
changeInterrupt[];TimeConstrained[changeAbort[]; TimeConstrained[While[TrueWhile[True, Null], 1]

and also the same thing but withThe last line was written in case InterruptTimeConstrained replaced withwas implemented by temporarily redefining Abort. I get the print statement when I generate an abort/interruptUnfortunately, but not when either of the TimeConstrained invokations terminateit doesn't work.

Any ideasIs there some way to intercept the interrupt generated by TimeConstrained so that I can get a stack trace of the aborted computation, before it is removed from the stack?

According to the documentation, TimeConstrained generates an interrupt which interrupts the computation. This interrupt is treated just like an abort, in the sense that it respects AbortProtect.

I'd like to intercept this abort/interrupt and write a stack trace to a log file, so that I can determine whether or not the time out resulted from a bug. I tried this:

changeInterrupt[] :=
 (
  Unprotect[Interrupt];
  testInterrupt = True;
  Interrupt[args___] :=
   (
     Print["This is where I would write my stack trace to a log file."];
     Block[{testInterrupt = False}, Interrupt[args]]
   ) /; testInterrupt
  Protect[Interrupt];
 )

TimeConstrained[changeInterrupt[]; While[True, Null], 1]
changeInterrupt[]; TimeConstrained[While[True, Null], 1]

and also the same thing but with Interrupt replaced with Abort. I get the print statement when I generate an abort/interrupt, but not when either of the TimeConstrained invokations terminate.

Any ideas?

According to the documentation, TimeConstrained generates an interrupt which interrupts the computation. This interrupt is treated just like an abort, at least in the sense that it respects AbortProtect.

I want to see the contents of the stack at the time the abort is generated, before the aborted evaluation is removed from the stack. I tried this:

changeAbort[] :=
 (
  Unprotect[Abort];
  testAbort = True;
  Abort[args___] :=
   (
     Print[Stack[_]]; (* in real life, to a log file *)
     Block[{testAbort = False}, Abort[args]]
   ) /; testAbort
  Protect[Abort];
 )

This works for aborts that I generate myself. For example,

While[True, Abort[]]

aborts and prints the following stack trace:

(* 
{While[True,Abort[]],Print[Stack[_]];Block[{testAbort=False},Abort[]],Print[Stack[_]]}
*)

Along (spiritually) similar lines, I can print the the stack at the time a message is displayed with this code:

Internal`AddHandler["Message", Print[Stack[_]]&];

Getting back to TimeConstrained, neither of the following give me a stack trace:

changeAbort[];TimeConstrained[While[True, Null], 1]
TimeConstrained[changeAbort[]; While[True, Null], 1]

The last line was written in case TimeConstrained was implemented by temporarily redefining Abort. Unfortunately, it doesn't work.

Is there some way to intercept the interrupt generated by TimeConstrained so that I can get a stack trace of the aborted computation, before it is removed from the stack?

1
source | link

how to print stack trace when TimeConstrained times out

According to the documentation, TimeConstrained generates an interrupt which interrupts the computation. This interrupt is treated just like an abort, in the sense that it respects AbortProtect.

I'd like to intercept this abort/interrupt and write a stack trace to a log file, so that I can determine whether or not the time out resulted from a bug. I tried this:

changeInterrupt[] :=
 (
  Unprotect[Interrupt];
  testInterrupt = True;
  Interrupt[args___] :=
   (
     Print["This is where I would write my stack trace to a log file."];
     Block[{testInterrupt = False}, Interrupt[args]]
   ) /; testInterrupt
  Protect[Interrupt];
 )

TimeConstrained[changeInterrupt[]; While[True, Null], 1]
changeInterrupt[]; TimeConstrained[While[True, Null], 1]

and also the same thing but with Interrupt replaced with Abort. I get the print statement when I generate an abort/interrupt, but not when either of the TimeConstrained invokations terminate.

Any ideas?