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

Defer is not run if another goroutine panics #14542

Closed
slava-vishnyakov opened this Issue Feb 27, 2016 · 3 comments

Comments

Projects
None yet
3 participants
@slava-vishnyakov

slava-vishnyakov commented Feb 27, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.6 darwin/amd64
  2. What operating system and processor architecture are you using (go env)?
    darwin/amd64
    (also on linux/amd64 and probably others, did not test on Windows)
  3. What did you do?
    (Use play.golang.org to provide a runnable example, if possible.)
    https://play.golang.org/p/KdHIPSSiaX
  4. What did you expect to see?
    Defer called
  5. What did you see instead?
    nothing

This led to a real-life problem where I was running a process via exec.Command (which would have been be killed by defer), then later another goroutine paniced, defer did not run, the service manager restarted the main process and I was stuck with accumulation of infinite number of same programs (that was run via exec.Command) running. ( More of context here, if interested, but not necessary http://stackoverflow.com/questions/34095254/panic-in-other-goroutine-not-stopping-child-process )

@davecheney

This comment has been minimized.

Show comment
Hide comment
@davecheney

davecheney Feb 27, 2016

Contributor

I am sorry but this is working as intended, defer only runs if the function the defer block is declared in returns. If the panic occurs in another goroutine, defer will not run.

See https://golang.org/ref/spec#Defer_statements

A "defer" statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement, reached the end of its function body, or because the corresponding goroutine is panicking.

Contributor

davecheney commented Feb 27, 2016

I am sorry but this is working as intended, defer only runs if the function the defer block is declared in returns. If the panic occurs in another goroutine, defer will not run.

See https://golang.org/ref/spec#Defer_statements

A "defer" statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement, reached the end of its function body, or because the corresponding goroutine is panicking.

@davecheney davecheney closed this Feb 27, 2016

@slava-vishnyakov

This comment has been minimized.

Show comment
Hide comment
@slava-vishnyakov

slava-vishnyakov Feb 27, 2016

Thank you for the explanation.

There a lot of places where unexpected panics could happen. In other languages you could have a global try-catch block in main, which would definitely always run if something bad happens. Frankly I expected the defer-recover in main() to work like that.

Is there another mechanism to recover from such failures or is the only way to deal with it is to add defer-recover into every goroutine?

Thank you for your time!

slava-vishnyakov commented Feb 27, 2016

Thank you for the explanation.

There a lot of places where unexpected panics could happen. In other languages you could have a global try-catch block in main, which would definitely always run if something bad happens. Frankly I expected the defer-recover in main() to work like that.

Is there another mechanism to recover from such failures or is the only way to deal with it is to add defer-recover into every goroutine?

Thank you for your time!

@davecheney

This comment has been minimized.

Show comment
Hide comment
@davecheney

davecheney Feb 27, 2016

Contributor

@slava-vishnyakov we don't use the issue tracker for general discussions. Please ask your question via another method

Contributor

davecheney commented Feb 27, 2016

@slava-vishnyakov we don't use the issue tracker for general discussions. Please ask your question via another method

@golang golang locked and limited conversation to collaborators Feb 28, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.