When opening and attaching a link, an exception will cause the link to close. During this closing process, we should only send the Detach frame to remote if we have already sent or received an Attach frame. Otherwise, the remote party will have no idea about the existence of this link at the moment, and there will be no valid handle for this link on the remote session, so the remote session will not be able to handle this Detach frame.
For example, this behavior can be currently reproduced if the below exception is thrown from AmqpLink.AttachLink(AmqpLink link)
if (this.links.ContainsKey(link.Name))
{
throw new AmqpException(AmqpErrorCode.ResourceLocked, AmqpResources.GetString(AmqpResources.AmqpLinkNameInUse, link.Name, this.LocalChannel));
}
When opening and attaching a link, an exception will cause the link to close. During this closing process, we should only send the Detach frame to remote if we have already sent or received an Attach frame. Otherwise, the remote party will have no idea about the existence of this link at the moment, and there will be no valid handle for this link on the remote session, so the remote session will not be able to handle this Detach frame.
For example, this behavior can be currently reproduced if the below exception is thrown from AmqpLink.AttachLink(AmqpLink link)