AlexStocks commented on code in PR #39:
URL: https://github.com/apache/dubbo-net/pull/39#discussion_r4562098186
##########
NetService/Telnet/ExTelnet.cs:
##########
@@ -512,18 +512,11 @@ public async Task<bool> ConnectAsync(int keepAliveTime =
-1,int telnetAlivePerio
//如果设置过连接超时
if(connectTimeOut>0)
{
- CancellationTokenSource tokenSource = new
CancellationTokenSource();
+ using CancellationTokenSource tokenSource = new
CancellationTokenSource(connectTimeOut);
CancellationToken ct = tokenSource.Token;
- Task cancelTask = Task.Delay(connectTimeOut);
- Task connectTask = mySocket.ConnectAsync(iep,ct).AsTask();
// 带CancellationToken的重载返回的是ValueTask,WhenAny需要Task
-
- //double await so if cancelTask throws exception, this
throws it
- await await Task.WhenAny(connectTask, cancelTask);
- if(!connectTask.IsCompleted)
- {
- tokenSource.Cancel();
- throw new Exception($"Connect timed out for specify
time : {connectTimeOut}ms");
- }
+ // Only the new connection attempt is owned here. The
Standard
+ // fallback aborts it on timeout and releases its
timer/registration.
+ await NetworkCompatibility.ConnectOwnedAsync(mySocket,
iep, ct);
Review Comment:
[P2] 连接超时改用 OperationCanceledException 后,`NowErrorMes` 丢失超时时长且与主动取消不可区分
触发条件:`connectTimeOut > 0` 且连接在该窗口内未建立,即 base 版本里走 `throw new
Exception($"Connect timed out for specify time : {connectTimeOut}ms")` 的那条路径。
实际影响:netstandard2.0 分支由 `NetworkCompatibility.ConnectOwnedAsync` 把超时转成
`OperationCanceledException("Connection cancelled.")`,net8/net10 分支由
`socket.ConnectAsync(endpoint, token)` 抛 `OperationCanceledException`。两者都被本方法的
`catch (Exception ex)`(538–541 行)接住,写入 `nowErrorMes` 并 `ReportMes($"Connect
fail : {ex.Message}")`。于是 `NowErrorMes`(157 行的 public 属性,还会被 1067、1102 行拼进
`IOException` 的 message)从 "Connect timed out for specify time : 1000ms" 退化成
"Connection cancelled."——超时时长消失,且"超时"与"调用方取消"在状态信息和异常里不再可区分。
证据:head 版 `NetService/Telnet/ExTelnet.cs` 515–521 行(`new
CancellationTokenSource(connectTimeOut)` + `ConnectOwnedAsync`)、538–541 行
catch;base 版本此处为显式 `throw new Exception(...)`。
修复方向:把超时与取消分开,例如在 catch 中判断 `tokenSource.IsCancellationRequested`
后再抛出保留原文案的异常;或让 `ConnectOwnedAsync` 在"超时"而非"调用方取消"时抛 `TimeoutException`,与
`TaskCompatibility` 里已经采用的 TimeoutException 语义保持一致。
--
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]