Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions common/urltest/urltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ func URLTest(ctx context.Context, link string, detour N.Dialer) (t uint16, err e
return
}
defer instance.Close()
// Set hard read deadline: context cancellation does not interrupt
// net.Conn.Read() on connections from custom DialContext.
// Use relative timeout (not ctx.Deadline) because the context
// deadline includes DialContext time already consumed.
instance.SetReadDeadline(time.Now().Add(C.TCPTimeout))
if N.NeedHandshakeForWrite(instance) {
start = time.Now()
}
Expand Down
28 changes: 25 additions & 3 deletions protocol/group/urltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (g *URLTestGroup) urlTest(ctx context.Context, force bool) (map[string]uint
return result, nil
}
defer g.checking.Store(false)
b, _ := batch.New(ctx, batch.WithConcurrencyNum[any](10))
b, batchCtx := batch.New(ctx, batch.WithConcurrencyNum[any](10))
checked := make(map[string]bool)
var resultAccess sync.Mutex
for _, detour := range g.outbounds {
Expand All @@ -383,7 +383,7 @@ func (g *URLTestGroup) urlTest(ctx context.Context, force bool) (map[string]uint
continue
}
b.Go(realTag, func() (any, error) {
testCtx, cancel := context.WithTimeout(g.ctx, C.TCPTimeout)
testCtx, cancel := context.WithTimeout(batchCtx, C.TCPTimeout)
defer cancel()
t, err := urltest.URLTest(testCtx, g.link, p)
if err != nil {
Expand All @@ -402,7 +402,29 @@ func (g *URLTestGroup) urlTest(ctx context.Context, force bool) (map[string]uint
return nil, nil
})
}
b.Wait()
waitDone := make(chan struct{})
go func() {
b.Wait()
close(waitDone)
}()
timer := time.NewTimer(2 * C.TCPTimeout)
defer timer.Stop()
var timedOut bool
select {
case <-waitDone:
case <-timer.C:
timedOut = true
g.logger.Debug("urltest batch timed out, proceeding with available results")
}
if timedOut {
resultAccess.Lock()
for tag := range checked {
if _, ok := result[tag]; !ok {
g.history.DeleteURLTestHistory(tag)
}
}
resultAccess.Unlock()
}
g.performUpdateCheck()
return result, nil
}
Expand Down