chenhg5/cc-connect · info

googlechat: close attachment create response body: %w

Error message

googlechat: close attachment create response body: %w

What it means

Closing the HTTP response body of the attachment-create call returned an error. Go's http.Response.Body.Close can fail when the connection could not be reused or the underlying read failed; the platform surfaces it rather than ignoring it.

Source

Thrown at platform/googlechat/googlechat.go:554

	url, body, err := buildAttachmentRequest(rc, resourceName)
	if err != nil {
		return err
	}
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
	if err != nil {
		return fmt.Errorf("googlechat: attachment message: build request: %w", err)
	}
	req.Header.Set("Content-Type", "application/json")
	resp, err := p.doRequest(req)
	if err != nil {
		return err
	}
	if _, err := io.Copy(io.Discard, resp.Body); err != nil {
		_ = resp.Body.Close()
		return fmt.Errorf("googlechat: drain attachment create response body: %w", err)
	}
	if err := resp.Body.Close(); err != nil {
		return fmt.Errorf("googlechat: close attachment create response body: %w", err)
	}
	return nil
}

// SendImage uploads an image and posts it as a Chat message attachment.
// Implements core.ImageSender.
func (p *Platform) SendImage(ctx context.Context, rctx any, img core.ImageAttachment) error {
	rc, ok := rctx.(replyContext)
	if !ok {
		return fmt.Errorf("googlechat: SendImage: invalid reply context type %T", rctx)
	}
	return p.postAttachment(ctx, rc,
		coalesce(img.FileName, "image.png"),
		coalesce(img.MimeType, "image/png"),
		img.Data)
}

// SendFile uploads a file and posts it as a Chat message attachment.

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Treat as non-fatal if the message was confirmed sent — log and continue
  2. Verify no proxy/middlebox is resetting connections to chat.googleapis.com
  3. Retry only if delivery is genuinely uncertain
Defensive patterns

Strategy: try-catch

Try / catch

if err := p.SendFile(ctx, rc, file); err != nil {
    if strings.Contains(err.Error(), "close attachment create response body") {
        slog.Warn("googlechat: body close failed; send likely succeeded", "error", err)
        return nil
    }
    return err
}

Prevention

When it happens

Trigger: resp.Body.Close() returns non-nil after a successful attachment-create POST — typically due to a broken underlying connection or an error on a wrapped body (e.g. compressed responses failing to finalize).

Common situations: Keep-alive connection teardown races; HTTP/2 stream reset after the payload was already consumed; proxy interference. The send itself usually succeeded.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/ad6ae533d4035650. Report an issue: GitHub.