Improve error handling for `DeviceListener` (#11484)

... particularly for when it races with a client shutdown.
t3chguy/dedup-icons-17oct
Richard van der Hoff 2023-08-30 10:35:15 +01:00 committed by GitHub
parent 50160b9969
commit 6cc42b7e2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 3 deletions

View File

@ -14,7 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { MatrixEvent, ClientEvent, EventType, MatrixClient, RoomStateEvent, SyncState } from "matrix-js-sdk/src/matrix"; import {
MatrixEvent,
ClientEvent,
EventType,
MatrixClient,
RoomStateEvent,
SyncState,
ClientStoppedError,
} from "matrix-js-sdk/src/matrix";
import { logger } from "matrix-js-sdk/src/logger"; import { logger } from "matrix-js-sdk/src/logger";
import { CryptoEvent } from "matrix-js-sdk/src/crypto"; import { CryptoEvent } from "matrix-js-sdk/src/crypto";
import { IKeyBackupInfo } from "matrix-js-sdk/src/crypto/keybackup"; import { IKeyBackupInfo } from "matrix-js-sdk/src/crypto/keybackup";
@ -260,7 +268,17 @@ export default class DeviceListener {
return cli?.getRooms().some((r) => cli.isRoomEncrypted(r.roomId)) ?? false; return cli?.getRooms().some((r) => cli.isRoomEncrypted(r.roomId)) ?? false;
} }
private async recheck(): Promise<void> { private recheck(): void {
this.doRecheck().catch((e) => {
if (e instanceof ClientStoppedError) {
// the client was stopped while recheck() was running. Nothing left to do.
} else {
logger.error("Error during `DeviceListener.recheck`", e);
}
});
}
private async doRecheck(): Promise<void> {
if (!this.running || !this.client) return; // we have been stopped if (!this.running || !this.client) return; // we have been stopped
const cli = this.client; const cli = this.client;

View File

@ -15,7 +15,15 @@ limitations under the License.
*/ */
import { Mocked, mocked } from "jest-mock"; import { Mocked, mocked } from "jest-mock";
import { MatrixEvent, Room, MatrixClient, DeviceVerificationStatus, CryptoApi, Device } from "matrix-js-sdk/src/matrix"; import {
MatrixEvent,
Room,
MatrixClient,
DeviceVerificationStatus,
CryptoApi,
Device,
ClientStoppedError,
} from "matrix-js-sdk/src/matrix";
import { logger } from "matrix-js-sdk/src/logger"; import { logger } from "matrix-js-sdk/src/logger";
import { CrossSigningInfo } from "matrix-js-sdk/src/crypto/CrossSigning"; import { CrossSigningInfo } from "matrix-js-sdk/src/crypto/CrossSigning";
import { CryptoEvent } from "matrix-js-sdk/src/crypto"; import { CryptoEvent } from "matrix-js-sdk/src/crypto";
@ -257,6 +265,20 @@ describe("DeviceListener", () => {
expect(mockCrypto!.isCrossSigningReady).not.toHaveBeenCalled(); expect(mockCrypto!.isCrossSigningReady).not.toHaveBeenCalled();
}); });
it("correctly handles the client being stopped", async () => {
mockCrypto!.isCrossSigningReady.mockImplementation(() => {
throw new ClientStoppedError();
});
await createAndStart();
expect(logger.error).not.toHaveBeenCalled();
});
it("correctly handles other errors", async () => {
mockCrypto!.isCrossSigningReady.mockImplementation(() => {
throw new Error("blah");
});
await createAndStart();
expect(logger.error).toHaveBeenCalledTimes(1);
});
describe("set up encryption", () => { describe("set up encryption", () => {
const rooms = [{ roomId: "!room1" }, { roomId: "!room2" }] as unknown as Room[]; const rooms = [{ roomId: "!room1" }, { roomId: "!room2" }] as unknown as Room[];