Skip to content

Commit 65687c0

Browse files
committed
fix(ios): keep the USB tunnel wait within its deadline
Each devicectl listing while waiting for the tunnel is bounded by the time left before the wait's deadline, not a fresh 10 s, so the wait (and a stop() that awaits it) no longer runs to about 30 s. The cleanup-service calls for the holder are caught, so a rejection is logged at trace level instead of reaching the unhandled-rejection handler.
1 parent 6036d95 commit 65687c0

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎lib/services/ios/ios-device-tunnel-service.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,17 @@ export class IOSDeviceTunnelServiceImpl implements IOSDeviceTunnelService {
149149
private async getConnection(
150150
deviceIdentifier: string,
151151
jsonPath: string,
152+
timeoutMs = this.listTimeoutMs,
152153
): Promise<ICoreDeviceConnection | null> {
153154
// A wedged CoreDevice service can leave devicectl running indefinitely.
154155
const result = await this.childProcess.trySpawnFromCloseEvent(
155156
"xcrun",
156157
["devicectl", "list", "devices", "--quiet", "--json-output", jsonPath],
157158
{},
158-
{ throwError: false, timeout: this.listTimeoutMs },
159+
{
160+
throwError: false,
161+
timeout: Math.max(1, Math.min(this.listTimeoutMs, timeoutMs)),
162+
},
159163
);
160164
if (result.exitCode !== 0 || !this.fs.exists(jsonPath)) {
161165
return null;
@@ -195,7 +199,9 @@ export class IOSDeviceTunnelServiceImpl implements IOSDeviceTunnelService {
195199
);
196200
holder.once("exit", () => {
197201
if (holder.pid) {
198-
this.cleanupService.removeKillProcess(holder.pid.toString());
202+
this.cleanupService
203+
.removeKillProcess(holder.pid.toString())
204+
.catch((err) => this.logger.trace(`removeKillProcess: ${err}`));
199205
}
200206
if (tunnel.closed || tunnel.holder !== holder) {
201207
return;
@@ -219,7 +225,9 @@ export class IOSDeviceTunnelServiceImpl implements IOSDeviceTunnelService {
219225
});
220226
});
221227
if (holder.pid) {
222-
this.cleanupService.addKillProcess(holder.pid.toString());
228+
this.cleanupService
229+
.addKillProcess(holder.pid.toString())
230+
.catch((err) => this.logger.trace(`addKillProcess: ${err}`));
223231
}
224232
return holder;
225233
}
@@ -231,7 +239,11 @@ export class IOSDeviceTunnelServiceImpl implements IOSDeviceTunnelService {
231239
): Promise<string | null> {
232240
const deadline = Date.now() + this.tunnelReadyTimeoutMs;
233241
while (tunnel.holder && !tunnel.closed && Date.now() < deadline) {
234-
const connection = await this.getConnection(deviceIdentifier, jsonPath);
242+
const connection = await this.getConnection(
243+
deviceIdentifier,
244+
jsonPath,
245+
deadline - Date.now(),
246+
);
235247
if (
236248
connection?.tunnelState === "connected" &&
237249
connection.tunnelIPAddress

‎test/services/ios/ios-device-tunnel-service.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,21 @@ describe("IOSDeviceTunnelService", () => {
225225
}
226226
});
227227

228+
it("keeps each listing while waiting for the tunnel within the wait's deadline", async () => {
229+
devicectlListings = [
230+
[coreDevice(wiredDisconnected)],
231+
[coreDevice({ ...wiredConnected, tunnelState: "connecting" })],
232+
[coreDevice(wiredConnected)],
233+
];
234+
235+
await service.open(iPhone());
236+
237+
assert.equal(listCalls, 3);
238+
for (const timeout of listTimeouts.slice(1)) {
239+
assert.isAtMost(timeout, 200);
240+
}
241+
});
242+
228243
it("opens one tunnel per device however often it is asked", async () => {
229244
devicectlListings = [[coreDevice(wiredConnected)]];
230245

0 commit comments

Comments
 (0)