@@ -199,12 +199,17 @@ func (mtask *managedTask) overseeTask() {
199
199
// `desiredstatus`es which are a construct of the engine used only here,
200
200
// not present on the backend
201
201
mtask .UpdateStatus ()
202
- // If this was a 'state restore', send all unsent statuses
203
- mtask .emitCurrentStatus ()
204
202
205
- // Wait for host resources required by this task to become available
203
+ // Wait here until enough resources are available on host for the task to progress
204
+ // - Waits until host resource manager succesfully 'consume's task resources and returns
205
+ // - For tasks which have crossed this stage before (on agent restarts), resources are pre-consumed - returns immediately
206
+ // - If the task is already stopped (knownStatus is STOPPED), does not attempt to consume resources - returns immediately
207
+ // (resources are later 'release'd on Stopped task emitTaskEvent call)
206
208
mtask .waitForHostResources ()
207
209
210
+ // If this was a 'state restore', send all unsent statuses
211
+ mtask .emitCurrentStatus ()
212
+
208
213
// Main infinite loop. This is where we receive messages and dispatch work.
209
214
for {
210
215
if mtask .shouldExit () {
@@ -272,6 +277,13 @@ func (mtask *managedTask) emitCurrentStatus() {
272
277
// the task. It will wait for event on this task's consumedHostResourceEvent
273
278
// channel from monitorQueuedTasks routine to wake up
274
279
func (mtask * managedTask ) waitForHostResources () {
280
+ if mtask .GetKnownStatus ().Terminal () {
281
+ // Task's known status is STOPPED. No need to wait in this case and proceed to cleanup
282
+ // This is relevant when agent restarts and a task has stopped - do not attempt
283
+ // to consume resources in host resource manager
284
+ return
285
+ }
286
+
275
287
if ! mtask .IsInternal && ! mtask .engine .hostResourceManager .checkTaskConsumed (mtask .Arn ) {
276
288
// Internal tasks are started right away as their resources are not accounted for
277
289
mtask .engine .enqueueTask (mtask )
0 commit comments