phacility/phabricator · error · Exception

Trying to allocate a resource from the wrong status. Status

Error message

Trying to allocate a resource from the wrong status. Status must be "%s", actually "%s".

What it means

allocateResource() only accepts resources in STATUS_PENDING — the status newResourceTemplate() assigns. Any other status means the object already entered (or was pulled out of) the lifecycle: re-allocating an ACTIVE resource, allocating one manually set to BROKEN/RELEASED, or reusing a resource object whose allocation already ran will throw. The guard keeps the PENDING → (ACTIVE|PENDING) transition in allocateResource() unambiguous.

Source

Thrown at src/applications/drydock/storage/DrydockResource.php:131

    $this->slotLocks[] = $key;
    return $this;
  }

  public function allocateResource() {
    // We expect resources to have a pregenerated PHID, as they should have
    // been created by a call to DrydockBlueprint->newResourceTemplate().
    if (!$this->getPHID()) {
      throw new Exception(
        pht(
          'Trying to allocate a resource with no generated PHID. Use "%s" to '.
          'create new resource templates.',
          'newResourceTemplate()'));
    }

    $expect_status = DrydockResourceStatus::STATUS_PENDING;
    $actual_status = $this->getStatus();
    if ($actual_status != $expect_status) {
      throw new Exception(
        pht(
          'Trying to allocate a resource from the wrong status. Status must '.
          'be "%s", actually "%s".',
          $expect_status,
          $actual_status));
    }

    if ($this->activateWhenAllocated) {
      $new_status = DrydockResourceStatus::STATUS_ACTIVE;
    } else {
      $new_status = DrydockResourceStatus::STATUS_PENDING;
    }

    $this->openTransaction();

    try {
      DrydockSlotLock::acquireLocks($this->getPHID(), $this->slotLocks);
      $this->slotLocks = array();

View on GitHub (pinned to 5720a38cfe)

Solutions

  1. Allocate each template exactly once; on failure, build a fresh template with newResourceTemplate() and retry
  2. Do not pre-set the resource status — newResourceTemplate() already sets STATUS_PENDING
  3. To reactivate an existing resource, use the resource update worker path, not allocateResource()
  4. Assert status is PENDING before calling allocateResource() in custom code to fail with a clearer trace

Example fix

// before
$resource = $this->newResourceTemplate($blueprint);
try {
  $resource->allocateResource();
} catch (Exception $ex) {
  $resource->allocateResource(); // second attempt on same object
}
// Exception: Trying to allocate a resource from the wrong status...

// after
try {
  $resource = $this->newResourceTemplate($blueprint);
  $resource->allocateResource();
} catch (Exception $ex) {
  $resource = $this->newResourceTemplate($blueprint); // fresh PENDING template
  $resource->allocateResource();
}
Defensive patterns

Strategy: validation

Validate before calling

// Assert PENDING status before allocating:
if ($resource->getStatus() !== DrydockResourceStatus::STATUS_PENDING) {
  // already advanced or terminal: build a fresh template instead
  $resource = $this->newResourceTemplate($blueprint);
}
$resource->allocateResource();

Type guard

function isFreshResourceTemplate(DrydockResource $resource) {
  return $resource->getStatus() === DrydockResourceStatus::STATUS_PENDING
    && (bool)$resource->getPHID();
}

Try / catch

catch (Exception $ex) { on 'from the wrong status', discard the current object and rebuild via newResourceTemplate() before retrying; do not retry on the same object }

Prevention

When it happens

Trigger: Calling allocateResource() twice on the same template object after the first call saved/activated it; setting a custom status on the resource before allocating; loading an existing resource from the database and trying to push it back through allocation.

Common situations: Retry logic in custom blueprints that re-enters allocation after a partial failure without rebuilding the template; copy-paste blueprint code that mutates status; migration scripts trying to 're-allocate' old resources.

Related errors


AI-assisted analysis of phacility/phabricator@5720a38cfe (2026-08-21). Data as JSON: /api/errors/ec6d6789b3846826. Report an issue: GitHub.