fix: useattendance races #3854
Open
ionfwsrijan wants to merge 4 commits into
Open
Conversation
added 4 commits
June 23, 2026 13:56
- Create lib/api-routes-config.js as the single source of truth - Includes missing /api/upload/certificate and /api/achievements rules - rbac-policy.js re-exports from shared config - rbac.js imports from shared config instead of rbac-policy - middleware.js imports from shared config, removing inline definitions
…jections
- Import getAuth from firebase/auth (was missing, causing ReferenceError)
- Replace getOutboxRecords with getPendingActions (already imported from offlineStore)
- Replace removeFromOutbox with removePendingAction (already imported from offlineStore)
- Wrap module-level window.addEventListener('online') in setupNetworkListener()
with explicit cleanup function; register HMR dispose hook to prevent leak
- Attach .catch() handlers to unhandled syncAttendanceQueue() promise
in registerBackgroundSync fallback paths
- Pass AbortController signals through all async fetchers and check signal.aborted before setState calls - Fix broken AbortController pattern in fetchGamification (controller was created inside callback but never accessible to the effect's cleanup; now created in the effect and passed in) - Add stale-guard checks to fetchStudentActivity, fetchTodayAttendanceStats, and loadMoreRequests to prevent setState after unmount - Handle AbortError gracefully in all fetch paths - Ensure loadingRequests is not reset if the request was aborted
Contributor
Author
|
@Premshaw23 Please review this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Multiple async fetchers in
useAttendancecould callsetStateafter the componentunmounted (or after a re-render with different deps), causing React state-update warnings
and stale data display. The
fetchGamificationcallback had a brokenAbortControllerpattern — the controller was created inside the callback but never accessible to the
effect's cleanup function.
Changes
hooks/useAttendance.jsAll async fetchers (
fetchStudentActivity,fetchGamification,fetchTodayAttendanceStats,loadMoreRequests) now accept an optionalAbortSignalparameter and checksignal.abortedbefore callingsetStateStudent effect: creates a single
AbortController, passes its signal to bothfetchers, and calls
controller.abort()in the cleanupTeacher stats effect: same pattern —
AbortControllercreated in the effect,passed to
fetchTodayAttendanceStats, aborted on cleanupfetchGamification: removed the broken internalAbortController/ returnof cleanup function from
useCallback(React ignores the return value ofcallbacks); controller ownership moved to the effect
fetchTodayAttendanceStats: addedsignal?.abortedguard beforesetAttendanceStatsloadMoreRequests: passessignaltoapiFetch, guardssetAttendanceRequests,and avoids resetting
loadingRequestswhen abortedAll
catchblocks now check forAbortErrorand skip error logging when thefetch was intentionally cancelled
Closes [Bug] Race Conditions in useAttendance Hook: Stale State Overwrites, Duplicate Firestore Queries Bypassing Pool, and Data Inconsistency Window #3851