Skip to content

Fix race condition in notes-data.json read-modify-write - #301

Open
anshul23102 wants to merge 1 commit into
mugenkyou:mainfrom
anshul23102:fix/289-race-condition
Open

Fix race condition in notes-data.json read-modify-write#301
anshul23102 wants to merge 1 commit into
mugenkyou:mainfrom
anshul23102:fix/289-race-condition

Conversation

@anshul23102

@anshul23102 anshul23102 commented Jun 5, 2026

Copy link
Copy Markdown

Add threading lock to protect concurrent access to notes-data.json, preventing silent data loss when multiple uploads/deletes happen simultaneously.

Problem

When two upload or delete requests arrive concurrently:

  1. Request A reads notes-data.json
  2. Request B reads the same version (before A writes)
  3. Request A writes its changes
  4. Request B writes its changes (silently overwriting A's data)
  5. Result: A's changes are lost with no error returned

Solution

  • Add module-level _notes_lock = threading.Lock()
  • Wrap read-modify-write operations in both functions with the lock
  • File uploads happen outside the lock for performance

Changes

  • app.py: Added threading lock protection to critical sections
  • Both admin_upload() and delete_material() now use with _notes_lock:
  • Lock ensures only one thread reads and writes at a time

Testing

  • Concurrent uploads: Each upload's metadata is preserved
  • Concurrent deletes: Each delete's JSON update is applied atomically
  • File operations succeed without data corruption

Resolves #289

Work completed under GSSoC'26

Add module-level threading lock to protect concurrent access to notes-data.json
in admin_upload() and delete_material() endpoints. Without synchronization,
concurrent requests can cause race conditions where the second write silently
overwrites the first, leading to data loss.

- Add _notes_lock at module level using threading.Lock()
- Wrap all read-modify-write operations in admin_upload() with the lock
- Wrap all read-modify-write operations in delete_material() with the lock
- File uploads happen outside the lock for better performance

This prevents silent data loss when multiple lecturers upload or delete notes
simultaneously.

Resolves mugenkyou#289
@github-actions github-actions Bot added awaiting review enhancement New feature or request level-1 Beginner issue; basic HTML/CSS or docs updates, good for first-time contributors nsoc26 labels Jun 5, 2026
@github-actions
github-actions Bot requested a review from mugenkyou June 5, 2026 13:11
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown

📥 Pull Request Received

Thank you for your contribution to College Daddy. Your pull request has been received and is currently under review.

🔗 Linked Issues

✅ Pre-Merge Checklist

Please ensure the following requirements are met:

  • Changes adhere to contribution guidelines
  • Related issues are referenced in description
  • All changes tested and verified locally
  • Code follows project standards

👤 Reviewer Assigned

@mugenkyou has been assigned to review this pull request.


Our team will review your submission shortly. We appreciate your effort in improving the platform for students.

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

Labels

awaiting review enhancement New feature or request level-1 Beginner issue; basic HTML/CSS or docs updates, good for first-time contributors nsoc26

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][Level 2] app.py: concurrent uploads and deletes race on notes-data.json read-modify-write, causing silent data loss

1 participant