Skip to content

Commit cc2aa9b

Browse files
author
Tal Hadad
committed
cleaning old active connection if a new one wants to connect
1 parent a4102bf commit cc2aa9b

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

src/session.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let requestTimeStamp: number;
5353
let responseTimeStamp: number;
5454
export let sessionDir: string;
5555
export let workingDir: string;
56-
let incomingRequestServerConnected = false;
56+
let incomingRequestServerCurrentSocket: Socket | null = null;
5757
let rVer: string;
5858
let pid: string;
5959
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -74,6 +74,15 @@ let activeBrowserExternalUri: Uri | undefined;
7474
export let incomingRequestServerAddressInfo: AddressInfo | undefined = undefined;
7575
export let attached = false;
7676

77+
class AnotherSocketConnectionError extends Error {
78+
finishCleaningCallback: () => void;
79+
80+
constructor (finishCleaningCallback: () => void) {
81+
super();
82+
this.finishCleaningCallback = finishCleaningCallback;
83+
}
84+
}
85+
7786
const addressToStr = (addressInfo: AddressInfo) => `${addressInfo.address}:${addressInfo.port}`;
7887

7988
function updateSessionStatusBarItem(sessionStatusBarItem: StatusBarItem) {
@@ -823,13 +832,29 @@ function startIncomingRequestServer(sessionStatusBarItem: StatusBarItem) {
823832
// eslint-disable-next-line @typescript-eslint/no-misused-promises
824833
const incomingRequestServer = new Server(async (socket: Socket) => {
825834
console.info(`Incoming connection to the request server from ${addressToStr(socket.address() as AddressInfo)}`);
826-
if (incomingRequestServerConnected) {
827-
console.error('A new connection to the incoming request server tries to connect but another connection currently connected!');
828-
return;
835+
if (incomingRequestServerCurrentSocket !== null) {
836+
console.info('Closing existing connection to the incoming request server since a new one is pending.');
837+
838+
let resolveHandle: (() => void) | null = null;
839+
let wasHandledQuick = false;
840+
const promise = new Promise<void>((resolve) => {
841+
resolveHandle = resolve;
842+
});
843+
const callback = () => {
844+
if (resolveHandle === null) {
845+
wasHandledQuick = true;
846+
} else {
847+
resolveHandle();
848+
}
849+
};
850+
incomingRequestServerCurrentSocket.destroy(new AnotherSocketConnectionError(callback));
851+
if (!wasHandledQuick) {
852+
await promise;
853+
}
829854
}
830855

831856
console.info('A new connection to the incoming request server has been established.');
832-
incomingRequestServerConnected = true;
857+
incomingRequestServerCurrentSocket = socket;
833858

834859
try {
835860
const promiseSocket = new PromiseSocket(socket);
@@ -849,7 +874,7 @@ function startIncomingRequestServer(sessionStatusBarItem: StatusBarItem) {
849874
if (contentToProcess) {
850875
console.error('TCP connection recieved EOF, but the last content didn\'t end up with line break.');
851876
}
852-
incomingRequestServerConnected = false;
877+
incomingRequestServerCurrentSocket = null;
853878
return;
854879
}
855880
// otherwise
@@ -867,11 +892,19 @@ function startIncomingRequestServer(sessionStatusBarItem: StatusBarItem) {
867892
contentToProcess = requests[requests.length - 1];
868893
}
869894
} catch (err) {
870-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
871-
console.error(`Error while processing TCP connection: ${err}`);
895+
if (err instanceof AnotherSocketConnectionError) {
896+
console.error(`Closing this TCP connection since another one is pending.`);
897+
} else {
898+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
899+
console.error(`Error while processing TCP connection: ${err}`);
900+
}
872901

873902
await cleanupSession();
874-
incomingRequestServerConnected = false;
903+
incomingRequestServerCurrentSocket = null;
904+
905+
if (err instanceof AnotherSocketConnectionError) {
906+
err.finishCleaningCallback();
907+
}
875908
}
876909
});
877910

0 commit comments

Comments
 (0)