Skip to content

Commit 7ade2b4

Browse files
authored
[wasm] rewrite crypto-worker to typescript (#73073)
* rewrite of crypto-worker to typescript * returning some noisy console logs in hope that they are serializing access and preventing the deadlock. This is RC1 stop-gap.
1 parent 667c170 commit 7ade2b4

File tree

3 files changed

+410
-406
lines changed

3 files changed

+410
-406
lines changed

src/mono/wasm/runtime/crypto-worker.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
import { Module } from "./imports";
4+
import { Module, runtimeHelpers } from "./imports";
55
import { mono_assert } from "./types";
66

77
class OperationFailedError extends Error { }
@@ -112,11 +112,13 @@ export function init_crypto(): void {
112112
channel: chan,
113113
worker: worker,
114114
};
115-
worker.postMessage({
115+
const messageData: InitCryptoMessageData = {
116+
config: JSON.stringify(runtimeHelpers.config),
116117
comm_buf: chan.get_comm_buffer(),
117118
msg_buf: chan.get_msg_buffer(),
118119
msg_char_len: chan.get_msg_len()
119-
});
120+
};
121+
worker.postMessage(messageData);
120122
worker.onerror = event => {
121123
console.warn(`MONO_WASM: Error in Crypto WebWorker. Cryptography digest calls will fallback to managed implementation. Error: ${event.message}`);
122124
mono_wasm_crypto = null;
@@ -202,9 +204,10 @@ class LibraryChannel {
202204

203205
public send_msg(msg: string): string {
204206
try {
205-
// const state = Atomics.load(this.comm, this.STATE_IDX);
206-
// if (state !== this.STATE_IDLE) console.debug(`MONO_WASM_ENCRYPT_DECRYPT: send_msg, waiting for idle now, ${state}`);
207-
this.wait_for_state(pstate => pstate == this.STATE_IDLE, "waiting");
207+
let state = Atomics.load(this.comm, this.STATE_IDX);
208+
// FIXME: this console write is possibly serializing the access and prevents a deadlock
209+
if (state !== this.STATE_IDLE) console.debug(`MONO_WASM_ENCRYPT_DECRYPT: send_msg, waiting for idle now, ${state}`);
210+
state = this.wait_for_state(pstate => pstate == this.STATE_IDLE, "waiting");
208211

209212
this.send_request(msg);
210213
return this.read_response();
@@ -213,13 +216,14 @@ class LibraryChannel {
213216
throw err;
214217
}
215218
finally {
216-
// const state = Atomics.load(this.comm, this.STATE_IDX);
217-
// if (state !== this.STATE_IDLE) console.debug(`MONO_WASM_ENCRYPT_DECRYPT: state at end of send_msg: ${state}`);
219+
const state = Atomics.load(this.comm, this.STATE_IDX);
220+
// FIXME: this console write is possibly serializing the access and prevents a deadlock
221+
if (state !== this.STATE_IDLE) console.debug(`MONO_WASM_ENCRYPT_DECRYPT: state at end of send_msg: ${state}`);
218222
}
219223
}
220224

221225
public shutdown(): void {
222-
// console.debug("MONO_WASM_ENCRYPT_DECRYPT: Shutting down crypto");
226+
console.debug("MONO_WASM_ENCRYPT_DECRYPT: Shutting down crypto");
223227
const state = Atomics.load(this.comm, this.STATE_IDX);
224228
if (state !== this.STATE_IDLE)
225229
throw new Error(`OWNER: Invalid sync communication channel state: ${state}`);
@@ -230,15 +234,14 @@ class LibraryChannel {
230234
Atomics.notify(this.comm, this.STATE_IDX);
231235
}
232236

233-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
234237
private reset(reason: string): void {
235-
// console.debug(`MONO_WASM_ENCRYPT_DECRYPT: reset: ${reason}`);
238+
console.debug(`MONO_WASM_ENCRYPT_DECRYPT: reset: ${reason}`);
236239
const state = Atomics.load(this.comm, this.STATE_IDX);
237240
if (state === this.STATE_SHUTDOWN)
238241
return;
239242

240243
if (state === this.STATE_RESET || state === this.STATE_IDLE) {
241-
// console.debug(`MONO_WASM_ENCRYPT_DECRYPT: state is already RESET or idle: ${state}`);
244+
console.debug(`MONO_WASM_ENCRYPT_DECRYPT: state is already RESET or idle: ${state}`);
242245
return;
243246
}
244247

@@ -386,3 +389,10 @@ class LibraryChannel {
386389
return new LibraryChannel(msg_char_len);
387390
}
388391
}
392+
393+
export type InitCryptoMessageData = {
394+
config: string,// serialized to avoid passing non-clonable objects
395+
comm_buf: SharedArrayBuffer,
396+
msg_buf: SharedArrayBuffer,
397+
msg_char_len: number
398+
}

0 commit comments

Comments
 (0)