vert-x3 / vertx-proton

Apache License 2.0
26 stars 26 forks source link

How can a link's open handler be invoked with a failed result? #86

Closed sophokles73 closed 5 years ago

sophokles73 commented 5 years ago

Hi,

I have had some trouble recently with handling rejected attach attempts in my code. My understanding is that my client needs to register an openHandler on the link before opening it. My client then sends its attach frame and the peer can process it. If the peer does not want to open the link, then the peer will send a detach frame instead of an attach frame in response to my client's request, right? On receipt of the detach frame, my client's closeHandler or detachHandler will be invoked (depending on the value of the close property).

What I do not understand is: under which circumstances will my client's openHandler be invoked with a failed AsyncResult? The attach frame does not allow to include an error condition, so I do not see how this could ever happen. Or am I mistaken?

gemmellr commented 5 years ago

If the peer does not want to open the link, then the peer will send a detach frame instead of an attach frame in response to my client's request, right?

No, the peer sends an attach and then a detach, even in the failure case.

Due to the way proton works, by the time the attach is signalled via the open handler it might (but might not) actually already know there was an error condition sent in a detach, in which case case the handler fires with a failed result essentially as a hint that the attach hasn't succeeded, allowing you to skip certain processing if you wanted.

sophokles73 commented 5 years ago

Does this mean that the client's openHandler might as well be invoked with a succeeded result if proton did not (yet) receive and process the accompanying detach frame? That would explain the behavior of my client that I am experiencing:

  1. client sets openHandler
  2. client sets closeHandler
  3. client opens link
  4. peer closes link instead of opening it
  5. client's open handler is invoked with a succeeded result
  6. client's close handler is invoked

Sometimes, though, the client's openHandler is (correctly) invoked with a failed result. Based on your explanation that would happen if the detach frame is received in close enough succession to the peer's attach frame for proton to process it in context with each other, right?

However, if all of this is true, then what is the right way to implement the client's openHandler so that it can deterministicly detect that the peer has rejected to establish the link?

gemmellr commented 5 years ago

Yes, if a detach with an error condition is received in the same TCP read then the open handler can indicate a failed result. If it wasn't, it wont.

In your open handler, something else to look at is the remote source/target terminus for the receiver/sender. The spec indicates that when refusing a peer should set the terminus null in the attach as a pre-indicator of refusal and the impending detach.

sophokles73 commented 5 years ago

Thanks for explaining. Got it :+1: