Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/tidy-clubs-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lynx-js/web-core": patch
---

fix: avoid to do use-after-free for rust instance
Comment thread
PupilTong marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class BackgroundThread implements AsyncDisposable {
#batchSendTimingInfo: RpcCallType<typeof markTimingEndpoint>;

readonly jsContext: LynxCrossThreadContext;
#messagePort?: MessagePort;

readonly postTimingFlags: RpcCallType<typeof postTimingFlagsEndpoint>;
readonly sendGlobalEvent: RpcCallType<typeof sendGlobalEventEndpoint>;
Expand Down Expand Up @@ -166,6 +167,7 @@ export class BackgroundThread implements AsyncDisposable {
} as WorkerStartMessage,
[messageChannel.port2],
);
this.#messagePort = messageChannel.port1;
this.#rpc.setMessagePort(messageChannel.port1);
}

Expand Down Expand Up @@ -303,6 +305,8 @@ export class BackgroundThread implements AsyncDisposable {
} else {
this.#webWorker?.terminate();
}
this.#messagePort?.close();
this.#messagePort = undefined;
this.#nextMacroTask && clearTimeout(this.#nextMacroTask);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,19 @@ export class ExposureServices {
}
this.#isExposureServiceOn = toEnable;
}

dispose() {
this.#exposureEnabledElementsToIntersectionObserver.forEach((observer) => {
observer.disconnect();
});
this.#exposureEnabledElementsToIntersectionObserver.clear();
this.#exposureEnabledElementsToOldExposureIdAttributeValue.clear();
this.#exposedElements.clear();
if (this.#globalExposureEventBatchTimer) {
clearTimeout(this.#globalExposureEventBatchTimer);
this.#globalExposureEventBatchTimer = null;
}
this.#globalExposureEventCache = [];
this.#globalDisexposureEventCache = [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { loadAllWebElements } from '../webElementsDynamicLoader.js';
// @ts-expect-error
import IN_SHADOW_CSS_MODERN from '../../../css/in_shadow.css?inline';
import type { LynxViewElement } from './LynxView.js';
import { requestIdleCallbackImpl } from './utils/requestIdleCallback.js';
loadAllWebElements().catch((e) => {
console.error('[lynx-web] Failed to load web elements', e);
});
Expand Down Expand Up @@ -297,7 +298,10 @@ export class LynxViewInstance implements AsyncDisposable {
}

async [Symbol.asyncDispose]() {
this.mtsWasmBinding.dispose();
await this.backgroundThread[Symbol.asyncDispose]();
this.exposureServices.dispose();
requestIdleCallbackImpl(() => {
this.mtsWasmBinding.dispose();
});
}
Comment thread
PupilTong marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,7 @@ export class WASMJSBinding implements RustMainthreadContextBinding {
this.lynxViewInstance.rootDom.removeEventListener(
eventName,
this.#commonEventHandler,
{
passive: true,
capture: true,
} as any,
true,
);
}
this.#addedEventListeners.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export function createElementAPI(
disposed = true;
if (wasmContext) {
wasmContext.free();
// @ts-expect-error It's better to throw an Error than triggering an use-after-free of rust struct
wasmContext = null;
}
page = undefined;
timingFlags.length = 0;
Expand Down
Loading