hibiken/asynq · error

failed to write task result

Error message

failed to write task result: %w

What it means

Error returned by ResultWriter.Write when writing the task's result data to Redis fails. Because the writer stores results in the same Redis instance the server is connected to, this fires on connection problems, Redis errors, or a closed/unavailable broker; the underlying cause is wrapped via %w.

Solutions

  1. Inspect the wrapped error (%w) to find the underlying redis/IO failure and fix it
  2. Check that the task's result data is writable and the redis connection backing the server is healthy
  3. Retry the task processing if the failure was transient (network blip, redis restart)
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at asynq.go:578 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of hibiken/asynq@d135f1439b (2026-09-07). Data as JSON: /api/errors/2398af5ee63433a5. Report an issue: GitHub.

Appendix: source

Thrown at asynq.go:578

		password = v
	}
	return RedisFailoverClientOpt{MasterName: master, SentinelAddrs: addrs, SentinelPassword: password, DB: db}, nil
}

// ResultWriter is a client interface to write result data for a task.
// It writes the data to the redis instance the server is connected to.
type ResultWriter struct {
	id     string // task ID this writer is responsible for
	qname  string // queue name the task belongs to
	broker base.Broker
	ctx    context.Context // context associated with the task
}

// Write writes the given data as a result of the task the ResultWriter is associated with.
func (w *ResultWriter) Write(data []byte) (n int, err error) {
	select {
	case <-w.ctx.Done():
		return 0, fmt.Errorf("failed to write task result: %w", w.ctx.Err())
	default:
	}
	return w.broker.WriteResult(w.qname, w.id, data)
}

// TaskID returns the ID of the task the ResultWriter is associated with.
func (w *ResultWriter) TaskID() string {
	return w.id
}

View on GitHub (pinned to d135f1439b)