Repository navigation
fix(revyl): retire lease owners before disposal #4169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+558
−230
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { afterEach, test, vi } from 'vitest'; | ||
| import { AppError } from '@agent-device/kernel/errors'; | ||
| import { buildRevylDevice } from './device.ts'; | ||
| import { createRevylRuntime } from './runtime.ts'; | ||
| import { REVYL_STOP_REQUEST_BUDGET_MS } from './revyl-client.ts'; | ||
| import { revylLease } from './revyl-backend.fixtures.ts'; | ||
|
|
||
| const device = buildRevylDevice('ios', revylLease('ios'), 'run-1'); | ||
| const lease = revylLease('ios'); | ||
| const startPath = '/api/v1/execution/start_device'; | ||
| const stopPath = '/api/v1/execution/device/sessions/'; | ||
|
|
||
| function deferred<T>() { | ||
| let resolve!: (value: T) => void; | ||
| const promise = new Promise<T>((report) => { | ||
| resolve = report; | ||
| }); | ||
| return { promise, resolve }; | ||
| } | ||
|
|
||
| function harness() { | ||
| const start = deferred<Response>(); | ||
| const stop = deferred<Response>(); | ||
| let creates = 0; | ||
| let stops = 0; | ||
| vi.stubGlobal( | ||
| 'fetch', | ||
| vi.fn(async (input: URL | string) => { | ||
| const path = new URL(input).pathname; | ||
| if (path === startPath) { | ||
| creates++; | ||
| return await start.promise; | ||
| } | ||
| if (path.startsWith(stopPath)) { | ||
| stops++; | ||
| return await stop.promise; | ||
| } | ||
| if (path.includes('worker-connection')) | ||
| return Response.json({ status: 'ready', worker_ws_url: 'wss://test' }); | ||
| return Response.json({ device_connected: true }); | ||
| }), | ||
| ); | ||
| const { runtime } = createRevylRuntime({ apiKey: 'test-key', clientVersion: 'test' }); | ||
| return { runtime, start, stop, creates: () => creates, stops: () => stops }; | ||
| } | ||
| const started = () => Response.json({ workflow_run_id: 'run-1' }); | ||
| const stopped = () => Response.json({ success: true, request_accepted: true }); | ||
| const retired = (error: unknown) => | ||
| error instanceof AppError && error.details?.reason === 'provider-lease-releasing'; | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| test('release immediately retires interactors, inventory and repeat allocation, including after failure', async () => { | ||
| const h = harness(); | ||
| h.start.resolve(started()); | ||
| await h.runtime.leaseLifecycle!.allocate!(lease); | ||
| assert.ok(h.runtime.getInteractor(device)); | ||
| const release = h.runtime.leaseLifecycle!.release!(lease); | ||
| const rejected = release.then( | ||
| () => undefined, | ||
| (error: unknown) => error, | ||
| ); | ||
| try { | ||
| assert.equal(h.runtime.hasLiveSession(device), false); | ||
| assert.equal(h.runtime.getInteractor(device), undefined); | ||
| assert.equal( | ||
| await h.runtime.deviceInventoryProvider!({ leaseProvider: 'revyl', leaseId: lease.leaseId }), | ||
| null, | ||
| ); | ||
| await assert.rejects(h.runtime.leaseLifecycle!.allocate!(lease), retired); | ||
| } finally { | ||
| h.stop.resolve(Response.json({ success: false })); | ||
| await rejected; | ||
| } | ||
| const error = await rejected; | ||
| assert.ok(error instanceof AppError); | ||
| assert.equal(error.details?.cleanup, 'uncertain'); | ||
| assert.equal(h.runtime.getInteractor(device), undefined); | ||
| await assert.rejects(h.runtime.leaseLifecycle!.allocate!(lease), retired); | ||
| assert.equal(h.creates(), 1); | ||
| }); | ||
|
|
||
| test('release before the first allocation await refuses the device it takes', async () => { | ||
| const h = harness(); | ||
| const allocation = h.runtime.leaseLifecycle!.allocate!(lease); | ||
| const refused = allocation.then(() => false, retired); | ||
| const disposal = h.runtime.leaseLifecycle!.release!(lease); | ||
| h.start.resolve(started()); | ||
| h.stop.resolve(stopped()); | ||
| await disposal; | ||
| assert.equal(h.stops(), 1); | ||
| assert.equal(await refused, true); | ||
| assert.equal(h.runtime.hasLiveSession(device), false); | ||
| }); | ||
|
|
||
| test('a caller that gives up during release retains the typed teardown reason', async () => { | ||
| const h = harness(); | ||
| const controller = new AbortController(); | ||
| const allocation = h.runtime.leaseLifecycle!.allocate!(lease, { signal: controller.signal }); | ||
| await vi.waitFor(() => assert.equal(h.creates(), 1)); | ||
| const disposal = h.runtime.leaseLifecycle!.release!(lease); | ||
| controller.abort(); | ||
| try { | ||
| await assert.rejects( | ||
| allocation, | ||
| (error: unknown) => | ||
| error instanceof AppError && | ||
| error.details?.reason === 'provider-lease-releasing' && | ||
| error.cause instanceof AppError && | ||
| error.cause.details?.reason === 'request_canceled', | ||
| ); | ||
| } finally { | ||
| h.start.resolve(started()); | ||
| h.stop.resolve(stopped()); | ||
| await disposal; | ||
| } | ||
| assert.equal(h.stops(), 1); | ||
| }); | ||
|
|
||
| test.each(['release', 'shutdown'] as const)( | ||
| '%s bounds a pending create and ends its late answer exactly once', | ||
| async (by) => { | ||
| const h = harness(); | ||
| const allocation = h.runtime.leaseLifecycle!.allocate!(lease); | ||
| const refused = allocation.then( | ||
| () => false, | ||
| (error: unknown) => error instanceof AppError, | ||
| ); | ||
| await vi.waitFor(() => assert.equal(h.creates(), 1)); | ||
| vi.useFakeTimers(); | ||
| const disposal = | ||
| by === 'release' ? h.runtime.leaseLifecycle!.release!(lease) : h.runtime.shutdown(); | ||
| let settled = false; | ||
| const observed = disposal | ||
| .then( | ||
| () => undefined, | ||
| (error: unknown) => error, | ||
| ) | ||
| .finally(() => { | ||
| settled = true; | ||
| }); | ||
| // Cross the 120-second disposal wait without settling the transport's create promise. | ||
| await vi.advanceTimersByTimeAsync(125_000); | ||
| try { | ||
| assert.equal(settled, true, 'disposal must answer within its creation budget'); | ||
| const error = await observed; | ||
| assert.ok(error instanceof AppError); | ||
| assert.equal(error.details?.cleanup, 'uncertain'); | ||
| } finally { | ||
| h.start.resolve(started()); | ||
| h.stop.resolve(stopped()); | ||
| await observed; | ||
| await refused; | ||
| } | ||
| assert.equal(await refused, true); | ||
| await vi.waitFor(() => assert.equal(h.stops(), 1)); | ||
| await h.runtime.shutdown(); | ||
| assert.equal(h.stops(), 1); | ||
| assert.equal(h.creates(), 1); | ||
| }, | ||
| ); | ||
|
|
||
| test('release waits beyond the HTTP attempt deadline for the stop result', async () => { | ||
| const h = harness(); | ||
| h.start.resolve(started()); | ||
| await h.runtime.leaseLifecycle!.allocate!(lease); | ||
| vi.useFakeTimers(); | ||
| let settled = false; | ||
| const release = h.runtime.leaseLifecycle!.release!(lease).then( | ||
| (value) => { | ||
| settled = true; | ||
| return value; | ||
| }, | ||
| (error: unknown) => { | ||
| settled = true; | ||
| return error; | ||
| }, | ||
| ); | ||
| // The HTTP attempt owns 30 seconds before its own timeout can settle the request. | ||
| await vi.advanceTimersByTimeAsync(30_001); | ||
| try { | ||
| assert.equal(settled, false, 'disposal must not race the HTTP attempt deadline'); | ||
| } finally { | ||
| h.stop.resolve(stopped()); | ||
| await release; | ||
| } | ||
| assert.equal((await release) instanceof AppError, false); | ||
| assert.equal(h.stops(), 1); | ||
| }); | ||
|
|
||
| test('an unanswered stop reports uncertainty, joins the pending stop, and accepts its late confirmation', async () => { | ||
| const h = harness(); | ||
| h.start.resolve(started()); | ||
| await h.runtime.leaseLifecycle!.allocate!(lease); | ||
| vi.useFakeTimers(); | ||
| let settled = false; | ||
| const release = h.runtime.leaseLifecycle!.release!(lease); | ||
| const observed = release | ||
| .then( | ||
| () => undefined, | ||
| (error: unknown) => error, | ||
| ) | ||
| .finally(() => { | ||
| settled = true; | ||
| }); | ||
| // Cross the HTTP retry budget and the disposal's 5 s settlement margin. | ||
| await vi.advanceTimersByTimeAsync(REVYL_STOP_REQUEST_BUDGET_MS + 10_000); | ||
| try { | ||
| assert.equal(settled, true, 'release must bound an unanswered stop'); | ||
| const error = await observed; | ||
| assert.ok(error instanceof AppError); | ||
| assert.equal(error.details?.cleanup, 'uncertain'); | ||
| } finally { | ||
| h.stop.resolve(stopped()); | ||
| await observed; | ||
| } | ||
| await h.runtime.leaseLifecycle!.release!(lease); | ||
| assert.equal(h.stops(), 1); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.