vert-x3 / vertx-lang-ruby

Vert.x Ruby support
Apache License 2.0
14 stars 12 forks source link

vertx_stop does not appear to block #16

Closed sigil66 closed 8 years ago

sigil66 commented 8 years ago

When running a ruby script as a verticle it would appear that vertx_stop does not block. I am not sure if this is the intent.

I have gotten around the issue utilize the vertx_stop_async method and completing the future after the async block completes.

Clarification here would be helpful.

purplefox commented 8 years ago

Hi, can you elaborate on what you mean by "does not block"?

sigil66 commented 8 years ago

Sure, if I utilize async methods on objects such as an httpclient, vertx_stop executes but none of the async methods do. No error is printed. So while this may be correct given the lifecycle (has the event loop been stopped?) it wasn't obvious to me that there was an issue until digging into it.

purplefox commented 8 years ago

Perhaps you could illustrate what you mean with an example? I'm still not clear what you mean here.

sigil66 commented 8 years ago
@client = $vertx.create_http_client({
  'defaultHost' => 'localhost',
  'defaultPort' => 8500
})

def vertx_stop
  puts "vertx_stop called" #prints to console
  @client.request(:GET, "/some/path") { |response|
    if response.status_code() == 200
      puts "Success!" # Never executes, no output
    else 
      puts "Failed!" # Never executes, no output
    end
  }.end()
end
pmlopes commented 8 years ago

That happens due to the synchronicity of the code. Look at the flow:

  1. vertx_stop is called
  2. a http async client is created and a request is made adding a new callback handler to the event loop
  3. vertx_stop completes (which implies, killing the event loop)

As you can see, your callback handler will not be called since the event loop is already dead.

In order to have it called you need to wait, probably using the method variant with a future.

sigil66 commented 8 years ago

Great, thank you for the help!