TL;DR

  • Changing the type of an EBS-backed EC2 instance requires a stop. A management worker drives stop, modify, and start step by step from database state.
  • After a stop, ModifyInstanceAttribute failed with InvalidParameterCombination and the host stayed stopped. Errors where AWS says it changed nothing now fail the operation and start the host at its original size.
  • The AWS call used to wait inside a database transaction. Now the transaction commits first, AWS is called with no locks, and a second transaction reads the state again. If it changed, the reply is not used.

A resize is stop, change type, start

Each workspace has one shared EC2 host. It moves between m7i.large, m7i.xlarge, and m7i.2xlarge depending on load.

An EBS-backed instance must be stopped before its type can change. So a management worker compares AWS state with the operation record in the database on every pass and moves one step at a time.

While the host is still running, the worker checks permission with DryRun=True. If the permission exists, AWS answers with a DryRunOperation error. This avoids learning about a missing permission only after the host is down.

The stop goes through the existing safe-stop path. It closes admission for new work and stops only after running work has finished.

After the stop, AWS refused and the host never came back

Once, the type change after the stop failed with InvalidParameterCombination. A previous fix had added Attribute=instanceType to the request. ModifyInstanceAttribute changes one attribute per call, and Attribute cannot be combined with InstanceType.

The real problem was that this error was treated as “result unknown”. On the next pass the worker observed again, sent the same request, and was refused again. The host stayed stopped and the user kept waiting for it to start.

The request now sets only InstanceType. AWS errors are split into two groups.

REFUSED = {"InvalidParameterCombination", "InvalidParameterValue",
           "InvalidInstanceAttributeValue", "UnsupportedOperation"}
DENIED = {"UnauthorizedOperation", "AccessDenied", "AccessDeniedException"}
  • Refused or denied (the two sets above): AWS did not accept the change. The operation fails and the host starts at its original size.
  • Everything else (throttling, insufficient capacity, timeouts, dropped connections): it is unknown whether the change applied. The intent stays, and the next pass observes AWS before deciding.

When the type could not change, what matters is getting the host back. Only a reply that proves nothing changed can justify that decision.

Holding a global lock while waiting for AWS

Looking over the resize path again showed a second problem. The AWS call was awaited inside a database transaction.

That transaction held these locks:

  • an advisory lock shared by all workspaces that serializes scaling policy changes
  • FOR UPDATE on the workspace row and on the resize operation row

AWS usually answers quickly, but a slow call waits until the SDK timeout. During that time, resizes and policy changes for other workspaces wait on this lock too.

On top of that, decisions after the reply used values read before the call. For a manually requested resize the host row was not locked, so a user’s stop could commit while AWS was answering. The worker still went ahead with the values it had read before.

I considered three options.

  • Shorter timeouts: locks are held for less time, but decisions still use stale values.
  • Hand the AWS call to a separate job: an outbox or similar. Reliable, but too heavy for a controller that already moves one step per pass.
  • Split into two transactions: decide and commit, call AWS without locks, then lock and read again.

I chose the third. Keeping external I/O out of transactions is common advice for PostgreSQL. A long open transaction keeps its locks and also holds back vacuum.

Per-host serialization is kept separately by a session-level advisory lock. It survives across transactions, so two workers never advance the same host at once.

Does the reply still match the current intent?

The first transaction decides what to ask AWS and records the state it was based on:

  • the operation’s ID, status, target type, requester, and the policy at request time
  • the current effective policy
  • the host row (instance ID, boot ID, interruption generation, desired state, stop request, start-after-stop)
action = await decide(conn)                 # committed in the first transaction
if conn.info.transaction_status != TransactionStatus.IDLE:
    raise RuntimeError("AWS I/O must not run inside a transaction")
error = await call_aws(action)              # no locks held
result = await decide(conn, reply=(action, error))   # read again and compare

The second transaction reads the same rows with FOR UPDATE and rebuilds the state. If anything differs from before the call, the reply is not used. Startup stays on hold, and the next pass observes AWS before deciding.

The transaction_status check guarantees that the psycopg autocommit connection is really idle. If a caller had opened a transaction around this code, the worker would wait for AWS while holding locks. In that case it stops before calling AWS.

When a manual stop overlaps, the stop request never gains a “start again afterwards” flag.

The operation itself can also be cancelled while the reply is in flight. The first version returned “go ahead and start” when the operation was gone, which meant starting from the observation made before the call. That was fixed too: if the operation is gone after an AWS call, startup is held and AWS is observed again.

An AWS success is not completion yet

A successful ModifyInstanceAttribute does not complete the resize. On a later pass, once DescribeInstances reports the target type, the host starts only if it was running before. The resize completes when the started host is ready and the observed type matches the target.

An AWS call is an event outside the database. Waiting for it inside a transaction does not stop anything that happens outside. Calling without locks and reading again afterwards turned out simpler and harder to get wrong.