Skip to content

Commit 67957ae

Browse files
fix: on awareness craah prevention and memory leak onStoreDocument (#1032)
* fix: on awareness craah prevention and memory leak onStoreDocument * fix: prevent awareness crash and memory leak in onStoreDocument - Fix BUG #1: Lock memory leak - store acquired lock in locks map - Fix BUG #3: Awareness crash when no connections - check connections before publish Fixes #1027 * chore: removed prettier locally * reformat --------- Co-authored-by: Jan Thurau <jan@janthurau.de>
1 parent cc04534 commit 67957ae

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ npm-debug.log*
1313
yarn-debug.log*
1414
yarn-error.log*
1515
/database
16+
.prettierignore

packages/extension-redis/src/Redis.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,12 @@ export class Redis implements Extension {
246246
const resource = this.lockKey(documentName);
247247
const ttl = this.configuration.lockTimeout;
248248
try {
249-
await this.redlock.acquire([resource], ttl);
249+
const lock = await this.redlock.acquire([resource], ttl);
250250
const oldLock = this.locks.get(resource);
251-
if (oldLock) {
251+
if (oldLock?.release) {
252252
await oldLock.release;
253253
}
254+
this.locks.set(resource, { lock });
254255
} catch (error) {
255256
//based on: https://github.com/sesamecare/redlock/blob/508e00dcd1e4d2bc6373ce455f4fe847e98a9aab/src/index.ts#L347-L349
256257
if (
@@ -328,7 +329,13 @@ export class Redis implements Extension {
328329
added,
329330
updated,
330331
removed,
332+
document,
331333
}: onAwarenessUpdatePayload) {
334+
// Do not publish if there is no connection: it fixes this issue: "https://github.com/ueberdosis/hocuspocus/issues/1027"
335+
const connections = document?.connections.size || 0;
336+
if (connections === 0) {
337+
return; // avoids exception
338+
}
332339
const changedClients = added.concat(updated, removed);
333340
const message = new OutgoingMessage(
334341
documentName,

0 commit comments

Comments
 (0)