Skip to content

Commit 4382acc

Browse files
committed
fix: set the timeout based on the caller to avoid strange issues
although a ctx with deadline is set in most cases, but passing in context.TODO() can lead to some very strange problems.
1 parent 3d3d4ee commit 4382acc

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

prober/dns.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ func ProbeDNS(ctx context.Context, target string, module config.Module, registry
259259
msg.Question[0] = dns.Question{dns.Fqdn(module.DNS.QueryName), qt, qc}
260260

261261
logger.Info("Making DNS query", "target", targetIP, "dial_protocol", dialProtocol, "query", module.DNS.QueryName, "type", qt, "class", qc)
262-
timeoutDeadline, _ := ctx.Deadline()
263-
client.Timeout = time.Until(timeoutDeadline)
262+
if timeoutDeadline, ok := ctx.Deadline(); ok {
263+
client.Timeout = time.Until(timeoutDeadline)
264+
}
264265
requestStart := time.Now()
265266
response, rtt, err := client.Exchange(msg, targetIP)
266267
// The rtt value returned from client.Exchange includes only the time to

prober/icmp.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,13 @@ func ProbeICMP(ctx context.Context, target string, module config.Module, registr
293293
}
294294

295295
rb := make([]byte, 65536)
296-
deadline, _ := ctx.Deadline()
297-
if icmpConn != nil {
298-
err = icmpConn.SetReadDeadline(deadline)
299-
} else {
300-
err = v4RawConn.SetReadDeadline(deadline)
296+
297+
if deadline, ok := ctx.Deadline(); ok {
298+
if icmpConn != nil {
299+
err = icmpConn.SetReadDeadline(deadline)
300+
} else {
301+
err = v4RawConn.SetReadDeadline(deadline)
302+
}
301303
}
302304
if err != nil {
303305
logger.Error("Error setting socket deadline", "err", err)

prober/tcp.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ func dialTCP(ctx context.Context, target string, module config.Module, registry
8080
// via tlsConfig to enable hostname verification.
8181
tlsConfig.ServerName = targetAddress
8282
}
83-
timeoutDeadline, _ := ctx.Deadline()
84-
dialer.Deadline = timeoutDeadline
83+
84+
if timeoutDeadline, ok := ctx.Deadline(); ok {
85+
dialer.Deadline = timeoutDeadline
86+
}
8587

8688
logger.Info("Dialing TCP with TLS")
8789
return tls.DialWithDialer(dialer, dialProtocol, dialTarget, tlsConfig)
@@ -124,7 +126,6 @@ func ProbeTCP(ctx context.Context, target string, module config.Module, registry
124126
Help: "Indicates if probe failed due to regex",
125127
})
126128
registry.MustRegister(probeFailedDueToRegex)
127-
deadline, _ := ctx.Deadline()
128129

129130
conn, err := dialTCP(ctx, target, module, registry, logger)
130131
if err != nil {
@@ -134,13 +135,13 @@ func ProbeTCP(ctx context.Context, target string, module config.Module, registry
134135
defer conn.Close()
135136
logger.Info("Successfully dialed")
136137

137-
// Set a deadline to prevent the following code from blocking forever.
138-
// If a deadline cannot be set, better fail the probe by returning an error
139-
// now rather than blocking forever.
140-
if err := conn.SetDeadline(deadline); err != nil {
141-
logger.Error("Error setting deadline", "err", err)
142-
return false
138+
if deadline, ok := ctx.Deadline(); ok {
139+
if err := conn.SetDeadline(deadline); err != nil {
140+
logger.Error("Error setting deadline", "err", err)
141+
return false
142+
}
143143
}
144+
144145
if module.TCP.TLS {
145146
state := conn.(*tls.Conn).ConnectionState()
146147
registry.MustRegister(probeSSLEarliestCertExpiry, probeTLSVersion, probeSSLLastChainExpiryTimestampSeconds, probeSSLLastInformation)

0 commit comments

Comments
 (0)