Should cancelling a context interrupt an ongoing operation?
wayan opened this issue · 0 comments
wayan commented
I try to interrupt ongoing SNMP GET operation (on unreachable Target). I pass a Context to GoSNMP struct and then cancel the context, but the cancel is ignored and the operation continues until timeout - code below takes approx 25 seconds to finish instead of 2 seconds.
Is this behaviour a feature? If so, how can I interrupt ongoing operation (in case of shutdown for example)? By closing the snmp connection?
package main
import (
"context"
"fmt"
"log"
"time"
g "github.com/gosnmp/gosnmp"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(2 * time.Second)
fmt.Println("Cancelling")
cancel()
}()
g.Default.Target = "192.168.1.10"
g.Default.Timeout = 25 * time.Second
g.Default.Retries = 0
g.Default.Context = ctx
err := g.Default.Connect()
if err != nil {
log.Fatalf("Connect() err: %v", err)
}
defer g.Default.Conn.Close()
_, err = g.Default.Get([]string{"1.3.6.1.2.1.1.4.0"})
if err != nil {
log.Fatalf("Get() err: %v", err)
}
}