diff --git a/lib/rufus/lua/state.rb b/lib/rufus/lua/state.rb
index <HASH>..<HASH> 100644
--- a/lib/rufus/lua/state.rb
+++ b/lib/rufus/lua/state.rb
@@ -424,6 +424,7 @@ module Rufus::Lua
 
       if lua_code == nil
 
+        Lib.lua_remove(@pointer, @error_handler) if @error_handler > 0
         @error_handler = 0
 
       elsif lua_code == :traceback
diff --git a/spec/error_handler_spec.rb b/spec/error_handler_spec.rb
index <HASH>..<HASH> 100644
--- a/spec/error_handler_spec.rb
+++ b/spec/error_handler_spec.rb
@@ -47,6 +47,8 @@ stack traceback:
 	[string "mystuff.lua:77"]:3: in function 'f'
 	[string "mymain.lua:88"]:1: in main chunk' (2 LUA_ERRRUN)
       }.strip)
+
+      expect(@s.send(:stack_top)).to eq(1)
     end
 
     it 'set the error handler in a permanent way' do
@@ -167,6 +169,7 @@ stack traceback:
       end
 
       expect(le.msg).to eq('[string "line"]:1: b')
+      expect(@s.send(:stack_top)).to eq(0)
     end
   end
 end
remove error_handler from stack when unsetting it

for gh-<I>