[M5] mxaccess-asb: register_items retry on InvalidConnectionId — LIVE PATH WORKS
rust / build / test / clippy / fmt (push) Has been cancelled

End-to-end live path now functional: Connect → AuthenticateMe →
RegisterItems → Read → Disconnect. The example reads back the live
TestChildObject.TestInt value (99) over the wire on the first run.

Root-cause of the previous "InvalidConnectionId" mystery: it was
never an HMAC verification failure. `AuthenticateMe` is one-way
(`AsbContracts.cs:18`) and the server commits auth state
asynchronously after the request lands. A Register that follows too
quickly sees the connection in pre-authenticated state and returns
`AsbErrorCode.InvalidConnectionId` (= 1).

.NET's `MxAsbDataClient.RegisterMany` (`cs:191-204`) handles this
explicitly with a retry loop:

  for (int attempt = 1; attempt < 5
       && response.Result.ErrorCode == InvalidConnectionId; attempt++)
  {
      Thread.Sleep(TimeSpan.FromMilliseconds(100 * attempt));
      response = RegisterOnce(items);
  }

We now mirror the same pattern in `AsbClient::register_items_once`
followed by a retry loop in `register_items` — up to 5 attempts with
`100 * attempt` ms backoff.

Supporting changes:
- `RegisterItemsResponse` gains `result_code: Option<u32>` +
  `success: Option<bool>` so callers can read `Result.resultCodeField`
  + `successField` from the response. `decode_register_items_response`
  now tolerates an empty `<ASBIData />` Status array (server returns
  empty when the operation fails server-side) instead of erroring
  with `MissingField`. New helper `find_text_in_named_element` walks
  the body token stream.
- New public constant `RESULT_CODE_INVALID_CONNECTION_ID = 1` for
  callers that want to detect this status outside the retry path.
- The previously-failing test `decode_register_items_response_returns_
  missing_field_when_status_absent` was renamed and rewritten as
  `decode_register_items_response_returns_empty_status_when_absent`
  to match the new tolerant decode contract.

F31 closed. F30 (read-side dict-id resolution, landed in `eb6c689`)
was the unblocker — without it we couldn't see the
`<resultCodeField>1</>` element in the response and the failure mode
looked like a HMAC mismatch instead of a transient retryable error.

Workspace: 711 unit tests pass. Clippy clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-05-05 21:02:38 -04:00
parent eb6c689f09
commit 9063f10b1b
3 changed files with 120 additions and 26 deletions
+32 -1
View File
@@ -547,12 +547,43 @@ impl<T: AsyncRead + AsyncWrite + Unpin + Send> AsbClient<T> {
}
/// `RegisterItems` operation — sends a signed `RegisterItemsIn`
/// SOAP envelope and decodes the `RegisterItemsResponse`.
/// SOAP envelope and decodes the `RegisterItemsResponse`. Retries
/// up to 5 times with `100 * attempt` ms backoff on
/// `InvalidConnectionId` (`AsbErrorCode = 1`), mirroring .NET's
/// `MxAsbDataClient.RegisterMany` (`cs:191-204`). The retry exists
/// because `AuthenticateMe` is one-way: the server may not have
/// finished processing it before our first Register lands, in
/// which case it sees an unauthenticated connection and returns
/// `InvalidConnectionId`. A short backoff lets the auth state
/// commit on the server side.
pub async fn register_items(
&mut self,
items: &[ItemIdentity],
require_id: bool,
register_only: bool,
) -> Result<RegisterItemsResponse, ClientError> {
let mut response = self
.register_items_once(items, require_id, register_only)
.await?;
let mut attempt = 1u32;
while attempt < 5
&& response.result_code
== Some(crate::operations::RESULT_CODE_INVALID_CONNECTION_ID)
{
tokio::time::sleep(std::time::Duration::from_millis(100 * u64::from(attempt))).await;
response = self
.register_items_once(items, require_id, register_only)
.await?;
attempt += 1;
}
Ok(response)
}
async fn register_items_once(
&mut self,
items: &[ItemIdentity],
require_id: bool,
register_only: bool,
) -> Result<RegisterItemsResponse, ClientError> {
let pre_signing = ConnectionValidator {
connection_id: self.authenticator.connection_id(),
+85 -12
View File
@@ -1080,8 +1080,24 @@ pub struct RegisterItemsResponse {
/// Whether the `<ItemCapabilities>` element appeared. Decoding the
/// individual `ItemRegistration` records is a future iteration.
pub item_capabilities_present: bool,
/// `Result.resultCodeField` from the response — `0` is success,
/// `1` is `InvalidConnectionId` (transient race with the one-way
/// AuthenticateMe), see `AsbResultMapping.cs:6` for the full enum.
/// `None` if the field wasn't found in the response.
pub result_code: Option<u32>,
/// `Result.successField` — `false` means the operation failed
/// server-side and the per-item Status array is empty.
pub success: Option<bool>,
}
/// `AsbErrorCode.InvalidConnectionId` per `AsbResultMapping.cs:6`.
/// Surfaces as `Result.resultCodeField=1` when the server has not
/// (yet) processed our one-way AuthenticateMe and treats the
/// connection as unauthenticated. .NET's `MxAsbDataClient.RegisterMany`
/// (`cs:191-204`) retries up to 5 times with a 100*N ms backoff per
/// attempt — we mirror that pattern in `AsbClient::register_items`.
pub const RESULT_CODE_INVALID_CONNECTION_ID: u32 = 1;
/// Decoded `UnregisterItemsResponse`. Single field: the per-item
/// `Status` array (`AsbContracts.cs:153-159`).
#[derive(Debug, Clone, PartialEq)]
@@ -1090,23 +1106,75 @@ pub struct UnregisterItemsResponse {
}
/// Decode a `RegisterItemsResponse` SOAP body from the NBFX token
/// stream returned by [`crate::decode_envelope`].
/// stream returned by [`crate::decode_envelope`]. Tolerates an empty
/// or missing `<ASBIData>` (Status array) — that's how the server
/// signals an operation-level failure (e.g. `successField=false` +
/// non-zero `resultCodeField`). Caller is expected to inspect
/// `result_code` for transient failures like InvalidConnectionId
/// and retry where appropriate.
pub fn decode_register_items_response(
body_tokens: &[NbfxToken],
) -> Result<RegisterItemsResponse, OperationError> {
let payloads = collect_asbidata_payloads(body_tokens);
let status_payload = payloads
.into_iter()
.next()
.ok_or(OperationError::MissingField { field: "Status" })?;
let status = decode_item_status_array(&status_payload)?;
let status = match payloads.into_iter().next() {
Some(payload) if !payload.is_empty() => decode_item_status_array(&payload)?,
_ => Vec::new(),
};
let item_capabilities_present = find_element_named(body_tokens, "ItemCapabilities").is_some();
let result_code = find_text_in_named_element(body_tokens, "resultCodeField")
.and_then(|s| s.parse().ok());
let success = find_text_in_named_element(body_tokens, "successField")
.map(|s| s.eq_ignore_ascii_case("true"));
Ok(RegisterItemsResponse {
status,
item_capabilities_present,
result_code,
success,
})
}
/// Walk the token stream looking for an element with the given local
/// name (inline match) and return its first text child as a string.
/// Used to extract `Result.resultCodeField`, `successField`, etc.
/// from the structured RegisterItemsResponse body.
fn find_text_in_named_element(tokens: &[NbfxToken], name: &str) -> Option<String> {
let mut idx = 0;
while let Some(tok) = tokens.get(idx) {
if let NbfxToken::Element {
name: NbfxName::Inline(local),
..
} = tok
{
if local == name {
let mut inner = idx + 1;
while matches!(
tokens.get(inner),
Some(NbfxToken::Attribute { .. })
| Some(NbfxToken::DefaultNamespace { .. })
| Some(NbfxToken::NamespaceDeclaration { .. })
) {
inner += 1;
}
if let Some(NbfxToken::Text(text)) = tokens.get(inner) {
return Some(match text {
NbfxText::Chars(s) => s.clone(),
NbfxText::Zero => "0".to_string(),
NbfxText::One => "1".to_string(),
NbfxText::Bool(b) => b.to_string(),
NbfxText::Int8(n) => n.to_string(),
NbfxText::Int16(n) => n.to_string(),
NbfxText::Int32(n) => n.to_string(),
NbfxText::Int64(n) => n.to_string(),
_ => return None,
});
}
}
}
idx += 1;
}
None
}
/// Decode an `UnregisterItemsResponse` SOAP body.
pub fn decode_unregister_items_response(
body_tokens: &[NbfxToken],
@@ -1610,13 +1678,18 @@ mod tests {
}
#[test]
fn decode_register_items_response_returns_missing_field_when_status_absent() {
fn decode_register_items_response_returns_empty_status_when_absent() {
// Per the live wire capture, the server returns an empty
// `<ASBIData />` (Status array) when an operation fails (e.g.
// `successField=false` + `resultCodeField=1`). Decode now
// tolerates this rather than erroring with `MissingField` —
// callers inspect `result_code` for the failure reason.
let body = asbidata_request_body("RegisterItemsResponse", &[]);
let err = decode_register_items_response(&body).unwrap_err();
assert!(matches!(
err,
OperationError::MissingField { field: "Status" }
));
let response = decode_register_items_response(&body).unwrap();
assert!(response.status.is_empty());
assert!(!response.item_capabilities_present);
assert_eq!(response.result_code, None);
assert_eq!(response.success, None);
}
#[test]