Skip to content

Commit 42d982d

Browse files
authored
OutgoingRequestProcessor: do not throw errors if shutting down (#3683)
* `OutgoingRequestProcessor`: do not throw errors if shutting down * Optimised builds throw a different error
1 parent f406ffd commit 42d982d

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

spec/unit/rust-crypto/OutgoingRequestProcessor.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import {
2929
} from "@matrix-org/matrix-sdk-crypto-wasm";
3030

3131
import { TypedEventEmitter } from "../../../src/models/typed-event-emitter";
32-
import { HttpApiEvent, HttpApiEventHandlerMap, MatrixHttpApi, UIAuthCallback } from "../../../src";
32+
import { HttpApiEvent, HttpApiEventHandlerMap, IHttpOpts, MatrixHttpApi, UIAuthCallback } from "../../../src";
3333
import { OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor";
34+
import { defer } from "../../../src/utils";
3435

3536
describe("OutgoingRequestProcessor", () => {
3637
/** the OutgoingRequestProcessor implementation under test */
@@ -218,4 +219,40 @@ describe("OutgoingRequestProcessor", () => {
218219
await Promise.all([processor.makeOutgoingRequest(outgoingRequest), markSentCallPromise]);
219220
expect(olmMachine.markRequestAsSent).toHaveBeenCalledWith("5678", 987, "");
220221
});
222+
223+
it("does not explode if the OlmMachine is stopped while the request is in flight", async () => {
224+
// we use a real olm machine for this test
225+
const olmMachine = await RustSdkCryptoJs.OlmMachine.initialize(
226+
new RustSdkCryptoJs.UserId("@alice:example.com"),
227+
new RustSdkCryptoJs.DeviceId("TEST_DEVICE"),
228+
);
229+
230+
const authRequestResultDefer = defer<string>();
231+
232+
const authRequestCalledPromise = new Promise<void>((resolve) => {
233+
const mockHttpApi = {
234+
authedRequest: async () => {
235+
resolve();
236+
return await authRequestResultDefer.promise;
237+
},
238+
} as unknown as Mocked<MatrixHttpApi<IHttpOpts & { onlyData: true }>>;
239+
processor = new OutgoingRequestProcessor(olmMachine, mockHttpApi);
240+
});
241+
242+
// build a request
243+
const request = olmMachine.queryKeysForUsers([new RustSdkCryptoJs.UserId("@bob:example.com")]);
244+
const result = processor.makeOutgoingRequest(request);
245+
246+
// wait for the HTTP request to be made
247+
await authRequestCalledPromise;
248+
249+
// while the HTTP request is in flight, the OlmMachine gets stopped.
250+
olmMachine.close();
251+
252+
// the HTTP request completes...
253+
authRequestResultDefer.resolve("{}");
254+
255+
// ... and `makeOutgoingRequest` resolves satisfactorily
256+
await result;
257+
});
221258
});

src/rust-crypto/OutgoingRequestProcessor.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,20 @@ export class OutgoingRequestProcessor {
105105
}
106106

107107
if (msg.id) {
108-
await this.olmMachine.markRequestAsSent(msg.id, msg.type, resp);
108+
try {
109+
await this.olmMachine.markRequestAsSent(msg.id, msg.type, resp);
110+
} catch (e) {
111+
// Ignore errors which are caused by the olmMachine having been freed. The exact error message depends
112+
// on whether we are using a release or develop build of rust-sdk-crypto-wasm.
113+
if (
114+
e instanceof Error &&
115+
(e.message === "Attempt to use a moved value" || e.message === "null pointer passed to rust")
116+
) {
117+
logger.log(`Ignoring error '${e.message}': client is likely shutting down`);
118+
} else {
119+
throw e;
120+
}
121+
}
109122
}
110123
}
111124

0 commit comments

Comments
 (0)