typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

Fix: Add mutex protection for projects map to prevent race conditions

Open Catsayer-Chan opened this issue 3 weeks ago • 3 comments

Summary

This PR adds proper mutex protection to the projects map in the API struct to prevent potential race conditions.

Problem

The projects map in internal/api/api.go was the only map in the API struct without mutex protection, while files, symbols, and types maps were already protected. This inconsistency could lead to data races if the API handles concurrent requests.

Solution

  • Added sync.RWMutex to protect the projects map
  • Applied read locks (RLock) for all read operations (4 locations)
  • Applied write locks (Lock) for write/delete operations (2 locations)
  • Used RWMutex instead of Mutex for better read performance

Testing

  • ✅ Code compiles without errors
  • ✅ Passes race detector: go test -race ./internal/api/...
  • ✅ No functional changes, backward compatible

Modified Files

  • internal/api/api.go

Impact

  • Improves thread safety and code consistency
  • Prepares codebase for concurrent request handling
  • Negligible performance impact in single-threaded scenarios

Author: Catsayer

Catsayer-Chan avatar Dec 06 '25 08:12 Catsayer-Chan