Skip to content
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

feat: refactor to _useSession semantics #726

Merged
merged 1 commit into from
Jul 13, 2023
Merged

Conversation

hf
Copy link
Contributor

@hf hf commented Jul 9, 2023

getSession() is problematic. The issue comes because whenever the library itself does anything with the session, it may be very quick < 10ms or very slow >= 10s. For example, if a session needs to be refreshed, when you call getSession() the response may be very quick or take very long.

When such long processing is required, a race condition arises between multiple windows or tabs concurrently doing the same processing. In reality, only one such tab / window (in further text "process") needs to do the processing. Therefore, just using a concept like a method call to getSession() is not advisable.

With this change, the internals of the library use a new function called _useSession() which expects a callback to be provided. So long as the callback's promise has not resolved, the session is "being used." In follow up PRs, locking semantics will be used to ensure that only one _useSession callback is active amongst all processes.

The existing getSession() computation is put behind __loadSession which should not be used outside of _useSession(). A debug-mode stack check is performed to make sure that issues like that are caught.

This PR also adds two new utility functions stackGuard and isInStackGuard. These can be used to detect recursive calls or inappropriate use of methods. For example, in this PR a stack guard is used at debug time to make sure that __loadSession is only called within _useSession, as that is the only valid use of the method. In follow up versions this will become a runtime exception.

It works because when using async/await JS engines like browsers and Node will respect stack traces. A stack guard is a special string that shows up in the stack trace of the Error object, which if seen (by isInStackGuard) can be used to detect whether a function was called from the correct parent-caller or whether there is a recursive call.

It's not possible to use plain functions because of JS minification, which mangles the name of virtually all functions, so a trick has to be used to dynamically generate a function name which is the special stack guard string.

src/GoTrueClient.ts Outdated Show resolved Hide resolved
src/GoTrueClient.ts Outdated Show resolved Hide resolved
src/GoTrueClient.ts Outdated Show resolved Hide resolved
src/GoTrueClient.ts Outdated Show resolved Hide resolved
@hf hf force-pushed the hf/refactor-use-session branch 8 times, most recently from a364b49 to 7f49bd5 Compare July 12, 2023 08:46
@J0 J0 self-requested a review July 12, 2023 09:00
src/lib/helpers.ts Outdated Show resolved Hide resolved
src/lib/helpers.ts Outdated Show resolved Hide resolved
@hf hf force-pushed the hf/refactor-use-session branch from 7f49bd5 to 58499ad Compare July 12, 2023 12:21
@hf hf merged commit ce5ae82 into master Jul 13, 2023
2 checks passed
@hf hf deleted the hf/refactor-use-session branch July 13, 2023 13:29
@github-actions
Copy link
Contributor

🎉 This PR is included in version 2.41.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

hf added a commit that referenced this pull request Jul 13, 2023
hf added a commit that referenced this pull request Jul 13, 2023
hf added a commit that referenced this pull request Jul 18, 2023
There were 2 issues with #726 which was reverted with #732:
- By accident a line was deleted in `#_initialize` that saved the
session.
- There was a recursive-wait of `#_initialize` in certain cases. 

This PR is the same change with some tweaks to resolve the issues. It
has been tested locally. Please test before approving!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants