Sija/raven.cr

Issue with renaming #spawn

greenbigfrog opened this issue · 5 comments

While trying to capture Exceptions within fibers, I've discovered that there's an issue with overriding #spawn.

Using the code at src/raven/integrations/kernel/spawn.cr in combination with the following code produces the error beneath:

require "raven"
# require "./lib/raven/src/raven/integrations/kernel/*"

def spawn(*, name : String? = nil, &block)
  # ameba:disable Style/RedundantBegin
  wrapped_block = ->{
    begin
      block.call
    rescue ex
      Raven.capture(ex, extra: {
        in_fiber:   true,
        fiber_name: name,
      })
      raise ex
    end
  }
  previous_def(name: name, &wrapped_block)
end


Raven.capture do
  spawn do
    raise "This is a test #{Time.now}"
  end
end

sleep
Module validation failed: !dbg attachment points at wrong subprogram for function
!10 = distinct !DISubprogram(name: "initialize", linkageName: "initialize", scope: !5, file: !5, line: 115, type: !6, isLocal: true, isDefinition: true, scopeLine: 115, isOptimized: false, unit: !0, variables: !2)
void (%"OpenSSL::BIO"*, %TCPSocket*)* @"*OpenSSL::BIO#initialize<TCPSocket>:Nil"
  %2 = call i8** @"~OpenSSL::BIO::CRYSTAL_BIO:read"(), !dbg !12
!12 = !DILocation(line: 14, column: 5, scope: !13)
!13 = distinct !DISubprogram(name: "set_data", linkageName: "set_data", scope: !5, file: !5, line: 13, type: !6, isLocal: true, isDefinition: true, scopeLine: 13, isOptimized: false, unit: !0, variables: !2)
!13 = distinct !DISubprogram(name: "set_data", linkageName: "set_data", scope: !5, file: !5, line: 13, type: !6, isLocal: true, isDefinition: true, scopeLine: 13, isOptimized: false, unit: !0, variables: !2)
 (Exception)
Unhandled exception: Error opening file 'crystal' with mode 'r': No such file or directory (Errno)
Failed to raise an exception: END_OF_STACK
[0x5607c1dd4e86] ???
[0x5607c1507f0b] __crystal_raise +43
[0x5607c150948c] ???
[0x5607c1519cdb] ???
[0x5607c15130fb] ???
[0x5607c15121fc] ???
[0x5607c150ed49] ???
[0x5607c1543b94] ???
[0x5607c150bb5d] main +45
[0x7f62d2dfc223] __libc_start_main +243
[0x5607c150448e] _start +46
[0x0] ???

Renaming the modified #spawn makes it work without any issues.

Not sure how this can be fixed so I thought I'd open an issue here

Sija commented

Looks like overriding #spawn is triggering a Crystal bug.

Could you try to reduce it and post it at the crystal issue tracker?

I think it's almost impossible to narrow it down really.

require "raven"

def spawn(*, name : String? = nil, &block)
  wrapped_block = ->{
    begin
      block.call
    rescue ex
      Raven.capture(ex)
      raise ex
    end
  }
  previous_def(name: name, &wrapped_block)
end


Raven.capture do
  spawn do
    raise "This is a test #{Time.now}"
  end
end

sleep

@Sija you able to narrow it down any further?

Sija commented

not atm, but it seems related to crystal-lang/crystal#7060

Sija commented

Closing, as the original code doesn't reproduce the error anymore (at least not in Crystal 1.2.1)

require "raven"

def spawn(*, name : String? = nil, same_thread = false, &block)
  wrapped_block = ->{
    begin
      block.call
    rescue ex
      Raven.capture(ex)
      raise ex
    end
  }
  previous_def(name: name, same_thread: same_thread, &wrapped_block)
end


Raven.capture do
  spawn do
    raise "This is a test #{Time.utc}"
  end
end

sleep

Correctly fails with:

Unhandled exception in spawn: This is a test 2021-12-29 02:07:42 UTC (Exception)
  from bug.cr:18:5 in '->'
  from /usr/local/Cellar/crystal/1.2.1/src/primitives.cr:266:3 in '->'
  from /usr/local/Cellar/crystal/1.2.1/src/primitives.cr:266:3 in 'run'
  from /usr/local/Cellar/crystal/1.2.1/src/fiber.cr:92:34 in '->'