Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make sure ports are closed properly after the cleanup #1094

Merged
merged 7 commits into from
Oct 31, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix server close logic
  • Loading branch information
mykola-mokhnach committed Oct 26, 2019
commit 578eb8d5eba3c54ddcbdb8b6a55bbaf32b7c8690
46 changes: 27 additions & 19 deletions lib/device-connections-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ class iProxy {
if (!this.serverSocket) {
return;
}
try {
await B.promisify(this.serverSocket.close, {context: this.serverSocket})();
this.log.info('The connection has been closed');
this.serverSocket = null;
} catch (err) {
this.log.error(`Failed to close the connection. Original error: ${err.message}`);
throw err;
}

this.serverSocket.close();
return await new B((resolve, reject) => {
mykola-mokhnach marked this conversation as resolved.
Show resolved Hide resolved
this.serverSocket.once('close', () => {
this.log.info('The connection has been closed');
this.serverSocket = null;
resolve();
});
this.serverSocket.once('error', (e) => {
this.log.error(`Failed to close the connection. Original error: ${e.message}`);
this.serverSocket = null;
reject(e);
});
});
}
}

Expand Down Expand Up @@ -133,17 +139,19 @@ class DeviceConnectionsFactory {

if (usePortForwarding) {
let isPortBusy = (await checkPortStatus(port, '127.0.0.1')) === 'open';
log.warn(`Port #${port} is busy`);
if (isPortBusy && !_.isEmpty(connectionsOnPort)) {
log.info('Trying to release the port');
for (const key of await this._releaseProxiedConnections(connectionsOnPort)) {
delete this._connectionsMapping[key];
}
if ((await checkPortStatus(port, '127.0.0.1')) !== 'open') {
log.info(`Port #${port} has been successfully released`);
isPortBusy = false;
} else {
log.warn(`Did not know how to release port #${port}`);
if (isPortBusy) {
log.warn(`Port #${port} is busy`);
if (!_.isEmpty(connectionsOnPort)) {
log.info('Trying to release the port');
for (const key of await this._releaseProxiedConnections(connectionsOnPort)) {
delete this._connectionsMapping[key];
}
if ((await checkPortStatus(port, '127.0.0.1')) !== 'open') {
log.info(`Port #${port} has been successfully released`);
isPortBusy = false;
} else {
log.warn(`Did not know how to release port #${port}`);
}
}
}

Expand Down