Aias00 opened a new pull request, #3519:
URL: https://github.com/apache/dubbo-go/pull/3519

   ## What
   
   `ServiceMappingChangedListenerImpl.Stop` writes the `stop` field **without** 
`lstn.mux`, while `OnEvent` reads `stop` **under** `lstn.mux`. The unlocked 
write races `OnEvent` (data race on the `int` field) and lets a mapping event 
be processed after the listener has been stopped.
   
   ## Why
   
   ```go
   // registry/servicediscovery/service_mapping_change_listener_impl.go
   func (lstn *ServiceMappingChangedListenerImpl) OnEvent(e observer.Event) 
error {
       lstn.mux.Lock()
       defer lstn.mux.Unlock()
       if lstn.stop == ServiceMappingListenerStop { ... }   // read under mux
       ...
   }
   
   func (lstn *ServiceMappingChangedListenerImpl) Stop() {
       lstn.stop = ServiceMappingListenerStop                 // write WITHOUT 
mux
   }
   ```
   
   `Stop` is reached from `serviceDiscoveryRegistry.stopListen` (under the 
registry's `s.lock`, not `lstn.mux`), so a concurrent `UnSubscribe` and a 
`ServiceMappingChangedEvent` delivered to `OnEvent` race on `stop`. `go test 
-race` flags it.
   
   ## Fix
   
   Acquire `lstn.mux` in `Stop` before writing `stop`. `Stop` is not called 
re-entrantly from `OnEvent`'s held-`mux` call stack (it is reached via 
`UnSubscribe`/`stopListen`, independent of the `OnEvent` → `SubscribeURL` 
path), so there is no self-deadlock.
   
   ## Tests
   
   Added `TestServiceMappingChangedListener_StopRace`: 100 rounds of concurrent 
`Stop()` vs `OnEvent(nil)` (which acquires `mux`, reads `stop`, early-returns), 
passing under `-race`.
   
   Fixes #3518


-- 
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]

Reply via email to