All finalizers and cleanups run on a single goroutine started internally in the runtime. If a finalizer/cleanup calls Goexit, it will do what it says on the tin and exit the finalizer goroutine. No more finalizers/cleanups will run after that point.
At minimum, Goexit should probably detect this case and throw.
@ianlancetaylor put together https://go.dev/play/p/7jRsXx0W2OZ, which shows that the finalizers never run, but otherwise no error is reported.
cc @golang/runtime
Comment From: gabyhelp
Related Issues
- runtime: bad "deadlock" fatal error when there are pending finalizers #41362
- runtime: finalizer dont called on panic #22493 (closed)
Related Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: mknyszek
Can we change this behavior to crashing at this point? It's almost certainly a bug, but crashing the program might cause programs that run OK to experience issues. Maybe it would be better to just start another finalizer goroutine when that one exits? There would have to be some state reconstruction by the new goroutine which might get complicated.
Comment From: ianlancetaylor
I think it's OK to crash the program if a finalizer/cleanup exits the goroutine. It's clearly a programming error.