Skip to content

Commit 294db1b

Browse files
committed
fix: timout not cleared on exit signals
1 parent e22a361 commit 294db1b

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/services/livesync/android-device-livesync-sockets-service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export class AndroidDeviceSocketsLiveSyncService extends DeviceLiveSyncServiceBa
2020
private $processService: IProcessService) {
2121
super($platformsData, device);
2222
this.livesyncTool = this.$injector.resolve(AndroidLivesyncTool);
23-
this.$processService.attachToProcessExitSignals(this.livesyncTool, this.livesyncTool.end);
2423
}
2524

2625
public async beforeLiveSyncAction(deviceAppData: Mobile.IDeviceAppData): Promise<void> {

lib/services/livesync/android-livesync-tool.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
2929
private $errors: IErrors,
3030
private $fs: IFileSystem,
3131
private $logger: ILogger,
32-
private $mobileHelper: Mobile.IMobileHelper) {
32+
private $mobileHelper: Mobile.IMobileHelper,
33+
private $processService: IProcessService) {
3334
this.operationPromises = Object.create(null);
3435
this.socketError = null;
3536
this.socketConnection = null;
37+
this.$processService.attachToProcessExitSignals(this, this.dispose);
3638
}
3739

3840
public async connect(configuration: IAndroidLivesyncToolConfiguration): Promise<void> {
@@ -135,21 +137,22 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
135137
headerBuffer.writeUInt8(doRefreshCode, offset);
136138
const hash = crypto.createHash("md5").update(headerBuffer).digest();
137139

138-
this.operationPromises[id] = {
139-
resolve,
140-
reject,
141-
socketId
142-
};
143-
144140
this.socketConnection.write(headerBuffer);
145141
this.socketConnection.write(hash);
146142

147143
timeout = timeout || SYNC_OPERATION_TIMEOUT;
148-
setTimeout(() => {
144+
const timeoutId = setTimeout(() => {
149145
if (this.isOperationInProgress(id)) {
150146
this.handleSocketError(socketId, "Sync operation is taking too long");
151147
}
152148
}, timeout);
149+
150+
this.operationPromises[id] = {
151+
resolve,
152+
reject,
153+
socketId,
154+
timeoutId
155+
};
153156
});
154157

155158
return operationPromise;
@@ -216,7 +219,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
216219
} else {
217220
const error = this.checkConnectionStatus();
218221
//TODO Destroy method added in node 8.0.0.
219-
//when we depricate node 6.x uncoment the line belolw
222+
//when we deprecate node 6.x uncomment the line below
220223
//fileStream.destroy(error);
221224
reject(error);
222225
}
@@ -348,6 +351,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
348351
const promiseHandler = this.operationPromises[operationId];
349352

350353
if (promiseHandler) {
354+
clearTimeout(promiseHandler.timeoutId);
351355
promiseHandler.resolve({operationId, didRefresh});
352356
delete this.operationPromises[operationId];
353357
}
@@ -370,6 +374,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
370374
.forEach(operationId => {
371375
const operationPromise = this.operationPromises[operationId];
372376
if (operationPromise.socketId === socketId) {
377+
clearTimeout(operationPromise.timeoutId);
373378
operationPromise.reject(error);
374379
delete this.operationPromises[operationId];
375380
}
@@ -403,5 +408,15 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
403408

404409
return this.$mobileHelper.buildDevicePath(relativeFilePath);
405410
}
411+
412+
private dispose(): void {
413+
this.end();
414+
415+
_.keys(this.operationPromises)
416+
.forEach(operationId => {
417+
const operationPromise = this.operationPromises[operationId];
418+
clearTimeout(operationPromise.timeoutId);
419+
});
420+
}
406421
}
407422
$injector.register("androidLivesyncTool", AndroidLivesyncTool);

0 commit comments

Comments
 (0)