Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should cancelling a context interrupt an ongoing operation? #432

Open
wayan opened this issue Apr 8, 2023 · 0 comments
Open

Should cancelling a context interrupt an ongoing operation? #432

wayan opened this issue Apr 8, 2023 · 0 comments

Comments

@wayan
Copy link

wayan commented Apr 8, 2023

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)
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant