Skip to content

Commit 3569327

Browse files
committed
fix(android): give localhost URL reverses an owner
The localhost URL auto-reverse now ensures its mapping with the stable owner 'localhost-url'. Teardown of an attached Limrun instance removes only owned mappings, so before this change the mapping stayed on the owner's device after disconnect. A live run showed that adbd keeps it.
1 parent e4bba1f commit 3569327

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎packages/platform-android/src/__tests__/app-lifecycle-open.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ test('openAndroidApp ensures Android reverse before localhost deep link launch',
171171
booted: true,
172172
};
173173
const calls: Array<
174-
{ kind: 'exec'; args: readonly string[] } | { kind: 'reverse'; local: string; remote: string }
174+
| { kind: 'exec'; args: readonly string[] }
175+
| { kind: 'reverse'; local: string; remote: string; ownerId?: string }
175176
> = [];
176177

177178
await withAndroidAdbProvider(
@@ -182,7 +183,12 @@ test('openAndroidApp ensures Android reverse before localhost deep link launch',
182183
},
183184
reverse: {
184185
ensure: async (mapping) => {
185-
calls.push({ kind: 'reverse', local: mapping.local, remote: mapping.remote });
186+
calls.push({
187+
kind: 'reverse',
188+
local: mapping.local,
189+
remote: mapping.remote,
190+
ownerId: mapping.ownerId,
191+
});
186192
},
187193
remove: async () => {},
188194
removeAllOwned: async () => {},
@@ -193,7 +199,7 @@ test('openAndroidApp ensures Android reverse before localhost deep link launch',
193199
);
194200

195201
assert.deepEqual(calls, [
196-
{ kind: 'reverse', local: 'tcp:8083', remote: 'tcp:8083' },
202+
{ kind: 'reverse', local: 'tcp:8083', remote: 'tcp:8083', ownerId: 'localhost-url' },
197203
{
198204
kind: 'exec',
199205
args: [

‎packages/platform-android/src/app-lifecycle.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const ANDROID_LAUNCHER_CATEGORY = 'android.intent.category.LAUNCHER';
2828
const ANDROID_LEANBACK_CATEGORY = 'android.intent.category.LEANBACK_LAUNCHER';
2929
const ANDROID_DEFAULT_CATEGORY = 'android.intent.category.DEFAULT';
3030
const ANDROID_LOCALHOST_HOSTNAMES = new Set(['localhost', '127.0.0.1', '::1', '[::1]']);
31+
/** Owns localhost URL reverses, so a teardown that removes only owned mappings removes them too. */
32+
const ANDROID_LOCALHOST_URL_REVERSE_OWNER = 'localhost-url';
3133
const ANDROID_CLOSE_FOCUS_TIMEOUT_MS = 2_000;
3234
const ANDROID_CLOSE_FOCUS_POLL_MS = 50;
3335
const ANDROID_CLOSE_PROCESS_TIMEOUT_MS = 2_000;
@@ -124,7 +126,11 @@ async function ensureAndroidLocalhostReverse(device: DeviceInfo, target: string)
124126

125127
const reverse = createAndroidPortReverseManager(resolveAndroidAdbProvider(device));
126128
try {
127-
await reverse.ensure({ local: endpoint, remote: endpoint });
129+
await reverse.ensure({
130+
local: endpoint,
131+
remote: endpoint,
132+
ownerId: ANDROID_LOCALHOST_URL_REVERSE_OWNER,
133+
});
128134
} catch (error) {
129135
const details = {
130136
localPort: endpoint.replace('tcp:', ''),

‎src/__tests__/limrun-runtime.test.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -843,25 +843,30 @@ test('Limrun removes only its own port reverse mappings from an attached Android
843843
const runtime = new LimrunRuntime({ instances: { android: ATTACHED_ANDROID } });
844844
const lease = { ...androidLease(), leaseId: 'lease-attached-android' };
845845
vi.mocked(runCmd).mockImplementation(async (_command, args) => ({
846-
stdout: args.includes('--list') ? 'host-7 tcp:8081 tcp:8081\nhost-9 tcp:8097 tcp:8097\n' : '',
846+
stdout: args.includes('--list')
847+
? 'host-7 tcp:8081 tcp:8081\nhost-9 tcp:8097 tcp:8097\nhost-9 tcp:8099 tcp:8099\n'
848+
: '',
847849
stderr: '',
848850
exitCode: 0,
849851
}));
850852

851-
await allocateLimrunDevice(runtime, lease);
853+
const device = await allocateLimrunDevice(runtime, lease);
852854
await runtime.configurePortReverse({
853855
leaseId: lease.leaseId,
854856
devicePort: 8097,
855857
hostPort: 8097,
856858
name: 'react-devtools',
857859
});
860+
await runtime.getInteractor(device)?.open('http://127.0.0.1:8099/');
858861
await runtime.shutdown();
859862

860863
const removals = vi
861864
.mocked(runCmd)
862865
.mock.calls.map(([, args]) => args)
863-
.filter((args) => args.includes('--remove'));
864-
assert.deepEqual(removals, [['-s', '127.0.0.1:62001', 'reverse', '--remove', 'tcp:8097']]);
866+
.filter((args) => args.includes('--remove'))
867+
.map((args) => args.at(-1))
868+
.sort();
869+
assert.deepEqual(removals, ['tcp:8097', 'tcp:8099']);
865870
});
866871

867872
test('Limrun refuses to replace an owner port reverse on an attached Android instance', async () => {

0 commit comments

Comments
 (0)