Skip to content

Commit b79910d

Browse files
thymikeeApex
andcommitted
fix(android): an issued emulator restore owes the flush hold whatever the outcome
The pending-write mechanism treats ISSUE as the moment the provider's window may start, but registration sat on the confirmed path only: a set-failed readback mismatch closed the pending entry with no mark, letting a drained kill-bound waiter read 'idle' and fire adb inside the window a half-accepted write may have opened. The mark now registers in the registrar's finally, beside the pending-close, for every issued emulator restore; registerTestImeRestore's max keeps that purely lengthening. The set-failed branch stays purely about recovery evidence: retained record and marker still cover retry. Co-Authored-By: Apex <[email protected]>
1 parent b0516fe commit b79910d

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎packages/platform-android/src/ime-restore.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,11 @@ test('a shutdown of a physical device restores without any flush wait', async ()
379379
expect(sleep).not.toHaveBeenCalled();
380380
});
381381

382-
test('a restore that did not switch the IME back skips the flush wait', async () => {
382+
test('a restore that did not switch the IME back still owes the flush wait it may have written', async () => {
383+
// Round twelve: a readback mismatch cannot prove the provider never accepted the write, so
384+
// the issued write owes the kill-bound hold like any other. This path waits ~the full window
385+
// yet still keeps the record and marker (see the failed-restore tests) — the hold covers the
386+
// possible half-written flush; the retained evidence covers the retry.
383387
bindAndroidAdbHostStub();
384388
setAndroidTestImeActiveForTests(DEVICE, true);
385389
const state = stuckDeviceState();
@@ -388,7 +392,9 @@ test('a restore that did not switch the IME back skips the flush wait', async ()
388392
const result = await restoreWith(state, { shutdownTarget: true });
389393

390394
expect(result).toMatchObject({ restored: false, reason: 'set-failed' });
391-
expect(sleep).not.toHaveBeenCalled();
395+
expect(sleep).toHaveBeenCalledTimes(1);
396+
expect(sleep.mock.calls[0]?.[0]).toBeGreaterThanOrEqual(SETTINGS_PROVIDER_FLUSH_SETTLE_MS * 0.8);
397+
expect([...testImeLastRestoreAtPerfMs.keys()]).toEqual([]);
392398
});
393399

394400
test('startup recovery never scans devices without a pending marker, and retains offline markers', async () => {

‎packages/platform-android/src/ime-restore.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,13 @@ async function restoreAndroidTestImeFor(
168168
});
169169
return { restored: false, previousIme, reason: 'set-failed' };
170170
}
171-
// Confirmed back on the previous IME. The provider's flush window opened at this write, so
172-
// register it here — before the record cleanup, before any caller can race a kill against
173-
// it.
174-
if (device.kind === 'emulator') {
175-
registerTestImeRestore(device.id);
176-
}
171+
// Confirmed back on the previous IME — the only outcome that earns the durable-marker
172+
// clear. The flush window, however, attaches to the ISSUED write, not to the confirmation:
173+
// a `set-failed` readback mismatch cannot prove the provider never accepted the write (it
174+
// can have been accepted and then re-resolved or overwritten), and a thrown transport
175+
// failure after dispatch carries the same ambiguity. The mark therefore registers in the
176+
// `finally` below for every issued emulator restore; this branch stays purely about
177+
// recovery evidence.
177178
// Now it is safe to drop the recovery value.
178179
await clearPersistedPreviousIme(adb).catch(() => {});
179180
await clearPersistedRebindDisplacement(adb).catch(() => {});
@@ -183,6 +184,14 @@ async function restoreAndroidTestImeFor(
183184
});
184185
return { restored: true, previousIme, reason: 'ok' };
185186
} finally {
187+
// One rule at one site: issuing an emulator restore write owes the kill-bound flush hold
188+
// whatever the outcome (ok, set-failed, or throw), so the mark registers exactly where the
189+
// pending entry closes, always before the close — a draining waiter can never observe
190+
// "closed but unregistered". registerTestImeRestore takes the max, so this can only
191+
// lengthen a window, never consume one another caller still owes.
192+
if (device.kind === 'emulator') {
193+
registerTestImeRestore(device.id);
194+
}
186195
pendingWriteOpens();
187196
}
188197
}

‎packages/platform-android/src/shutdown/runtime.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,17 @@ test('a kill started while a restore write is in flight waits for it to register
148148
expect([...testImeLastRestoreAtPerfMs.keys()]).toEqual([]);
149149
});
150150

151-
test('an in-flight write that fails without registering releases the kill as idle', async () => {
151+
test('a pending write that closes with no registered mark releases the kill as idle', async () => {
152+
// The registrar opens the pending entry for every restore but registers a window only for
153+
// emulators (the flush hazard is the emulator's SettingsProvider), so a physical-device
154+
// restore is the production shape of "closed, never registered". The drained wait must fall
155+
// through to 'idle' — never strand the kill on an entry that will never gain a mark.
152156
const device = androidDevice();
153157
const closeInFlightWrite = beginTestImeRestoreWrite(device.id);
154158

155159
const pendingKill = createAndroidShutdownRuntime({ commands }).shutdownTarget(device, signal());
156160
expect(run).not.toHaveBeenCalled();
157161

158-
// The set-failed path: the finally block closes the pending entry with no mark registered.
159-
// The drained wait must fall through to 'idle' — never strand the kill on a dead write.
160162
closeInFlightWrite();
161163

162164
await expect(pendingKill).resolves.toEqual(success());

0 commit comments

Comments
 (0)