AlexStocks commented on code in PR #3833:
URL: https://github.com/apache/dubbo-go/pull/3833#discussion_r4596343468
##########
remoting/getty/listener.go:
##########
@@ -272,6 +272,9 @@ func (h *RpcServerHandler) OnMessage(session getty.Session,
pkg any) {
attachments[constant.LocalAddr] = session.LocalAddr()
attachments[constant.RemoteAddr] = session.RemoteAddr()
+ reqCancel := h.bindRequestContext(session, invoc)
Review Comment:
按当前 head `cda0caec` 复核,这条判断成立,补三处可核对的坐标。
**生命周期确实越过了写回**:`listener.go:275-276` 的 `reqCancel :=
h.bindRequestContext(...)` / `defer reqCancel()` 只在 `OnMessage`
返回时执行,而双向路径的执行顺序是 `:278` `requestHandler` → `:284` `reply(session,
resp)`。`reply`(`:329`)内部要做编码与 session 写入,阻塞多久,handler 派生的 goroutine 就能看到多久的有效
context,上限是 session 写超时或 `OnCron` 判定超时。所以「handler 返回即释放」目前只对单向请求成立。
**测试覆盖也如你所说只到单向**:`listener_request_context_test.go:88` 显式设置 `req.TwoWay =
false`,整套用例没有走 `:278 → :284` 之后的取消时序。
**改法建议**(`defer` 需做成一次性,否则提前调用后仍会在返回时重复调用):
```go
var once sync.Once
cancel := func() { once.Do(reqCancel) }
defer cancel()
result := h.server.requestHandler(invoc)
cancel() // handler 返回即释放
if !req.TwoWay { return }
resp.Result = result
reply(session, resp)
```
有一个前提需要确认后再落地:提前取消会让 `reply` 与其后的编码阶段看不到请求 context。若响应编码链路或
observation/tracing 需要从 `invoc` 派生 span,取消时机会提前收尾这些 span(`reply` 本身只接收
`session`/`resp`,不接收 ctx,所以只要编码器不回溯 invoc 的 context 就是安全的)。建议在改的同时补一条双向用例:在
`reply` 之前用 `session.Close()` 或写阻塞的 fake session 制造阻滞,断言 handler 返回后
`<-ctx.Done()` 立即就绪。
--
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]