Skip to content

fix(console): propagate timeoutMs to Dubbo and Triple generic invoke client - #1655

Open
dodjdnh wants to merge 3 commits into
apache:developfrom
dodjdnh:fix/generic-invoke-timeout-propagation
Open

dodjdnh wants to merge 3 commits into
apache:developfrom
dodjdnh:fix/generic-invoke-timeout-propagation

Conversation

@dodjdnh

@dodjdnh dodjdnh commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Please provide a description of this PR:

Problem

在泛化调用(POST /api/v1/service/generic/invoke)中,pkg/console/service/service_generic_invoke.go 虽为 callCtx 设置了 context.WithTimeout,但在创建 dubbo-go Client / GenericService 时未传入请求超时参数。由于 Dubbo 协议的 DubboInvoker 依据 Reference URL 的 timeout 参数控制超时(默认 3s),导致单次请求的 timeoutMs 在小于 3s 时不超时、大于 3s 时被提前截断。

Changes

  • 在 genericInvocation 补充 Timeout 字段,并由 InvokeServiceGeneric 传入请求 timeoutMs。
  • 新增 resolveGenericInvokeTimeout 与 ceilTimeoutToMillisecond:优先检查 callCtx.Err()(已取消或已过期则在建连前短路返回),结合 callCtx.Deadline() 与 invocation.Timeout 取较小剩余预算,并将正超时按整毫秒向上取整(避免亚毫秒被 Dubbo 截断为 "0" 或向 Triple HTTP/2 header 写入非 ASCII 字符 µ)。
  • 在 invokeGenericServiceRPC 中将有效超时同时透传至 client.WithClientRequestTimeout 与 client.WithRequestTimeout,覆盖默认 3s。
  • 行为变更(直连集群改为 failfast):为直连引用显式设置 client.WithClusterFailFast(),禁用默认 failover 重试。原因:固定 SDK 版本中 DubboInvoker 首次超时后会将 timeout attachment 改写为裸毫秒字符串,触发重试时会因 time.ParseDuration 解析失败而回落到 3s,导致 timeoutMs < 3s 仍可能意外成功,同时避免对非幂等调试方法重复调用或使总耗时超过 timeoutMs。
  • 建连阶段 deadline 感知与资源清理:通过 newGenericServiceWithContext 使 cli.NewGenericService 遵循请求 deadline,避免被 SDK 独立连接超时阻塞;并通过 ReferenceOption 捕获 *client.ReferenceOptions,在正常返回、超时遗弃、取消或 panic 时均调用 GetInvoker().Destroy() 释放底层 Invoker/连接。
  • 防护 timeout attachment 覆盖:在 InvokeServiceGeneric 和 applyGenericInvokeTimeoutAttachment 中克隆 attachment map、剥离大小写不敏感的 timeout 键,并在 svc.Invoke 前写入最新剩余预算,防止用户 attachments 绕过 timeoutMs。
  • 补充单元测试与进程内真实 Dubbo / Triple 协议延迟响应测试(< 3s 超时且单次尝试、attachments["timeout"] 无法绕过、> 3s 突破 SDK 默认 3s 成功,以及超时/取消后 Invoker.Destroy() 清理断言)。

Verification

  • 提交版本:24a65e4
  • gofmt -l & git diff --check:PASS
  • go vet ./pkg/console/service ./pkg/console/handler:PASS
  • go test -count=1 ./pkg/console/service ./pkg/console/handler:PASS(service 9.127s、handler 0.504s,含真实 Dubbo/Triple 协议延迟用例与超时释放断言)
  • 真实 Go Provider K8s E2E(RUN_DUBBO_GO_E2E=1):NOT VERIFIED(本轮通过进程内真实 Dubbo/Triple 协议端到端单测覆盖)

Related Issues

To help us figure out who should review this PR, please put an X in all the areas that this PR affects.

  • Docs
  • Installation
  • User Experience
  • Dubboctl
  • Console
  • Core Component

…client

Signed-off-by: Kevin <yaojiaangyao@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Dubbo calls can exceed the requested deadline because failover retries, synchronous reference creation, and unfiltered timeout attachments bypass the effective request budget.

3 open issues
What changed in this PR

This PR addresses #1654 by passing the Console generic-invocation timeout into Dubbo and Triple client configuration, rather than relying only on the Go context.

Changes:

  • Carries timeoutMs through the invocation and applies client/reference timeout options.
  • Resolves remaining deadline budgets, rejects canceled or expired contexts before setup, and rounds positive timeouts to whole milliseconds.
  • Adds timeout boundary tests and SDK-generated Reference URL checks.
File Description
pkg/​console/​service/​service_generic_invoke.go Resolves and propagates per-request SDK timeouts.
pkg/​console/​service/​service_generic_invoke_validation_test.go Tests timeout resolution and Dubbo/Triple Reference URL settings.

Review effort: Max

client.WithProtocol(invocation.Protocol),
client.WithSerialization(invocation.Serialization),
)
}
}

var invokeGenericServiceRPC = func(callCtx context.Context, invocation genericInvocation) (any, error) {
timeout, err := resolveGenericInvokeTimeout(callCtx, invocation)
}
if timeout > 0 {
clientOpts = append(clientOpts, client.WithClientRequestTimeout(timeout))
referenceOpts = append(referenceOpts, client.WithRequestTimeout(timeout))
…ing budget

Signed-off-by: Kevin <yaojiaangyao@gmail.com>

@robocanic robocanic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review:#1655 fix(console): propagate timeoutMs to Dubbo and Triple generic invoke client

已对 head 511e5334 的全部三个变更文件做了审查,并对照 PR 所用 dubbo-go 版本(v3.0.0-20260210015753-35ea886421f9)的 DubboInvoker.getTimeout / Triple applyDefaultTimeout 源码核对了超时透传链路。

Strengths

  • 根因分析准确:同时通过 client.WithClientRequestTimeout / client.WithRequestTimeout 设置 Reference URL 的 timeout,并通过 invocation attachment 覆盖,覆盖了 Dubbo(getTimeout 用 time.ParseDuration 解析 attachment)和 Triple(TimeoutKey{} 走 ParseDuration)两条路径,格式 timeout.String() 与 SDK 解析一致。
  • resolveGenericInvokeTimeout 对已取消/已过期 context 提前短路,并向上取整到毫秒,规避亚毫秒被截断为 "0" 的边界问题。
  • 剥离用户传入的 timeout attachment(大小写不敏感),防止其绕过 timeoutMs;applyGenericInvokeTimeoutAttachment 拷贝 map 而不修改调用方数据。
  • 测试既有基于 recorder 的 Reference URL 断言,也有真实 Dubbo/Triple 协议端到端的超时用例,覆盖面好。

Critical

未发现。

Important

  1. newGenericServiceWithContext 在 ctx 到期后遗弃 goroutine 及其创建的 Reference,造成资源泄漏

    • 位置:pkg/console/service/service_generic_invoke.go,newGenericServiceWithContext(select 的 <-ctx.Done() 分支,以及 res := <-resultCh 后 ctx.Err() != nil 的分支)。
    • 问题:cli.NewGenericService 本身不可取消。ctx 先到期时函数直接返回,后台 goroutine 仍继续建连,最终产生的 *GenericService(含 Invoker / 连接)无人持有、永远不会 Destroy;第二个分支甚至已经拿到了 res.svc 却直接丢弃。对不可达/慢速目标,每次超时的泛化调用都会残留一个 goroutine 和一个未释放的连接/Reference,Console 长期运行会累积。
    • 建议:在两个丢弃分支中,让 goroutine(或 ctx.Err() 分支)负责在 res.svc != nil 时调用其 Invoker/Reference 的销毁方法(或在 goroutine 内 select 检测 ctx 已完成后自行销毁);同时落实文件中已有的「缓存/复用 client」TODO,避免每次调用都新建 dubbo.Instance。至少应补一个用例验证超时后被丢弃的 service 会被清理。
  2. 未在 PR 描述中说明的行为变更:新增 client.WithClusterFailFast()

    • 位置:invokeGenericServiceRPC 中的 referenceOpts。
    • 问题:此前使用 SDK 默认的 failover 集群(默认重试),现在改为 fail-fast,即泛化调用失败后不再重试。这对非幂等方法是合理的,也让「总耗时 ≤ timeoutMs」成立,但它不属于 issue #1654 的范围,PR 描述的 Changes 中也没有提及,评审与后续排障者容易遗漏。
    • 建议:在 PR 描述和提交说明中明确写出该变更及原因(避免 failover 重试使总时长超过 timeoutMs 或重复触发非幂等调用);若不想改变重试语义,则去掉该选项。
  3. 新增的真实协议测试偏慢且有时序/端口竞争风险

    • 位置:service_generic_invoke_timeout_test.go(reserveLocalTCPPort 先 Close 再由 SDK 重新绑定;above-3s-succeeds-beyond-sdk-default 每个协议固定 sleep 3.2s;isGenericInvokeTimeoutErr 以字符串包含 "timeout"/"deadline" 判定)。
    • 问题:先释放端口再重绑存在被其他进程抢占的 TOCTOU 窗口;elapsed < 2s、800ms 延迟等基于墙钟的断言在 CI 负载下可能抖动;字符串匹配过宽,可能把「连接超时」等无关错误也判为通过,削弱对本修复的回归保护。
    • 建议:优先让 SDK/监听器直接使用 :0 并读取实际端口(或在 export 失败时重试);将超时错误判定收窄为 context.DeadlineExceeded 或 tri.CodeDeadlineExceeded;适当放宽时间阈值。

Assessment

Ready to merge? With fixes

Reasoning: 超时透传的核心逻辑与 SDK 行为一致、边界处理周全,未发现会导致功能错误的 Critical 问题;但 ctx 到期后遗弃 goroutine/Reference 的资源泄漏应当修复,FailFast 的行为变更需要在描述中说明,测试的稳定性也建议加固。

…timeout tests

Signed-off-by: Kevin <yaojiaangyao@gmail.com>
@sonarqubecloud

sonarqubecloud Bot commented Dec 6, 2026

Copy link
Copy Markdown

@dodjdnh

dodjdnh commented Dec 6, 2026

Copy link
Copy Markdown
Contributor Author

@robocanic 感谢细致的 review!三条建议均已在 24a65e4 中处理并补充验证:

  1. 超时/取消后清理遗弃 Reference:在 newGenericServiceWithContext 中通过 ReferenceOption 捕获 *client.ReferenceOptions,使用无缓冲 channel + doneCh 明确所有权交接——无论是超时遗弃(后台 goroutine 在 <-doneCh 分支自行销毁)、Refer 返回瞬间已取消、panic,还是正常调用结束(defer cleanup()),都会调用 GetInvoker().Destroy() 释放底层 Invoker/连接。已在 dubbo/slow-refer-aborts-at-deadline 与 dubbo/rejects-canceled-after-refer 中补充 Destroy() 计数断言(== 1)。
  2. 补充 WithClusterFailFast() 行为变更说明:已更新 PR 描述,明确列出将直连引用改为 fail-fast 的原因(避免 pinned SDK 在 Dubbo 首次超时后将 timeout attachment 改写为裸毫秒导致重试回落 3s,同时避免超出 timeoutMs 预算或重复触发非幂等调用)。
  3. 加固真实协议超时测试:在 exportDelayedGenericServer 中为端口绑定增加最多 5 次重试规避 TOCTOU;将超时错误判定收窄(Triple 仅匹配 context.DeadlineExceeded / tri.CodeDeadlineExceeded,Dubbo 仅匹配 context.DeadlineExceeded / getty 精确的 "client read timeout",并配合 attempts == 1 排除建连失败误判);适当放宽墙钟上限至 2.5s。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Generic invoke does not propagate timeoutMs to Dubbo client, causing per-request timeout to fall back to 3s

4 participants