-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
context: AfterFunc
spawns a goroutine
#61672
Comments
In this case, Stubby is a Google-internal RPC system which uses its own |
Could you help me understand where I see the static |
The For some historical context: The The As of Go 1.21, the The |
Is this WAI and workaround supplied? (It looks like that to me). |
I believe so, yes. |
Yes, meant to close this. To confirm, that answers my question! |
What version of Go are you using (
go version
)?1.21
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?N/A
What did you do?
We are running a Stubby RPC server for a service processing ~10'000 in-flight RPCs. This server has performance issues where locks get held for excessive durations. Thanks to the help of @aktau, we were able to attribute some of this to the Go scheduler having trouble with the ~100'000 goroutines our server ends up running.
A lof of these were caused by goroutines waiting for channels, either in
context.propagateCancel
, or in other cancellation propagation mechanisms (e.g. Stubby connection breaking cancelling the RPC).Thus, we were expecting to be able to use
AfterFunc
in some of the libraries to reduce the total number of goroutines.What did you expect to see?
Based on #57928 mentioning performance and avoiding goroutines as motivating reasons, I expected this to not spawn a separate goroutine, but rather use some kind of callback registration. So I expected fewer goroutines in total on our server.
What did you see instead?
Reading the code,
context.AfterFunc
ends up using thepropagateCancel
API internally, which spawns a goroutine to wait on cancellation. Thus, usingcontext.AfterFunc
does not reduce the goroutines.It looks like there is an
afterFuncer
interface defined to do exactly this, but it is not implemented by any of the stdlib contexts. It is also not exported (though implementing our own context and rolling it out everywhere would not be viable either way).Filing this issue on the public Go repo on the suggestion of @aktau, please let me know if I am missing any information as this is the first time I do this!
The text was updated successfully, but these errors were encountered: