Skip to content

Commit

Permalink
fix: Memory leak in Direct Connection (#895)
Browse files Browse the repository at this point in the history
* trigger 'onDisconnect' and unloadDocument if direct connection is ONLY connection

* fix lint issue

* fix lint issue #2

---------

Co-authored-by: bcarleton3 <[email protected]>
  • Loading branch information
bencarletonn and bcarleton3 authored Feb 20, 2025
1 parent c749761 commit 70ab0e2
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/server/src/DirectConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ export class DirectConnection implements DirectConnectionInterface {
socketId: 'server',
}, true)

// If the direct connection was the only connection to the document
// then we should trigger the onDisconnect hook for
// this doc and unload the document
if (this.document.getConnectionsCount() === 0) {
await this.instance.hooks('onDisconnect', {
instance: this.instance,
clientsCount: this.document.getConnectionsCount(),
context: this.context,
document: this.document,
socketId: 'server',
documentName: this.document.name,
requestHeaders: {},
requestParameters: new URLSearchParams(),
})

await this.instance.unloadDocument(this.document)
}

this.document = null
}
}
Expand Down

0 comments on commit 70ab0e2

Please sign in to comment.