xpzouying/xiaohongshu-mcp · error

read current user state failed

Error message

read current user state failed

What it means

CurrentUser reads the logged-in user info by running a JS snippet against window.__INITIAL_STATE__ on the already-loaded xiaohongshu explore page. 'read current user state failed' wraps any error returned by rod's Eval — the page.evaluate call itself failed (execution context destroyed, page navigating/closed, timeout of the 10s sub-context, or CDP error), not that the user data is missing (that's a separate 'current user not found in page state' error).

Source

Thrown at xiaohongshu/login.go:57

// CurrentUser 当前登录用户的基础信息。
type CurrentUser struct {
	Nickname string `json:"nickname"`
	UserID   string `json:"userId"`
}

// CurrentUser 从当前页面的 __INITIAL_STATE__ 读取登录用户信息。
// 需在 CheckLoginStatus 之后调用:复用已加载的 explore 页,不做额外导航。
func (a *LoginAction) CurrentUser(ctx context.Context) (*CurrentUser, error) {
	pp := a.page.Context(ctx).Timeout(10 * time.Second)

	res, err := pp.Eval(`() => {
		const u = window.__INITIAL_STATE__ && window.__INITIAL_STATE__.user;
		const info = u && u.userInfo && u.userInfo.value !== undefined ? u.userInfo.value : (u && u.userInfo);
		if (!info || info.guest) return "";
		return JSON.stringify({nickname: info.nickname, userId: info.userId || info.user_id});
	}`)
	if err != nil {
		return nil, errors.Wrap(err, "read current user state failed")
	}

	raw := res.Value.String()
	if raw == "" {
		return nil, errors.New("current user not found in page state")
	}

	var user CurrentUser
	if err := json.Unmarshal([]byte(raw), &user); err != nil {
		return nil, errors.Wrap(err, "unmarshal current user failed")
	}

	return &user, nil
}

func (a *LoginAction) Login(ctx context.Context) error {
	pp := a.page.Context(ctx)

View on GitHub (pinned to 332d196854)

Solutions

  1. Ensure the explore page is loaded and stable before calling CurrentUser (call CheckLoginStatus or re-navigate first)
  2. Increase the per-call deadline by passing a ctx with a longer budget, since CurrentUser wraps the page in Timeout(10s)
  3. Re-create or re-navigate the rod.Page if it was closed, then retry CurrentUser once
  4. Check the wrapped rod error: 'execution context destroyed' means navigation raced the eval — add a short sleep or WaitDOMStable before the call

Example fix

// before
user, err := login.CurrentUser(ctx) // page was closed earlier
// after
pp := page.MustNavigate("https://www.xiaohongshu.com/explore").MustWaitLoad()
login := xiaohongshu.NewLogin(pp)
user, err := login.CurrentUser(context.Background()) // fresh page, ample ctx budget
Defensive patterns

Strategy: try-catch

Validate before calling

if page.IsClosed() { return nil, errors.New("page closed before CurrentUser") }
if _, err := page.Element(".main-container .user .link-wrapper .channel"); err != nil { return nil, errors.New("not on logged-in explore page") }

Type guard

func evalOk(res *proto.EvalResponse) bool { return res != nil && !res.Value.Nil() && res.Value.Str() != "" }

Try / catch

user, err := login.CurrentUser(ctx)
if err != nil {
    if strings.Contains(err.Error(), "context deadline exceeded") {
        // retry with fresh page navigation
    }
    return fmt.Errorf("cannot read current user: %w", err)
}

Prevention

When it happens

Trigger: Calling LoginAction.CurrentUser when the page is navigating or was closed/navigated away since CheckLoginStatus; the 10s Timeout(ctx) deadline expires before the eval returns; the browser tab crashed or the CDP connection dropped; the page is on a different origin than explore so the eval hits a destroyed context.

Common situations: Calling CurrentUser after a redirect or on a freshly created page without first loading the explore page via CheckLoginStatus; headless runs where a previous step closed the page; a slow network making the 10s deadline fire; xiaohongshu front-end redeploy replacing __INITIAL_STATE__ shape causing eval-time JS exceptions.

Related errors


AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05). Data as JSON: /api/errors/637f9e98ac8ac423. Report an issue: GitHub.