Skip to content

Conversation

@mandarini
Copy link
Contributor

What kind of change does this PR introduce?

Automatically initialize session on server client creation to trigger token refresh early in request lifecycle. This prevents race condition where refresh completes after response is sent, causing cookie errors. Also adds helpful error context before throwing if edge cases occur.

What is the current behavior?

Error: Cannot use `cookies.set(...)` after the response has been generated

Most possibly caused by token refresh.

What is the new behavior?

Initialize session to trigger token refresh early.

@mandarini mandarini force-pushed the fix/async-token-refresh-race-condition branch from dc11250 to 9c556eb Compare October 14, 2025 10:48
@mandarini mandarini requested a review from hf October 14, 2025 14:22
@mandarini mandarini self-assigned this Oct 14, 2025
Comment on lines +221 to +226
Promise.resolve().then(() => {
client.auth.getSession().catch(() => {
// Ignore errors - if session loading fails, the client is still usable
// and subsequent auth operations will handle the error appropriately
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit scared of this as it runs all the time when the server gets created, but without the ability to set cookies if that's needed

Copy link
Contributor Author

@mandarini mandarini Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hf Yeah I agree, it's a slippery slope. I wonder if there's a better way to treat this, and not get that race condition issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants