henrygd/beszel · error

failed to encode request: %w

Error message

failed to encode request: %w

What it means

In transport.SSH Request (internal/hub/transport/ssh.go:101), the hub writes a HubRequest struct CBOR-encoded to the SSH session's stdin pipe; if cbor.NewEncoder(...).Encode fails it wraps the error as 'failed to encode request'. This indicates the write to the agent's stdin failed or the value is not CBOR-encodable.

Source

Thrown at internal/hub/transport/ssh.go:101

	}
	defer session.Close()

	stdout, err := session.StdoutPipe()
	if err != nil {
		return err
	}
	stdin, err := session.StdinPipe()
	if err != nil {
		return err
	}
	if err := session.Shell(); err != nil {
		return err
	}

	// Send request
	hubReq := common.HubRequest[any]{Action: action, Data: req}
	if err := cbor.NewEncoder(stdin).Encode(hubReq); err != nil {
		return fmt.Errorf("failed to encode request: %w", err)
	}
	stdin.Close()

	// Read response
	var resp common.AgentResponse
	if err := cbor.NewDecoder(stdout).Decode(&resp); err != nil {
		return fmt.Errorf("failed to decode response: %w", err)
	}

	if resp.Error != "" {
		return errors.New(resp.Error)
	}

	if err := session.Wait(); err != nil {
		return err
	}

	return UnmarshalResponse(resp, action, dest)

View on GitHub (pinned to b38fb7dafa)

Solutions

  1. Check the agent process is running on the remote host and the SSH session is healthy
  2. Retry the request (RequestWithRetry already wraps this) after reconnecting the transport
  3. Inspect the wrapped error: 'broken pipe'/'io: read/write on closed pipe' means reconnect; 'unsupported type' means fix the payload
  4. Ensure req payloads use CBOR-serializable types (no funcs, channels, or unencodable maps)

Example fix

// before
err := transport.Request(ctx, action, reqWithChannel, &dest) // chan not CBOR-encodable
// after
err := transport.Request(ctx, action, plainStructReq, &dest) // use encodable types
Defensive patterns

Strategy: retry

Validate before calling

if !transport.IsConnected() {
    return errors.New("transport disconnected; reconnect before requesting")
}

Try / catch

err := t.Request(ctx, action, req, dest)
if err != nil && strings.Contains(err.Error(), "failed to encode request") {
    if strings.Contains(err.Error(), "pipe") {
        return retryAfterReconnect(ctx, action, req, dest)
    }
    return err // payload type problem; do not blind-retry
}

Prevention

When it happens

Trigger: The SSH session/pipe broke while sending (agent process exited, session closed mid-write, broken pipe), or the request payload contains a type the fxamacker/cbor encoder cannot marshal (unsupported type like chan/func, invalid map key).

Common situations: Agent crashed or restarted while a request was in flight; session torn down by createSessionWithTimeout/timeout; unusual payload types passed as req for custom actions.

Related errors


AI-assisted analysis of henrygd/beszel@b38fb7dafa (2026-08-31). Data as JSON: /api/errors/138f7b79b162b95d. Report an issue: GitHub.