OpenNHP/opennhp · warning

failed to compress response body

Error message

failed to compress response body: %w

What it means

After reading the body, GetEvidenceWithCCUrl zlib-compresses it; this wraps a failure from w.Write or w.Close. Practically this only fires if the underlying bytes.Buffer write fails, which is nearly impossible — the error is defensive.

Solutions

  1. Verify w.Write's error is checked separately from w.Close so failures are attributed correctly
  2. If this fires, inspect system memory (bytes.Buffer growth) and Go runtime health
  3. Treat as a bug report scenario — capture the wrapped error and open an issue

Example fix

// before
_, err = w.Write(body)
w.Close()
if err != nil {
    return nil, fmt.Errorf("failed to compress response body: %w", err)
}
// after
if _, err = w.Write(body); err != nil {
    return nil, fmt.Errorf("failed to compress response body: %w", err)
}
if err = w.Close(); err != nil {
    return nil, fmt.Errorf("failed to close zlib writer: %w", err)
}
Defensive patterns

Strategy: try-catch

Try / catch

ev, err := engine.GetEvidenceWithCCUrl()
if err != nil {
    if strings.Contains(err.Error(), "failed to compress response body") {
        // in-memory compression failed; report bug, retry rarely helps
        return fmt.Errorf("evidence compression failed: %w", err)
    }
}

Prevention

When it happens

Trigger: The zlib Writer's Close returning an error (e.g. flushing internal deflate state failed), or Write to the in-memory buffer erroring; note the code ignores Write's error and only checks the one after Close.

Common situations: Essentially unreachable in normal operation; would indicate severe memory exhaustion or a Go runtime-level failure.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of OpenNHP/opennhp@6e04ca5ff0 (2026-09-07). Data as JSON: /api/errors/b51e0d7f5f4e4e63. Report an issue: GitHub.

Appendix: source

Thrown at nhp/core/wasm/engine/host.go:61

	}

	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
	}

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("failed to read response body: %w", err)
	}

	var buf bytes.Buffer
	w := zlib.NewWriter(&buf)
	_, err = w.Write(body)
	w.Close()
	if err != nil {
		return nil, fmt.Errorf("failed to compress response body: %w", err)
	}

	compressedBody := buf.Bytes()

	return compressedBody, nil
}

func GetEvidenceWithAgentUuid() ([]byte, error) {
	agentUniqueId, err := CalculateAgentUniqueId()
	if err != nil {
		return nil, fmt.Errorf("failed to get agent unique id: %v", err)
	}

	evidence := map[string]any{
		"test_purpose":  "this evidence is for testing purposes only",
		"measure":       agentUniqueId,
		"serial_number": agentUniqueId,
	}

View on GitHub (pinned to 6e04ca5ff0)