Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(loro-mirror): drop the doc listener before free (#6751)

dev-rbe3df9c9a4be2
diff --git a/packages/loro-mirror/src/core/mirror.ts b/packages/loro-mirror/src/core/mirror.ts
index 4af43937a0..e490fe68a1 100644
--- a/packages/loro-mirror/src/core/mirror.ts
+++ b/packages/loro-mirror/src/core/mirror.ts
@@ -180,6 +180,9 @@ export class Mirror<S extends SchemaType> {
// Unsubscribe functions for container subscriptions
private containerSubscriptions: Map<ContainerID, () => void> = new Map();
+ /** Root `doc.subscribe` listener. Must be dropped before `doc.free()`. */
+ private docUnsubscribe?: () => void;
+ private disposed = false;
private containerRegistry: ContainerRegistry = new Map();
@@ -215,7 +218,7 @@ export class Mirror<S extends SchemaType> {
this.initializeContainers();
// Subscribe to the root doc for global updates
- this.doc.subscribe(this.handleLoroEvent);
+ this.docUnsubscribe = this.doc.subscribe(this.handleLoroEvent);
}
/**
@@ -370,7 +373,7 @@ export class Mirror<S extends SchemaType> {
* Handle events from the LoroDoc
*/
private handleLoroEvent = (event: LoroEventBatch) => {
- if (this.syncing) return;
+ if (this.disposed || this.syncing) return;
if (event.origin === 'to-loro') return;
this.syncing = true;
@@ -456,7 +459,7 @@ export class Mirror<S extends SchemaType> {
* Handle events from individual containers
*/
private handleContainerEvent = (event: LoroEventBatch) => {
- if (this.syncing) return;
+ if (this.disposed || this.syncing) return;
if (event.origin === 'to-loro') return;
this.syncing = true;
@@ -1077,6 +1080,13 @@ export class Mirror<S extends SchemaType> {
* Clean up resources
*/
dispose() {
+ // Drop the doc listener before the owner calls `doc.free()`. Freeing a
+ // doc with an open transaction commits it from `Drop`, after the JS
+ // pointer is already zero, and this listener would call back into it.
+ this.disposed = true;
+ this.docUnsubscribe?.();
+ this.docUnsubscribe = undefined;
+
// Unsubscribe from all container subscriptions
for (const [_, unsubscribe] of this.containerSubscriptions) {
unsubscribe();
diff --git a/packages/loro-mirror/tests/core/mirror-dispose.test.ts b/packages/loro-mirror/tests/core/mirror-dispose.test.ts
new file mode 100644
index 0000000000..9f24ab045a
--- /dev/null
+++ b/packages/loro-mirror/tests/core/mirror-dispose.test.ts
@@ -0,0 +1,33 @@
+import { LoroDoc } from 'loro-crdt';
+import { describe, expect, it } from 'vitest';
+import { Mirror } from '../../src/core/mirror';
+
+describe('Mirror dispose', () => {
+ it('does not touch the doc when freeing one with an open transaction', async () => {
+ const doc = new LoroDoc();
+ const mirror = new Mirror({ doc });
+
+ // Uncommitted. LoroDoc.free() commits from Drop after the JS pointer is zero
+ // and flushes subscribers; calling back into the doc throws.
+ doc.getText('text').insert(0, 'pending');
+ mirror.dispose();
+
+ let onRejection: (error: unknown) => void = () => {};
+ const rejection = new Promise<unknown>((resolve) => {
+ onRejection = resolve;
+ process.on('unhandledRejection', onRejection);
+ });
+
+ doc.free();
+
+ const outcome = await Promise.race([
+ rejection.then((error) => ({ rejected: error })),
+ new Promise<{ rejected: false }>((resolve) =>
+ setTimeout(() => resolve({ rejected: false }), 20)
+ ),
+ ]);
+ process.off('unhandledRejection', onRejection);
+
+ expect(outcome.rejected).toBe(false);
+ });
+});