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

Wrap context flow inside a run func #2

Open
agiacomolli opened this issue May 9, 2024 · 0 comments
Open

Wrap context flow inside a run func #2

agiacomolli opened this issue May 9, 2024 · 0 comments

Comments

@agiacomolli
Copy link
Owner

agiacomolli commented May 9, 2024

These contexts are not handled correctly because of os.Exit():

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

As per docs:

... The program terminates immediately; deferred functions are not run.

Better to wrap the flow inside a run that returns an error:

func main() {
    err := run()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}

func run() error {
    ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
    defer cancel()

    ...

    return nil
}
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