99designs / 99designs/gqlgen

Subscription ctx.Done() not closed during server shutdown

Open
#2,610 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
10.8k
Forks
1.3k
Avg merge
2d 36m
Merged PRs (30d)
26

Description

### What happened?

We have long running graphql subscriptions for tracking things like online status updates that we define like this:
```
func (r *subscriptionResolver) Online(ctx context.Context) (<-chan *model.OnlineUpdates, error) {
onlineEvents := make(chan *model.OnlineUpdates, 1)
doSomeSetup()
go func() {
for {
select {
case <-ctx.Done():
doSomeCleanup()
default:
sendUpdates(onlineEvents)
}
}
}
return onlineEvents, nil
}
```

This is currently working exactly how we'd expect for a running server. If the client closes the subscription websocket, then the `ctx.Done()` channel gets closed and triggers our cleanup code, which does things like marking the user as offline in our data store, and other such things.

The problem comes in when the server gets shutdown by calling something like `http.Server.Shutdown()`. In this case, it appears that the subscription channels themselves get closed, but the actual `ctx.Done()` channel never gets closed, presumably because cancel never gets called on the Context. This means that our cleanup code never gets called.

### What did you expect?

`ctx.Done()` to get closed when the http server is shutdown.

### versions
- `go run github.com/99designs/gqlgen version`: `v0.17.28`
- `go version`: `go version go1.20.2 darwin/arm64`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.