Skip to content

Commit 2c54bcb

Browse files
author
Akos Kitta
committed
fix: monitor reconnect on upload
Signed-off-by: Akos Kitta <[email protected]>
1 parent 855e8ad commit 2c54bcb

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

Diff for: arduino-ide-extension/src/node/monitor-service.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ export class MonitorService extends CoreClientAware implements Disposable {
410410
]) as Promise<unknown> as Promise<void>;
411411
}
412412

413-
private closingDuplex: Promise<void> | undefined;
413+
private endingDuplex: Promise<void> | undefined;
414414

415415
/**
416416
* Pauses the currently running monitor, it still closes the gRPC connection
@@ -428,26 +428,28 @@ export class MonitorService extends CoreClientAware implements Disposable {
428428
);
429429
return;
430430
}
431-
if (this.closingDuplex) {
432-
return this.closingDuplex;
431+
if (this.endingDuplex) {
432+
return this.endingDuplex;
433433
}
434-
const deferredClose = new Deferred<void>();
435-
this.closingDuplex = deferredClose.promise;
434+
const deferredEnd = new Deferred<void>();
435+
this.endingDuplex = deferredEnd.promise;
436436

437-
// to terminate the monitor connection, send a close request, and wait for the expected cancellation error
438-
duplex.once('error', (error) => {
439-
// TODO: check if cancel error
440-
deferredClose.resolve();
437+
// to terminate the monitor connection, send a close request, and wait for the end event
438+
duplex.once('end', () => {
439+
deferredEnd.resolve();
441440
});
442-
duplex.write(new MonitorRequest().setClose(true));
443441
try {
444-
await this.closingDuplex;
442+
await new Promise((resolve) =>
443+
duplex.write(new MonitorRequest().setClose(true), resolve)
444+
);
445+
await this.endingDuplex;
445446
} finally {
446-
this.closingDuplex = undefined;
447+
this.endingDuplex = undefined;
447448
}
448449
// Sanity check
449-
if (!duplex.closed) {
450-
throw new Error('Could not close monitor connection');
450+
// Duplexes are allowed to be half open, check whether the monitor server (the readable) has ended
451+
if (!duplex.readableEnded) {
452+
throw new Error('Could not end the monitor connection');
451453
}
452454
}
453455

0 commit comments

Comments
 (0)