AlexStocks commented on code in PR #3826:
URL: https://github.com/apache/dubbo-go/pull/3826#discussion_r4596332525


##########
remoting/getty/getty_server.go:
##########
@@ -137,6 +145,9 @@ type Server struct {
 
 // NewServer create a new Server
 func NewServer(url *common.URL, handlers func(*invocation.RPCInvocation) 
result.RPCResult) *Server {
+       srvConfLock.Lock()

Review Comment:
   Adding a recomputable evidence boundary for the severity call.
   
   **The nested locking itself is real**: `NewServer` 
(`getty_server.go:147-148`) takes `srvConfLock.Lock()` and then calls 
`initServer`, which runs `yaml.Marshal(gettyServerConfig)` at `:100`, 
`yaml.Unmarshal(..., srvConf)` at `:104`, and `CheckValidity()` at `:110`. 
`GetServerConfig()` (`:129`) takes `srvConfLock.RLock()`, and 
`SetServerConfig()` (`:116-117`) takes the write lock. `sync.RWMutex` is not 
reentrant, so taking the read lock on the same goroutine while holding the 
write lock self-deadlocks — the mechanism checks out.
   
   **But the default path cannot trigger it**: across the whole tree, `grep -rn 
"GetServerConfig" --include=*.go` yields only `getty_server.go:128-129` (the 
doc comment and the definition) — **no call sites at all**; the only external 
call inside `initServer`, `commonCfg.EnsureApplicationAttribute` 
(`common/config/utils.go:121`), never touches this lock. The deadlock therefore 
requires a caller to put a custom `MarshalYAML`/`UnmarshalYAML` into 
`ProtocolConfig.Params` and call back into 
`GetServerConfig()`/`SetServerConfig()`. Based on trigger conditions and blast 
radius, I would lean P2 (a reentrancy trap in the API contract) rather than a 
blocker on the default startup path.
   
   **To eliminate this class of problem entirely**, narrow the critical section 
to the assignment itself instead of relying on caller discipline:
   
   ```go
   // initServer returns a local copy instead of mutating the global directly
   conf := *srvConf            // lock-free snapshot, or pass it in from the 
caller
   initServer(url, &conf)      // yaml.Marshal/Unmarshal happen entirely 
outside the lock
   srvConfLock.Lock()
   srvConf = &conf
   srvConfLock.Unlock()
   ```
   
   That way, even if a type inside `Params` calls back into `GetServerConfig()` 
during serialization, it is just an ordinary read lock and cannot self-deadlock 
against the write lock. It also moves "yaml-serializing user-controlled data" 
out of the global write lock, so concurrent `NewServer` calls no longer 
serialize against each other.
   
   **I independently verified that the PR's main fix is effective**: after the 
hot paths `Read`/`Write`/`Start` switched to the per-server snapshot `s.conf` 
(`readwriter.go:134`, `getty_server.go:237-239`), `go test -race -count=1 
./remoting/getty/` passes (`ok ... 5.400s`), and concurrent rewrites via 
`SetServerConfig` no longer race with in-flight writes.
   
   Also: `GetServerConfig()` returns a shallow copy of `*srvConf`, and 
`TLSBuilder` is a `*getty.ServerTlsConfigBuilder` pointer, so snapshots still 
share the same builder object; that is already covered by your existing thread 
at `getty_server.go:239`, so I am not opening a separate thread for it.



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