fix: stream channel 提前返回未关闭导致 goroutine 泄漏,服务探测超时下限 500ms

This commit is contained in:
ZacharyZcR
2026-05-13 02:00:31 +08:00
parent f30a153b67
commit 5ada780f6e
+15 -1
View File
@@ -127,6 +127,9 @@ func EnhancedPortScan(ctx context.Context, hosts []string, ports string, timeout
hosts = probeSubnets(ctx, hosts, time.Duration(timeout)*time.Second, session)
if len(hosts) == 0 {
common.LogInfo(i18n.GetText("port_scan_no_alive_subnet"))
if stream != nil {
close(stream)
}
return nil
}
}
@@ -135,6 +138,9 @@ func EnhancedPortScan(ctx context.Context, hosts []string, ports string, timeout
portList := parsers.ParsePort(ports)
if len(portList) == 0 {
common.LogError(i18n.Tr("invalid_port", ports))
if stream != nil {
close(stream)
}
return nil
}
common.LogDebug(fmt.Sprintf("[PortScan] 端口解析完成: %d个端口", len(portList)))
@@ -203,6 +209,9 @@ func EnhancedPortScan(ctx context.Context, hosts []string, ports string, timeout
}, state)
if err != nil {
common.LogError(i18n.Tr("thread_pool_create_failed", err))
if stream != nil {
close(stream)
}
return nil
}
common.LogDebug("[PortScan] 线程池创建成功")
@@ -408,8 +417,13 @@ func scanSinglePort(ctx context.Context, host string, port int, addr string, ada
// 步骤3:服务识别(Scanner负责关闭连接,包括探测中可能创建的新连接)
scanner := NewSmartPortInfoScanner(ctx, host, port, conn, timeout, config, session)
// 服务探测超时自适应:用 RTT 采样值约束读超时上限
// 下限 500ms:服务处理需要时间,不能太激进
if rttTO := adaptiveTO.Timeout(); rttTO < timeout {
scanner.info.maxReadTimeoutMS = int(rttTO.Milliseconds()) * 6
maxMS := int(rttTO.Milliseconds()) * 6
if maxMS < 500 {
maxMS = 500
}
scanner.info.maxReadTimeoutMS = maxMS
}
defer scanner.Close()
serviceInfo, _ := scanner.SmartIdentify()