[Bug] A cross-cluster migration loses its temporary token while it runs and ends with 401 Unauthorized
Describe the bug
A live cross-cluster migration started from PegaProx stops in the middle of the copy:
migration active, transferred 296.0 B of 8.0 GiB VM-state, 0.0 B/s
tunnel: Tunnel for /run/qemu-server/105.migrate failed - server didn't upgrade: 401 Unauthorized
migration status error: failed - Unable to write to socket: Broken pipe
PegaProx deletes the temporary token (pegaprox-migrate-<epoch>) on the target cluster while the task is still running. The source node presents that token each time it opens a tunnel, so the next tunnel is refused.
Three runs of the same guest, read from the PegaProx journal and from the task logs of the source node:
| token created | token deleted | task ended | task ran |
|---|---|---|---|
| 17:06:30 | 17:17:31 (661 s) | 401 | 870 s |
| 17:22:38 | 17:27:53 (315 s) | 401 | 840 s |
| 05:59:50 | 06:05:09 (319 s) | 401 | 360 s |
The thread that deletes the token is cleanup_token_when_done in cross_cluster_migrate_api (pegaprox/api/vms.py). It looks for the task in source_manager.get_tasks(limit=100). Three points in it end a running migration:
-
After 300 s, a task missing from that list counts as finished when a guest with the source or the target VMID exists on the target cluster:
vm_on_target = any( v.get('vmid') == vmid or v.get('vmid') == target_vmid for v in (target_vms or []) )Any guest with that number is enough. Our two clusters each have a VM 105, so the check is always true here, whatever the migration does.
-
get_tasksreturns[]when the read fails (timeout, connection error, not connected). To the loop, a failed read and a finished task look the same. The journal shows connection resets towards the source cluster around those times. I could not prove that this is why the task was missing at 315 s and 319 s: the[TOKEN-CLEANUP]lines go to the root logger at INFO and do not reach the journal of a stock install. -
After 7200 s the token is deleted whatever the task does (
Timeout after {max_wait}s waiting for task, deleting token anyway). A guest of 1 or 2 TB does not fit in two hours on our links.
_token_cleanup_thread in pegaprox/background/cross_cluster_lb.py is the same loop with 120 s instead of 300 s, and without the check on the target.
One consequence: preflight.cross_online explains the 401 by a WebSocket ticket timeout of about five minutes in Proxmox, and turns a live migration of more than 100 GB into an offline one. The five minutes we measured are the 300 s of this cleanup, not a Proxmox limit. If that holds for you too, the offline fallback may not be needed once the token stays.
Steps to Reproduce
- Two clusters in PegaProx, each with a guest that has the same VMID as a guest on the other (for example a VM 105 on both).
- Start a live cross-cluster migration of that guest that takes more than five minutes, with a free target VMID.
- Make the source cluster miss one task list read after the fifth minute (or wait for a slow answer).
Without step 3, the same end comes at two hours for any migration that long.
tests/test_migration_token_outlives_the_task.py in the PR drives the route and the balancer up to the cleanup thread and runs it on a fake clock. On the current code, the token goes at 315 s (135 s for the balancer) with the task still running.
Expected behavior
The token stays on the target until the migration task has ended, however long it takes, and is deleted then.
Environment
- PegaProx Version: 1.4.1 (Debian package 1.4.1-1). The same code is on
Testing. - Installation Method: apt (packages.gyptazy.com)
- OS: Debian 13
- Proxmox VE: 9.2 on both clusters
- Browser: not involved
- Behind Reverse Proxy? Yes (HAProxy)
- UI? not involved
Also seen, not part of this report
- A PegaProx restart during a migration leaves the token on the target for good: the thread is a daemon, and nothing looks for leftover
pegaprox-migrate-*tokens at start. - Two migrations started in the same second get the same token name, and
create_api_tokendrops the first one. - The result of
delete_api_tokenis not read, so the log says "Deleted" when the delete failed.
I can open separate issues for these if you want them tracked.
Thanks for the detailed write-up, the code reads the way you describe. The cleanup thread in cross_cluster_migrate_api counts a task that is missing from get_tasks(limit=100) as done after 300 s as soon as any guest with either VMID exists on the target, a failed read looks the same as an empty list, and the two hour cap deletes the token whatever the task does. The balancer loop in cross_cluster_lb.py has the same shape. Your #1349 is on the list for review.
Cheers, Marcus
Fixed in 1.5.0 (thanks for the PR as well). The temporary token stays valid for the whole cross-cluster migration now. Closing.