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:
补充一条可复算的证据边界,用于定级。
**锁嵌套本身成立**:`NewServer`(`getty_server.go:147-148`)取 `srvConfLock.Lock()` 后调用
`initServer`,而 `initServer` 在 `:100` 执行
`yaml.Marshal(gettyServerConfig)`、`:104` 执行 `yaml.Unmarshal(...,
srvConf)`、`:110` 执行 `CheckValidity()`;`GetServerConfig()`(`:129`)取
`srvConfLock.RLock()`,`SetServerConfig()`(`:116-117`)取写锁。`sync.RWMutex` 不可重入,同
goroutine 在持写锁时再取读锁即自死锁——机制无误。
**但默认路径不会触发**:全仓 `grep -rn "GetServerConfig" --include=*.go` 只有
`getty_server.go:128-129`(注释与定义),**没有任何调用点**;`initServer` 内唯一的外部调用
`commonCfg.EnsureApplicationAttribute`(`common/config/utils.go:121`)不触碰该锁。因此死锁需要调用方在
`ProtocolConfig.Params` 里放入自定义 `MarshalYAML`/`UnmarshalYAML` 并回调
`GetServerConfig()`/`SetServerConfig()` 才会发生。按触发条件与影响面,我倾向定 P2(API
契约上的重入陷阱),而非默认启动路径的阻断。
**若要彻底消除这一类问题**,建议把临界区收窄到赋值本身,而不是靠调用方自律:
```go
// initServer 改为返回局部副本,不直接改全局
conf := *srvConf // 无锁读快照,或由调用方传入
initServer(url, &conf) // yaml.Marshal/Unmarshal 全在锁外
srvConfLock.Lock()
srvConf = &conf
srvConfLock.Unlock()
```
这样即使 `Params` 里的类型在序列化过程中回调 `GetServerConfig()`,也只是一次普通读锁,不会与写锁自锁;同时把「yaml
序列化用户可控数据」从全局写锁里移出去,`NewServer` 的并发创建也不再互相串行。
**该 PR 主修复我已独立验证有效**:热路径 `Read`/`Write`/`Start` 改用每服务器快照
`s.conf`(`readwriter.go:134`、`getty_server.go:237-239`)后,`go test -race
-count=1 ./remoting/getty/` 通过(`ok ... 5.400s`),`SetServerConfig` 并发改写不再与在途写回竞争。
另:`GetServerConfig()` 返回 `*srvConf` 是浅拷贝,`TLSBuilder` 为
`*getty.ServerTlsConfigBuilder` 指针,快照之间仍共享同一 builder 对象;这一点已由你在
`getty_server.go:239` 的线程覆盖,此处不另开线程。
--
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]