Skip to content

Wait for guest user setup before firing authenticated API calls#1703

Open
aleksandar-apostolov wants to merge 1 commit into
developfrom
aleksandarapostolov/and-1202-guest-user-type-is-broken
Open

Wait for guest user setup before firing authenticated API calls#1703
aleksandar-apostolov wants to merge 1 commit into
developfrom
aleksandarapostolov/and-1202-guest-user-type-is-broken

Conversation

@aleksandar-apostolov
Copy link
Copy Markdown
Contributor

@aleksandar-apostolov aleksandar-apostolov commented Jun 3, 2026

Goal

Closes AND-1202 — push notifications never arrived for guest users on Android.

StreamVideoBuilder.build() returns immediately while setupGuestUser runs in the background. If client.createDevice(pushDevice) (or any other authenticated API call) fires before that completes, the request goes out with no Authorization header and stream-auth-type: anonymous, so the backend silently registers it against !anon instead of the guest's identity. iOS doesn't hit this because integrators typically call setDevice from the PushKit callback, after connectTask has finished. RN doesn't hit it because device registration reads _user only after connectUser(response.user, ...).

Implementation

  • apiCall now awaits guestUserJob before invoking the request block.
  • A self-job guard (currentCoroutineContext()[Job] !== job) skips the await when createGuestUser is itself running inside guestUserJob, so it doesn't await its own enclosing Deferred and deadlock.

Testing

  • New tests in StreamVideoClientTest:
    • apiCall waits for guestUserJob to complete before invoking the block
    • apiCall does not deadlock when invoked from within guestUserJob

@aleksandar-apostolov aleksandar-apostolov requested a review from a team as a code owner June 3, 2026 07:37
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 3, 2026

PR checklist ✅

All required conditions are satisfied:

  • Title length is OK (or ignored by label).
  • At least one pr: label exists.
  • Sections ### Goal, ### Implementation, and ### Testing are filled, or the PR is bot-authored.
  • An issue is linked (Linear ticket or GitHub issue), or the PR is bot-authored.

🎉 Great job! This PR is ready for review.

@aleksandar-apostolov aleksandar-apostolov force-pushed the aleksandarapostolov/and-1202-guest-user-type-is-broken branch from 6b86dcd to ba62c23 Compare June 3, 2026 07:38
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 3, 2026

Review Change Stack

Walkthrough

StreamVideoClient now synchronizes API calls with guest user setup. The apiCall method waits for a pending guestUserJob to complete before executing requests, but skips the wait when called from within the guest setup coroutine itself to prevent deadlocks. Two regression tests verify this behavior in both blocking and non-blocking scenarios.

Changes

Guest user setup synchronization in API calls

Layer / File(s) Summary
Guest job synchronization in apiCall
stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoClient.kt
Imports Job and currentCoroutineContext to enable coroutine context detection. Updates apiCall to await guestUserJob completion before executing the API request, unless the call is already running inside the guest setup coroutine (detected by checking the current Job context).
Regression tests for apiCall synchronization
stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/StreamVideoClientTest.kt
Adds test-coroutine and reflection utilities, then introduces two regression tests: one verifies apiCall blocks until the reflected guestUserJob completes; the second verifies apiCall does not deadlock when invoked from within the same guest job by coordinating with a lazily-started async task.

🎯 3 (Moderate) | ⏱️ ~20 minutes

🐰 Guest setup awaits with patient Job,
API calls now dance in perfect sync,
No deadlock here—just coroutine harmony,
Racing ahead is yesterday's wink! 🎯

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and accurately summarizes the main change: preventing authenticated API calls from racing ahead of guest user setup.
Description check ✅ Passed The description covers Goal, Implementation, and Testing sections matching the template. Goal explains the issue (AND-1202), Implementation describes the fix with deadlock prevention, and Testing documents the new regression tests.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch aleksandarapostolov/and-1202-guest-user-type-is-broken

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoClient.kt`:
- Around line 294-296: The current use of runCatching around awaiting
guestUserJob swallows failures from setupGuestUser; change the guestUserJob
handling so failures propagate instead of being ignored—i.e., keep the existing
guard (guestUserJob?.takeIf { currentCoroutineContext()[Job] !== it }?.let { job
-> ... }) but replace runCatching { job.await() } with a direct await that will
throw on error (or explicitly rethrow the exception), so the guest-auth gate
behaves like connectAsync/Call.join and the apiCall cannot proceed with an
invalid/anonymous token when setupGuestUser fails.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 42dfb45b-c8a7-47b9-ace9-372ac484a01e

📥 Commits

Reviewing files that changed from the base of the PR and between 7ce2a23 and 6b86dcd.

📒 Files selected for processing (2)
  • stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoClient.kt
  • stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/StreamVideoClientTest.kt

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 3, 2026

SDK Size Comparison 📏

SDK Before After Difference Status
stream-video-android-core 12.21 MB 12.21 MB 0.00 MB 🟢
stream-video-android-ui-xml 5.68 MB 5.66 MB -0.02 MB 🚀
stream-video-android-ui-compose 6.28 MB 6.28 MB 0.00 MB 🟢

@aleksandar-apostolov aleksandar-apostolov changed the title fix(core): wait for guest user setup before authenticated API calls Wait for guest user setup before firing authenticated API calls Jun 3, 2026
@aleksandar-apostolov aleksandar-apostolov added the pr:bug Fixes a bug label Jun 3, 2026
@aleksandar-apostolov aleksandar-apostolov marked this pull request as draft June 3, 2026 07:47
… device registration

Guest user setup runs asynchronously: StreamVideoBuilder.build returns immediately
while setupGuestUser kicks off a background createGuest call to fetch the JWT.
Any authenticated request that fires in that window goes out with stream-auth-type
"anonymous" and no Authorization header, so the backend silently registers it
against the wrong identity. The customer-visible effect is push device
registration succeeding under !anon and incoming-call pushes never reaching
the guest user.

apiCall now awaits guestUserJob before invoking the request block, with a
self-job guard so createGuestUser — which also goes through apiCall — does not
await its own enclosing job and deadlock.

Adds two regression tests: one for the wait, one for the deadlock guard.

AND-1202
@aleksandar-apostolov aleksandar-apostolov force-pushed the aleksandarapostolov/and-1202-guest-user-type-is-broken branch from ba62c23 to f13c6a4 Compare June 3, 2026 07:50
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jun 3, 2026

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

Labels

pr:bug Fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant