AlistGo/alist · error

failed to renew token: %w

Error message

failed to renew token: %w

What it means

renewToken wraps a transport-level failure from postForm against /user/renew_session_token.php: network unreachable, timeout, DNS failure, TLS error, or the response body failing to unmarshal into MediafireRenewTokenResponse (API format change, HTML error page). The %w preserves the underlying cause. This is the cron-job path (Init schedules renewal every 6-9 minutes), so failures here surface as an expired-token cascade in later API calls.

Source

Thrown at drivers/mediafire/util.go:126

	d.SessionToken = tokenResp.Response.SessionToken

	//fmt.Printf("Init :: Obtain Session Token %v", d.SessionToken)

	op.MustSaveDriverStorage(d)

	return d.SessionToken, nil
}

func (d *Mediafire) renewToken(_ context.Context) error {
	query := map[string]string{
		"session_token":   d.SessionToken,
		"response_format": "json",
	}

	var resp MediafireRenewTokenResponse
	_, err := d.postForm("/user/renew_session_token.php", query, &resp)
	if err != nil {
		return fmt.Errorf("failed to renew token: %w", err)
	}

	//fmt.Printf("getInfo :: Raw response: %s\n", string(body))
	//fmt.Printf("getInfo :: Parsed response: %+v\n", resp)

	if resp.Response.Result != "Success" {
		return fmt.Errorf("MediaFire token renewal failed: %s", resp.Response.Result)
	}

	d.SessionToken = resp.Response.SessionToken

	//fmt.Printf("Init :: Renew Session Token: %s", resp.Response.Result)

	op.MustSaveDriverStorage(d)

	return nil
}

View on GitHub (pinned to 843d9dc814)

Solutions

  1. Inspect the wrapped error: net/DNS/timeout issues are environmental; JSON errors suggest an HTML or changed response
  2. Make renewal errors observable (log them) instead of discarding in the cron callback
  3. On next failure, fall back to a full getSessionToken with a fresh cookie rather than endless renew attempts
  4. Retry with backoff for transient network failures

Example fix

// before: cron discards the renewal error
d.cron.Do(func() {
    d.renewToken(ctx)
})

// after: log and escalate to full token re-acquisition
d.cron.Do(func() {
    if err := d.renewToken(ctx); err != nil {
        log.Printf("[mediafire] token renewal failed: %v, re-authenticating", err)
        if err2 := d.getSessionToken(ctx); err2 != nil {
            log.Printf("[mediafire] re-auth failed: %v", err2)
        }
    }
})
Defensive patterns

Strategy: retry

Try / catch

In the cron callback, check the error: for net.Error/timeout, retry with backoff (up to 3 attempts); for JSON unmarshal failures, capture and log the body; if renewal keeps failing, fall back to full getSessionToken with a fresh cookie.

Prevention

When it happens

Trigger: The renewal cron firing during a network blip or MediaFire outage; MediaFire returning an HTML Cloudflare challenge page that fails JSON unmarshal; proxy misconfiguration on the host.

Common situations: Servers with flaky egress; token renewal errors silently ignored (cron Do discards the error) until the session actually expires and every driver call fails.

Related errors


AI-assisted analysis of AlistGo/alist@843d9dc814 (2026-08-15). Data as JSON: /api/errors/1a4cf7f516b8f5ee. Report an issue: GitHub.