amqp.cr icon indicating copy to clipboard operation
amqp.cr copied to clipboard

KeyError "Missing Hash Key" with basic example code

Open andy-twosticks opened this issue 8 years ago • 3 comments

Please forgive me if I am doing something stupid -- Crystal newbie -- but I can't get this to work. I made a simple program out of one of your examples, but I just get the above error:

#<KeyError:0x1be4f00 @message="Missing hash key: \"\"", @cause=nil, @callstack=CallStack(@callstack=[Pointer(Void)@0x4942f7, Pointer(Void)@0x49428a, Pointer(Void)@0x49425a, Pointer(Void)@0x490686, Pointer(Void)@0x54129a, Pointer(Void)@0x541196, Pointer(Void)@0x53f6c0, Pointer(Void)@0x53e990, Pointer(Void)@0x490ece, Pointer(Void)@0x4fefc0, Pointer(Void)@0x4feac4, Pointer(Void)@0x48f139, Pointer(Void)@0x4a68de, Pointer(Void)@0x48bd16, Pointer(Void).null], @backtrace=nil)>
0x4942f7: *CallStack::unwind:Array(Pointer(Void)) at ??
0x54129a: fetch at /opt/crystal/src/hash.cr 124:9
0x541196: [] at /opt/crystal/src/hash.cr 61:5
0x53f6c0: deliver_content at /home/andy/andy/try_raze/lib/amqp/src/amqp/channel.cr 600:7
0x53e990: process_frame at /home/andy/andy/try_raze/lib/amqp/src/amqp/channel.cr 570:11
0x4fefc0: on_frame at /home/andy/andy/try_raze/lib/amqp/src/amqp/broker.cr 255:3
0x4feac4: process_frames at /home/andy/andy/try_raze/lib/amqp/src/amqp/broker.cr 134:9
0x4a68de: run at /opt/crystal/src/fiber.cr 255:3
0x48bd16: ~proc2Proc(Fiber, (IO::FileDescriptor | Nil)) at /opt/crystal/src/concurrent.cr 61:3
0x0: ??? at ??
Unhandled exception in spawn:
Channel is closed with code 0:  (AMQP::ChannelClosed)
0x53ff53: rpc_call at /home/andy/andy/try_raze/lib/amqp/src/amqp/channel.cr 496:5
0x543e85: get at /home/andy/andy/try_raze/lib/amqp/src/amqp/queue.cr 181:16
0x543e2f: get at /home/andy/andy/try_raze/lib/amqp/src/amqp/queue.cr 179:3
0x4a68de: run at /opt/crystal/src/fiber.cr 255:3
0x48bd16: ~proc2Proc(Fiber, (IO::FileDescriptor | Nil)) at /opt/crystal/src/concurrent.cr 61:3
0x0: ??? at ??

Here's the program. I know that it's connecting okay. The error message only occurs on queues with messages in:

require "amqp"

HOST="10.0.0.150"
COUNT = 20
EXCHANGE_NAME = "basic_get"
QUEUE_NAME = "/queue/jonea.dev"
STDOUT.sync = true
config = AMQP::Config.new(host: HOST)

AMQP::Connection.start(config) do |conn|

  spawn do
    channel  = conn.channel
    exchange = channel.exchange(EXCHANGE_NAME, "direct", auto_delete: true)
    queue    = channel.queue(QUEUE_NAME)
    queue.bind(exchange, queue.name)

    counter = 0
    loop do
      msg = queue.get
      next unless msg
      counter += 1
      puts "Received msg: #{msg.to_s}. Count: #{msg.message_count}"
      msg.ack
      break if counter == COUNT
      sleep 0.5
    end

    queue.unbind(exchange, queue.name)
    queue.delete
    channel.close
    conn.loop_break
  end

  conn.run_loop
end

andy-twosticks avatar Aug 10 '17 15:08 andy-twosticks

Im getting this too and for the life of my its indecypherable

shayneoneill avatar Nov 02 '17 21:11 shayneoneill

amqp.cr want to include a reference to the Exchange instance, but if the exchange hasn't been delcared on the same channel earlier it will throw this exception. pretty easy fix, don't include a refernce to the exchange object, only the name of the exchange.

carlhoerberg avatar Mar 29 '18 10:03 carlhoerberg

I encountered the same problem with the following code:

    queue = channel.queue(QUEUE_NAME, passive: true)
    queue.subscribe do |msg|
      puts "Received msg (1): #{msg.to_s}"
      msg.ack
    end

I am confused by @carlhoerberg comment. Actually I feel the opposite way. So I add the following line:

    exchange = channel.default_exchange

Then this exception is gone.

However, in @andy-twosticks example, the exchange has been declared within the same channel. So @carlhoerberg suggestion seems not to fit there either.

In my case, the line of code caused trouble is:

#<KeyError:Missing hash key: "">
/usr/lib/crystal/hash.cr:0:9 in 'fetch'
/usr/lib/crystal/hash.cr:62:5 in '[]'
lib/amqp/src/amqp/channel.cr:585:7 in 'deliver_content'
lib/amqp/src/amqp/channel.cr:570:11 in 'process_frame'
lib/amqp/src/amqp/broker.cr:0:3 in 'on_frame'
lib/amqp/src/amqp/broker.cr:134:9 in 'process_frames'
/usr/lib/crystal/fiber.cr:255:3 in 'run'
/usr/lib/crystal/concurrent.cr:61:3 in '~proc2Proc(Fiber, (IO::FileDescriptor | Nil))'

So this bug still needs to be fixed.

nengxu avatar Jun 14 '18 22:06 nengxu