# Program behavior differs when tracing function is set

**URL:** <https://www.ruby-forum.com/t/program-behavior-differs-when-tracing-function-is-set/121948>\
**Category:** Ruby\
**Created:** [December 3, 2007, 9:06am UTC](https://www.ruby-forum.com/t/program-behavior-differs-when-tracing-function-is-set/121948 "2007-12-03T09:06:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![timas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb9ed0/32.png) [@timas](https://www.ruby-forum.com/u/timas)\
**Post date:** [December 3, 2007, 9:06am UTC](https://www.ruby-forum.com/t/program-behavior-differs-when-tracing-function-is-set/121948/1 "2007-12-03T09:06:31Z")

</div>

Hi,

I’ve been working on simple ruby debugger that is using  
Kernel.set\_trace\_func method. In my work I’ve found that it is possible  
to write a program that behave different depending on tracing function  
is set or not.

Here is an example application:  
at\_exit {  
puts ‘program terminated’  
}  
Thread.abort\_on\_exception = true  
$stdout.sync = true  
$stderr.sync = true  
begin  
Thread.new {  
raise Exception, ‘Kill them all!!!’  
}  
sleep 5  
rescue SystemExit  
printf(“Exception caught\n”)  
end  
set\_trace\_func nil

The output is:  
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:19:  
Kill them all!!! (Exception)  
from  
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:18:in  
`initialize' from C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:18:in`new’  
from  
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:18  
Exception caught  
program terminated

But if we add code setting dummy tracing function, we will find that  
sleep is not interrupted and rescue block is not called:

at\_exit {  
puts ‘program terminated’  
}  
Thread.abort\_on\_exception = true  
$stdout.sync = true  
$stderr.sync = true

def trace(event, file, line, id, binding, klass)  
a = 1  
end  
set\_trace\_func proc { |event, file, line, id, binding, klass|  
trace(event, file, line, id, binding, klass)  
}

begin  
Thread.new {  
raise Exception, ‘Kill them all!!!’  
}  
sleep 5  
rescue SystemExit  
printf(“Exception caught\n”)  
end  
set\_trace\_func nil

The output is:  
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:18:  
Kill them all!!! (Exception)  
from  
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:17:in  
`initialize' from C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:17:in`new’  
from  
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:17  
program terminated

I want somebody to comment what is the big difference?

Thanks,  
Timur
