Skip to content

Commit

Permalink
fix: disconnect extension when server is closed VSCODE-536 (#734)
Browse files Browse the repository at this point in the history
* chore: bump minor dependencies

* fix: update package lock

* chore: remove npm legacy-peer-deps

* chore: bump vscode engine and fix useEffect cleanup

* chore: bump data service

* build: bump vscode engine

* refactor: remove sleeps

* refactor: use waitFor
  • Loading branch information
alenakhineika authored Jun 26, 2024
1 parent 37daa7a commit 20caf07
Show file tree
Hide file tree
Showing 19 changed files with 7,686 additions and 7,448 deletions.
2 changes: 0 additions & 2 deletions .npmrc

This file was deleted.

14,780 changes: 7,487 additions & 7,293 deletions package-lock.json

Large diffs are not rendered by default.

106 changes: 53 additions & 53 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"create-vulnerability-tickets": "mongodb-sbom-tools generate-vulnerability-report --snyk-reports=.sbom/snyk-test-result.json --dependencies=.sbom/dependencies.json --create-jira-issues"
},
"engines": {
"vscode": "^1.79.1",
"vscode": "^1.90.2",
"node": ">=16.16.0",
"npm": ">=8.19.4"
},
Expand Down Expand Up @@ -1076,93 +1076,93 @@
}
},
"dependencies": {
"@babel/parser": "^7.22.6",
"@babel/traverse": "^7.23.2",
"@mongodb-js/compass-components": "^1.21.2",
"@babel/parser": "^7.24.6",
"@babel/traverse": "^7.24.6",
"@mongodb-js/compass-components": "^1.25.0",
"@mongodb-js/connection-form": "^1.22.2",
"@mongodb-js/connection-info": "^0.1.2",
"@mongodb-js/mongodb-constants": "^0.8.9",
"@mongodb-js/mongodb-constants": "^0.10.2",
"@mongosh/browser-runtime-electron": "^2.1.1",
"@mongosh/i18n": "^2.1.1",
"@mongosh/service-provider-server": "^2.1.1",
"@mongosh/shell-api": "^2.1.1",
"@segment/analytics-node": "^1.1.4",
"bson": "^6.2.0",
"@segment/analytics-node": "^1.3.0",
"bson": "^6.7.0",
"bson-transpilers": "^2.2.0",
"debug": "^4.3.4",
"dotenv": "^16.3.1",
"debug": "^4.3.5",
"dotenv": "^16.4.5",
"lodash": "^4.17.21",
"micromatch": "^4.0.7",
"mongodb": "^6.3.0",
"mongodb-build-info": "^1.7.1",
"mongodb-cloud-info": "^2.1.0",
"mongodb-connection-string-url": "^3.0.0",
"mongodb-data-service": "^22.17.3",
"mongodb-log-writer": "^1.4.0",
"mongodb-query-parser": "^4.0.2",
"mongodb-schema": "^12.1.1",
"mongodb": "^6.7.0",
"mongodb-build-info": "^1.7.2",
"mongodb-cloud-info": "^2.1.2",
"mongodb-connection-string-url": "^3.0.1",
"mongodb-data-service": "^22.21.1",
"mongodb-log-writer": "^1.4.2",
"mongodb-query-parser": "^4.1.3",
"mongodb-schema": "^12.2.0",
"numeral": "^2.0.6",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"resolve-mongodb-srv": "^1.1.3",
"resolve-mongodb-srv": "^1.1.5",
"ts-log": "^2.2.5",
"uuid": "^8.3.2",
"vscode-languageclient": "^8.1.0",
"vscode-languageserver": "^8.1.0",
"vscode-languageserver-textdocument": "^1.0.10"
"vscode-languageserver-textdocument": "^1.0.11"
},
"devDependencies": {
"@babel/preset-typescript": "^7.22.5",
"@mongodb-js/oidc-mock-provider": "^0.6.10",
"@mongodb-js/oidc-plugin": "^0.3.0",
"@babel/preset-typescript": "^7.24.7",
"@mongodb-js/oidc-mock-provider": "^0.9.1",
"@mongodb-js/oidc-plugin": "^0.4.0",
"@mongodb-js/prettier-config-devtools": "^1.0.1",
"@mongodb-js/sbom-tools": "^0.5.4",
"@mongodb-js/signing-utils": "^0.3.1",
"@mongosh/service-provider-core": "^2.0.2",
"@mongodb-js/sbom-tools": "^0.7.0",
"@mongodb-js/signing-utils": "^0.3.4",
"@mongosh/service-provider-core": "^2.2.1",
"@testing-library/react": "^12.1.5",
"@types/babel__core": "^7.20.1",
"@types/babel__traverse": "^7.20.1",
"@types/chai": "^4.3.5",
"@types/debug": "^4.1.8",
"@types/babel__core": "^7.20.5",
"@types/babel__traverse": "^7.20.6",
"@types/chai": "^4.3.16",
"@types/debug": "^4.1.12",
"@types/glob": "^7.2.0",
"@types/jest": "^26.0.24",
"@types/micromatch": "^4.0.2",
"@types/micromatch": "^4.0.7",
"@types/mkdirp": "^2.0.0",
"@types/mocha": "^8.2.3",
"@types/node": "^14.18.53",
"@types/react": "^17.0.62",
"@types/react-dom": "^17.0.20",
"@types/node": "^14.18.63",
"@types/react": "^17.0.80",
"@types/react-dom": "^17.0.25",
"@types/sinon": "^9.0.11",
"@types/uuid": "^8.3.4",
"@types/vscode": "^1.79.1",
"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0",
"@vscode/test-electron": "^2.3.8",
"@vscode/vsce": "^2.19.0",
"@types/vscode": "^1.90.0",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"@vscode/test-electron": "^2.4.0",
"@vscode/vsce": "^2.29.0",
"@wojtekmaj/enzyme-adapter-react-17": "^0.8.0",
"autoprefixer": "^10.4.16",
"autoprefixer": "^10.4.19",
"buffer": "^6.0.3",
"chai": "^4.3.7",
"chai-as-promised": "^7.1.1",
"chai": "^4.4.1",
"chai-as-promised": "^7.1.2",
"cross-env": "^7.0.3",
"crypto-browserify": "^3.12.0",
"depcheck": "^1.4.3",
"depcheck": "^1.4.7",
"duplicate-package-checker-webpack-plugin": "^3.0.0",
"enzyme": "^3.11.0",
"eslint": "^8.44.0",
"eslint": "^8.57.0",
"eslint-config-mongodb-js": "^5.0.3",
"eslint-plugin-mocha": "^10.1.0",
"eslint-plugin-mocha": "^10.4.3",
"fork-ts-checker-webpack-plugin": "^9.0.2",
"glob": "^7.2.3",
"jest": "^26.6.3",
"jest-junit": "^12.3.0",
"jest-transform-stub": "^2.0.0",
"mkdirp": "^1.0.4",
"mocha": "^10.2.0",
"mocha-junit-reporter": "^2.2.0",
"mocha": "^10.5.1",
"mocha-junit-reporter": "^2.2.1",
"mocha-multi": "^1.1.7",
"mongodb-client-encryption": "^6.0.0",
"mongodb-runner": "^5.5.2",
"mongodb-client-encryption": "^6.0.1",
"mongodb-runner": "^5.6.2",
"node-fetch": "^2.7.0",
"node-loader": "^0.6.0",
"npm-run-all": "^4.1.5",
Expand All @@ -1175,13 +1175,13 @@
"sinon-chai": "^3.7.0",
"stream-browserify": "^3.0.0",
"ts-jest": "^26.5.6",
"ts-loader": "^9.4.4",
"ts-node": "^10.9.1",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^4.9.5",
"webpack": "^5.88.1",
"webpack-bundle-analyzer": "^4.9.0",
"webpack": "^5.92.1",
"webpack-bundle-analyzer": "^4.10.2",
"webpack-cli": "^5.1.4",
"webpack-merge": "^5.9.0",
"webpack-merge": "^5.10.0",
"xvfb-maybe": "^0.2.1"
},
"precommit": [
Expand Down
56 changes: 26 additions & 30 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,9 @@ export default class ConnectionController {
this._statusView.hideMessage();
this._connectionAttempt = null;
this._connectingConnectionId = null;
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
}

this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
}

log.info('Successfully connected', { connectionId });
Expand All @@ -423,11 +424,10 @@ export default class ConnectionController {
dataService.addReauthenticationHandler(
this._reauthenticationHandler.bind(this)
);
this._activeDataService = dataService;
this.setActiveDataService(dataService);
this._currentConnectionId = connectionId;
this._connectionAttempt = null;
this._connectingConnectionId = null;
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
this.eventEmitter.emit(DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED);

// Send metrics to Segment
Expand Down Expand Up @@ -466,7 +466,6 @@ export default class ConnectionController {
);

if (removeConfirmationResponse !== 'Confirm') {
await this.disconnect();
throw new Error('Reauthentication declined by user');
}
}
Expand Down Expand Up @@ -574,43 +573,38 @@ export default class ConnectionController {

this._currentConnectionId = null;
this._disconnecting = true;
this._statusView.showMessage('Disconnecting from current connection...');

this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
this.eventEmitter.emit(DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED);

if (!this._activeDataService) {
void vscode.window.showErrorMessage(
'Unable to disconnect: no active connection.'
);

log.error('Unable to disconnect: no active connection');
return false;
}

this._statusView.showMessage('Disconnecting from current connection...');
const originalDisconnect = this._activeDataService.disconnect.bind(this);
this._activeDataService = null;

try {
// Disconnect from the active connection.
await this._activeDataService.disconnect();
void vscode.window.showInformationMessage('MongoDB disconnected.');
this._activeDataService = null;

void vscode.commands.executeCommand(
'setContext',
'mdb.connectedToMongoDB',
false
);
void vscode.commands.executeCommand(
'setContext',
'mdb.isAtlasStreams',
false
);
await originalDisconnect();
} catch (error) {
// Show an error, however we still reset the active connection to free up the extension.
void vscode.window.showErrorMessage(
'An error occurred while disconnecting from the current connection.'
);
log.error('Unable to disconnect', error);
}

void vscode.commands.executeCommand(
'setContext',
'mdb.connectedToMongoDB',
false
);
void vscode.commands.executeCommand(
'setContext',
'mdb.isAtlasStreams',
false
);
void vscode.window.showInformationMessage('MongoDB disconnected.');

this._disconnecting = false;
this._statusView.hideMessage();

Expand All @@ -626,9 +620,7 @@ export default class ConnectionController {
}

delete this._connections[connectionId];

await this._connectionStorage.removeConnection(connectionId);

this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
}

Expand Down Expand Up @@ -989,9 +981,13 @@ export default class ConnectionController {
this._connectingConnectionId = '';
}

// Exposed for testing.
setActiveDataService(newDataService: DataService): void {
this._activeDataService = newDataService;

// Disconnect the extension if the MongoDB client is closed.
this._activeDataService?.once('close', () => {
void this.disconnect();
});
}

getConnectionQuickPicks(): ConnectionQuickPicks[] {
Expand Down
4 changes: 2 additions & 2 deletions src/telemetry/connectionTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ type CloudInfo = {

async function getHostnameForConnection(
connectionStringData: ConnectionString
): Promise<string | null> {
): Promise<string | undefined> {
if (connectionStringData.isSRV) {
const uri = await resolveMongodbSrv(connectionStringData.toString()).catch(
() => null
);
if (!uri) {
return null;
return undefined;
}
connectionStringData = new ConnectionString(uri, {
looseValidation: true,
Expand Down
Loading

0 comments on commit 20caf07

Please sign in to comment.