Skip to content

fix: resolve stale closure race condition in face scan timeout#1422

Open
Janeeshareddy wants to merge 4 commits into
viru0909-dev:mainfrom
Janeeshareddy:fix/face-scan-stale-closure-timeout
Open

fix: resolve stale closure race condition in face scan timeout#1422
Janeeshareddy wants to merge 4 commits into
viru0909-dev:mainfrom
Janeeshareddy:fix/face-scan-stale-closure-timeout

Conversation

@Janeeshareddy

Copy link
Copy Markdown
Contributor

Fixes #1413

Root Cause

The original setTimeout inside startFaceDetection closed over step and faceDetected state values at creation time. Even when face detection succeeded and step transitioned to success, the timeout still saw stale values (step === 'scanning', faceDetected === false) and incorrectly fired the error state.

Changes

  • Added timeoutRef (useRef) to store the 45s timeout ID explicitly
  • Added scanActiveRef (useRef) to track if the scan session is still active
  • Cleared timeout immediately inside handleFaceLogin when face is confirmed
  • Added useEffect cleanup to cancel timeout on step change or component unmount
  • Replaced stale state checks in timeout callback with scanActiveRef.current

Benefits

  • Eliminates race condition from stale closures
  • Prevents false error state after successful authentication
  • Cleans up properly on unmount (no memory leaks)

@vercel

vercel Bot commented Jun 18, 2026

Copy link
Copy Markdown

@Janeeshareddy is attempting to deploy a commit to the CodeBlooded's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

Hi @Janeeshareddy, thanks for contributing to Nyay Setu! 🎉

I have automatically:

  • 👤 Assigned this PR to you.
  • 🏷️ Applied the gssoc:approved label.

Our workflows will now analyze your changes to classify:

  • 📈 PR Difficulty: level:*
  • 🧩 PR Type: type:*
  • 🌟 PR Quality: quality:*

Tip

Ensure your PR description references the issue it resolves (e.g. Closes #123). This allows the bot to inherit any additional labels from that issue!

Happy coding! 🚀

@github-actions github-actions Bot added quality:clean level:advanced GSSoC Advance Level labels Jun 18, 2026

@viru0909-dev viru0909-dev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

⚠️ Please Isolate Your Changes

Your fix for the face scan timeout bug using timeoutRef is correct and exactly what we need!

However, this PR accidentally includes over 700 lines of unrelated code for a new 'Lawyer Matching' feature (e.g., LawyerMatchingService, CaseAssignmentController, CaseAssignment.jsx).

Could you please clean up this branch so that it only includes the changes to FaceLoginModal.jsx? We can't merge the lawyer assignment logic as part of this bug fix.

@viru0909-dev viru0909-dev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

PR contains unrelated files that have already been merged (e.g. CaseAssignmentController, LawyerMatchingService). Please rebase your branch on the latest main and only include the FaceLoginModal.jsx changes to resolve the timeout issue cleanly.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Face scan timeout uses stale state and may incorrectly transition successful scans to error state

2 participants