Skip to content

Commit 7192664

Browse files
committed
fix: temporarily removes beforeSync callback (#919, partially reverts a6a7bcd). Making applySync async causes timing issues that leads to issues when using PermanentUserData. This can be reproduced by using the playground server with sqlite and a sleep(1000). Additionally, use Y.PermanentUserData, or try to read data from the ydoc in the onSynced event.
1 parent c129ee9 commit 7192664

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

packages/server/src/Connection.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import type Document from "./Document.ts";
99
import { IncomingMessage } from "./IncomingMessage.ts";
1010
import { MessageReceiver } from "./MessageReceiver.ts";
1111
import { OutgoingMessage } from "./OutgoingMessage.ts";
12-
import type { beforeSyncPayload, onTokenSyncPayload, onStatelessPayload } from "./types.ts";
12+
import type {
13+
beforeSyncPayload,
14+
onStatelessPayload,
15+
onTokenSyncPayload,
16+
} from "./types.ts";
1317

1418
export class Connection {
1519
webSocket: WebSocket;
@@ -29,7 +33,8 @@ export class Connection {
2933
payload: Pick<beforeSyncPayload, "type" | "payload">,
3034
) => Promise.resolve(),
3135
statelessCallback: (payload: onStatelessPayload) => Promise.resolve(),
32-
onTokenSyncCallback: (payload: Partial<onTokenSyncPayload>) => Promise.resolve(),
36+
onTokenSyncCallback: (payload: Partial<onTokenSyncPayload>) =>
37+
Promise.resolve(),
3338
};
3439

3540
socketId: string;
@@ -107,7 +112,7 @@ export class Connection {
107112
return this;
108113
}
109114

110-
/**
115+
/**
111116
* Set a callback that will be triggered when on token sync message is received
112117
*/
113118
onTokenSyncCallback(
@@ -154,7 +159,9 @@ export class Connection {
154159
* Request current token from the client
155160
*/
156161
public requestToken(): void {
157-
const message = new OutgoingMessage(this.document.name).writeTokenSyncRequest();
162+
const message = new OutgoingMessage(
163+
this.document.name,
164+
).writeTokenSyncRequest();
158165

159166
this.send(message.toUint8Array());
160167
}
@@ -209,18 +216,18 @@ export class Connection {
209216
this.callbacks
210217
.beforeHandleMessage(this, data)
211218
.then(() => {
212-
new MessageReceiver(message)
213-
.apply(this.document, this)
214-
.catch((e: any) => {
215-
console.error(
216-
`closing connection ${this.socketId} (while handling ${documentName}) because of exception`,
217-
e,
218-
);
219-
this.close({
220-
code: "code" in e ? e.code : ResetConnection.code,
221-
reason: "reason" in e ? e.reason : ResetConnection.reason,
222-
});
219+
try {
220+
new MessageReceiver(message).apply(this.document, this);
221+
} catch (e) {
222+
console.error(
223+
`closing connection ${this.socketId} (while handling ${documentName}) because of exception`,
224+
e,
225+
);
226+
this.close({
227+
code: "code" in e ? e.code : ResetConnection.code,
228+
reason: "reason" in e ? e.reason : ResetConnection.reason,
223229
});
230+
}
224231
})
225232
.catch((e: any) => {
226233
console.error(

packages/server/src/MessageReceiver.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AuthMessageType } from "@hocuspocus/common";
12
import * as decoding from "lib0/decoding";
23
import { readVarString } from "lib0/decoding";
34
import { applyAwarenessUpdate } from "y-protocols/awareness";
@@ -15,7 +16,6 @@ import type Document from "./Document.ts";
1516
import type { IncomingMessage } from "./IncomingMessage.ts";
1617
import { OutgoingMessage } from "./OutgoingMessage.ts";
1718
import { MessageType } from "./types.ts";
18-
import { AuthMessageType } from "@hocuspocus/common";
1919

2020
export class MessageReceiver {
2121
message: IncomingMessage;
@@ -27,7 +27,7 @@ export class MessageReceiver {
2727
this.defaultTransactionOrigin = defaultTransactionOrigin;
2828
}
2929

30-
public async apply(
30+
public apply(
3131
document: Document,
3232
connection?: Connection,
3333
reply?: (message: Uint8Array) => void,
@@ -40,7 +40,7 @@ export class MessageReceiver {
4040
case MessageType.Sync:
4141
case MessageType.SyncReply: {
4242
message.writeVarUint(MessageType.Sync);
43-
await this.readSyncMessage(
43+
this.readSyncMessage(
4444
message,
4545
document,
4646
connection,
@@ -65,7 +65,7 @@ export class MessageReceiver {
6565
break;
6666
}
6767
case MessageType.Awareness: {
68-
await applyAwarenessUpdate(
68+
applyAwarenessUpdate(
6969
document.awareness,
7070
message.readVarUint8Array(),
7171
connection?.webSocket,
@@ -111,7 +111,7 @@ export class MessageReceiver {
111111
token: message.readVarString(),
112112
});
113113
break;
114-
}
114+
}
115115
console.error(
116116
"Received an authentication message on a connection that is already fully authenticated. Probably your provider has been destroyed + recreated really fast.",
117117
);
@@ -126,7 +126,7 @@ export class MessageReceiver {
126126
}
127127
}
128128

129-
async readSyncMessage(
129+
readSyncMessage(
130130
message: IncomingMessage,
131131
document: Document,
132132
connection?: Connection,
@@ -136,7 +136,7 @@ export class MessageReceiver {
136136
const type = message.readVarUint();
137137

138138
if (connection) {
139-
await connection.callbacks.beforeSync(connection, {
139+
connection.callbacks.beforeSync(connection, {
140140
type,
141141
payload: message.peekVarUint8Array(),
142142
});

0 commit comments

Comments
 (0)