jieguo-coder opened a new pull request, #3367: URL: https://github.com/apache/dubbo-go/pull/3367
# Description ## Summary Fixes concurrency safety issues in the application-level metadata path by adding proper synchronization protection for multiple global maps and shared states. ## Background After the #2534 metadata refactor, application-level metadata is maintained through shared states such as local MetadataInfo, metadata report instances, and MetadataService. Currently, multiple core states are maps without clear synchronization protection. Data races, stale reads, or fatal error: concurrent map writes might occur when service registration, unregistration, subscription, unsubscription, instance changes, and metadata service queries happen concurrently. **Related Issue: Fixes #3353** ## Changes 1. Internal Lock Protection for MetadataInfo (metadata/info/metadata_info.go) - Added a sync.RWMutex field to the MetadataInfo struct (with json:"-" and hessian:"-" tags to skip serialization) to protect the three internal maps: Services, exportedServiceURLs, and subscribedServiceURLs. - - AddService / RemoveService / AddSubscribeURL / RemoveSubscribeURL now acquire the write lock. - - GetExportedServiceURLs / GetSubscribedURLs now acquire the read lock. - - Added a new GetServices() method that returns a snapshot of Services under a read lock for safe external iteration. 2. Global registryMetadataInfo Lock Protection (metadata/metadata.go) Added a sync.RWMutex for the global map[string]*MetadataInfo. The get-or-create phase in AddService / AddSubscribeURL is now executed atomically under a write lock to prevent race conditions. GetMetadataInfo is protected by a read lock. 3. Global instances Lock Protection (metadata/report_instance.go) - Added a sync.RWMutex for the global map[string]MetadataReport. - - Extracted an internal helper function getMetadataReportUnsafe to avoid deadlocks caused by the un-reentrant nature of Go's RWMutex during the fallback path of GetMetadataReportByRegistry. 3. Concurrency Safety for Listeners (registry/.../service_instances_changed_listener_impl.go) 4. Added missing mutex protection for AddListenerAndNotify and RemoveListener to prevent concurrent reads/writes on listeners and serviceUrls against OnEvent. 5. Safe Access to Services Field Replaced direct accesses to metadataInfo.Services in OnEvent and convertV2 with the new safe method GetServices(). ## Test Plan - go vet ./metadata/... ./registry/... : Zero warnings. - - golangci-lint run ./metadata/... ./registry/servicediscovery/... : Zero issues. - - go test ./metadata/... ./registry/... : All 21 packages passed. - - go build ./... : Full project compilation passed successfully. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
