@@ -58,6 +58,10 @@ type Options struct {
58
58
}
59
59
60
60
type server struct {
61
+ // lifecycleCtx must be tied to the API server's lifecycle
62
+ // as when the API server shuts down, we want to cancel any
63
+ // long-running operations.
64
+ lifecycleCtx context.Context
61
65
AccessURL * url.URL
62
66
ID uuid.UUID
63
67
Logger slog.Logger
@@ -107,6 +111,7 @@ func (t Tags) Valid() error {
107
111
}
108
112
109
113
func NewServer (
114
+ lifecycleCtx context.Context ,
110
115
accessURL * url.URL ,
111
116
id uuid.UUID ,
112
117
logger slog.Logger ,
@@ -124,7 +129,10 @@ func NewServer(
124
129
deploymentValues * codersdk.DeploymentValues ,
125
130
options Options ,
126
131
) (proto.DRPCProvisionerDaemonServer , error ) {
127
- // Panic early if pointers are nil
132
+ // Fail-fast if pointers are nil
133
+ if lifecycleCtx == nil {
134
+ return nil , xerrors .New ("ctx is nil" )
135
+ }
128
136
if quotaCommitter == nil {
129
137
return nil , xerrors .New ("quotaCommitter is nil" )
130
138
}
@@ -153,6 +161,7 @@ func NewServer(
153
161
options .AcquireJobLongPollDur = DefaultAcquireJobLongPollDur
154
162
}
155
163
return & server {
164
+ lifecycleCtx : lifecycleCtx ,
156
165
AccessURL : accessURL ,
157
166
ID : id ,
158
167
Logger : logger ,
@@ -1184,16 +1193,21 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob)
1184
1193
}
1185
1194
go func () {
1186
1195
for _ , wait := range updates {
1187
- // Wait for the next potential timeout to occur. Note that we
1188
- // can't listen on the context here because we will hang around
1189
- // after this function has returned. The s also doesn't
1190
- // have a shutdown signal we can listen to.
1191
- <- wait
1192
- if err := s .Pubsub .Publish (codersdk .WorkspaceNotifyChannel (workspaceBuild .WorkspaceID ), []byte {}); err != nil {
1193
- s .Logger .Error (ctx , "workspace notification after agent timeout failed" ,
1196
+ select {
1197
+ case <- s .lifecycleCtx .Done ():
1198
+ // If the server is shutting down, we don't want to wait around.
1199
+ s .Logger .Debug (ctx , "stopping notifications due to server shutdown" ,
1194
1200
slog .F ("workspace_build_id" , workspaceBuild .ID ),
1195
- slog .Error (err ),
1196
1201
)
1202
+ return
1203
+ case <- wait :
1204
+ // Wait for the next potential timeout to occur.
1205
+ if err := s .Pubsub .Publish (codersdk .WorkspaceNotifyChannel (workspaceBuild .WorkspaceID ), []byte {}); err != nil {
1206
+ s .Logger .Error (ctx , "workspace notification after agent timeout failed" ,
1207
+ slog .F ("workspace_build_id" , workspaceBuild .ID ),
1208
+ slog .Error (err ),
1209
+ )
1210
+ }
1197
1211
}
1198
1212
}
1199
1213
}()
0 commit comments