Skip to content

feat: warn on async subscriber to onAuthStateChange #1062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AJamesPhillips
Copy link

What kind of change does this PR introduce?

Feature to warn developers that subscribing to onAuthStateChange with an async function might cause problems.

What is the current behavior?

Currently no warning is issued either when subscribing, or when calling other supabase functions from inside the onAuthStateChange subscriber leading to the supabase-js client silently hanging. Related issue: supabase/supabase-js#1401

What is the new behavior?

The new behaviour results in the developer being warned that they have subscribed with an async function.

Additional context

I couldn't get the tests to run locally so this PR may not be functioning correctly.

Comment on lines +1850 to +1856
if (callback.constructor.name === 'AsyncFunction') {
console.warn(
'Calling onAuthStateChange with an async function may cause a deadlock.'
+ ' For a solution please see the docs: ' +
'https://supabase.com/docs/reference/javascript/auth-onauthstatechange'
)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

In and of itself there is no problem subscribing an async function, but the problem arises if you use another Supabase Client Library within, and that is almost impossible to detect reliably :(

If you want, you can try to find a way -- maybe setting a boolean on the object and checking it in any other auth-js method?

Copy link
Author

@AJamesPhillips AJamesPhillips May 16, 2025

Choose a reason for hiding this comment

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

Yep, I totally agree. I couldn't think of any elegant way of dealing with this but wanted to help minimise the chance of someone else spending half a day getting confused by a race condition. 🙂
Perhaps this warning could only be displayed if the debug flag was set to true?
Certainly if it's not called with an Async function then it can't deadlock.

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.

2 participants