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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,14 @@ export default class GoTrueClient {
): {
data: { subscription: Subscription }
} {
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'
)
}
Comment on lines +1850 to +1856
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.


const id: string = uuid()
const subscription: Subscription = {
id,
Expand Down
28 changes: 28 additions & 0 deletions test/GoTrueClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,34 @@ describe('The auth client can signin with third-party oAuth providers', () => {
})
})

describe('Developers are warned if subscribing with an async function', () => {
const originalWarn = console.warn
let warnings: any[][] = []

beforeEach(() => {
console.warn = (...args: any[]) => {
console.log('WARN', ...args)

warnings.push(args)
}
})

afterEach(() => {
console.warn = originalWarn
warnings = []
})

test('Subscribing a non-async listener should not log a warning', async () => {
authSubscriptionClient.onAuthStateChange(() => console.log('onAuthStateChange was called'))
expect(warnings.length).toEqual(0)
})

test('Subscribing an async listener should log a warning', async () => {
authSubscriptionClient.onAuthStateChange(async () => console.log('onAuthStateChange was called'))
expect(warnings.length).toEqual(1)
})
})

describe('Sign Up Enabled', () => {
test('User can sign up', async () => {
const { email, password } = mockUserCredentials()
Expand Down
Loading