Skip to content

fix(hns): preserve HRESULT in typed HNSError for downstream unwrapping#2800

Open
HarshalPatel1972 wants to merge 1 commit into
microsoft:mainfrom
HarshalPatel1972:patch-hcsshim
Open

fix(hns): preserve HRESULT in typed HNSError for downstream unwrapping#2800
HarshalPatel1972 wants to merge 1 commit into
microsoft:mainfrom
HarshalPatel1972:patch-hcsshim

Conversation

@HarshalPatel1972

@HarshalPatel1972 HarshalPatel1972 commented Jun 30, 2026

Copy link
Copy Markdown

Problem

Currently, hnsCall collapses the JSON failure response from the Host Networking Service into a flat string (fmt.Errorf("hns failed with error : %s", hnsresponse.Error)). This strips the underlying ErrorCode (HRESULT) returned by the API. Because of this, downstream consumers (specifically the containerd CRI plugin) cannot use errors.As() to inspect the error type. This leads to severe downstream bugs—such as containerd hanging indefinitely during transient vSwitch states, because it cannot identify the specific HRESULT that should trigger its retry/backoff logic.

Fix

  • Introduced a strongly-typed HNSError struct that implements the error interface and maps the ErrorCode field from the HNS JSON payload.
  • Modified hnsCall to return this typed error, ensuring the HRESULT is preserved and bubbled up the stack.

Verification

  • Added TestHNSErrorPreservation to mock a failed HNS JSON response containing a transient ErrorCode (e.g., 0x803b0013).
  • Verified that downstream callers can successfully use standard errors.As() to unwrap the error and evaluate the specific ErrorCode.

Previously, hnsCall swallowed the specific ErrorCode/HRESULT returned by the
Host Networking Service into a flat string. This prevented downstream callers
(like containerd's CRI plugin) from using errors.As() to detect retryable or
transient states (e.g., vSwitch restarting).

This change introduces a strongly-typed HNSError that preserves the ErrorCode,
allowing callers to correctly handle specific HNS failure modes.

Signed-off-by: Harshal Patel <hp842484@gmail.com>
@HarshalPatel1972 HarshalPatel1972 requested a review from a team as a code owner June 30, 2026 13:11
Copilot AI review requested due to automatic review settings June 30, 2026 13:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

Comment on lines +46 to 50
Success bool
Error string
ErrorCode uint32 `json:"ErrorCode,omitempty"`
Output json.RawMessage
}
Comment thread internal/hns/hnsfuncs.go
Comment on lines +48 to +51
return &HNSError{
ErrorString: hnsresponse.Error,
ErrorCode: hnsresponse.ErrorCode,
}
Comment thread internal/hns/hnsfuncs.go
Comment on lines +31 to +38
type HNSError struct {
ErrorString string
ErrorCode uint32
}

func (e *HNSError) Error() string {
return fmt.Sprintf("hns failed with error : %s", e.ErrorString)
}
Comment on lines +5 to +22
import (
"errors"
"testing"
)

func TestHNSErrorPreservation(t *testing.T) {
// Mock the raw response
originalMock := hnsCallRawResponseMock
defer func() { hnsCallRawResponseMock = originalMock }()

hnsCallRawResponseMock = func(method, path, request string) (*hnsResponse, error) {
return &hnsResponse{
Success: false,
Error: "vSwitch in transient state",
ErrorCode: 0x803b0013, // HCS_E_VMSWITCH_IN_TRANSIENT_STATE
}, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants