git » sdk » commit a3fc6ef

Never write ring before propose

author Stephen Paul Weber
2026-11-03 20:10:13 UTC
committer Stephen Paul Weber
2026-11-09 14:58:42 UTC
parent 898b81721248307094427b359d2c404b1c639325

Never write ring before propose

Ring is a sent event, in response to an inbound event, writing it first
would mess up which direction we think this call was in to begin with.

borogove/Client.hx +18 -13
test/persistence-tests.ts +83 -0

diff --git a/borogove/Client.hx b/borogove/Client.hx
index 02cfc38..3e4ed52 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -541,12 +541,28 @@ class Client extends EventEmitter {
 							}
 						});
 					};
+					final maybeRing = () -> {
+#if !NO_JINGLE
+						final jmiP = stanza.getChild("propose", "urn:xmpp:jingle-message:0");
+						if (jmiP == null || jmiP.attr.get("id") == null || from == null) return;
+						final session = new IncomingProposedSession(this, from, jmiP.attr.get("id"));
+						if (!chat.jingleSessions.exists(session.sid)) {
+							chat.jingleSessions.set(session.sid, session);
+							session.ring();
+						}
+#end
+					};
 					if (chatMessage.serverId == null) {
 						updateChat(chatMessage);
+						maybeRing();
 					} else {
 						maybeAutodownloadAttachments(chat, chatMessage).then(_ ->
-							storeMessages([chatMessage]).then((stored) -> updateChat(stored[0]))
-						);
+							storeMessages([chatMessage])
+						).then((stored) -> {
+							updateChat(stored[0]);
+							maybeRing();
+							return true;
+						});
 					}
 				}
 			case ReactionUpdateStanza(update):
@@ -620,17 +636,6 @@ class Client extends EventEmitter {
 		if (stanza.attr.get("type") == "error") return;
 
 #if !NO_JINGLE
-		final jmiP = stanza.getChild("propose", "urn:xmpp:jingle-message:0");
-		if (jmiP != null && jmiP.attr.get("id") != null) {
-			final session = new IncomingProposedSession(this, from, jmiP.attr.get("id"));
-			final chat = getDirectChat(from.asBare().asString());
-			if (!chat.jingleSessions.exists(session.sid)) {
-				chat.jingleSessions.set(session.sid, session);
-				chatActivity(chat);
-				session.ring();
-			}
-		}
-
 		final jmiR = stanza.getChild("retract", "urn:xmpp:jingle-message:0");
 		if (jmiR != null && jmiR.attr.get("id") != null) {
 			final chat = getDirectChat(from.asBare().asString());
diff --git a/test/persistence-tests.ts b/test/persistence-tests.ts
index 0aa77b1..feffe35 100644
--- a/test/persistence-tests.ts
+++ b/test/persistence-tests.ts
@@ -2290,6 +2290,89 @@ export function sharedPersistenceTests(test) {
 			original.text,
 		]);
 	});
+
+	test("ring-before-propose storage race keeps direction from the proposal", async ({
+		borogove,
+		persistence,
+	}) => {
+		const account = "race-alice@example.com";
+		const contact = "race-hatter@example.com";
+		const sid = "ring-before-propose";
+		const client = new borogove.Client(account, persistence);
+		const storeOrder: string[] = [];
+		const originalStoreMessages = persistence.storeMessages.bind(persistence);
+		let proposalStarted!: () => void;
+		let releaseProposal!: () => void;
+		const proposalStoreStarted = new Promise<void>((resolve) => {
+			proposalStarted = resolve;
+		});
+		const proposalStoreCanFinish = new Promise<void>((resolve) => {
+			releaseProposal = resolve;
+		});
+		let ringStoreStarted = false;
+		let ringStoreFinished!: () => void;
+		const ringStoreCanFinish = new Promise<void>((resolve) => {
+			ringStoreFinished = resolve;
+		});
+
+		persistence.storeMessages = async (accountId, messages) => {
+			const isProposal = messages.some(
+				(message) =>
+					message.type === borogove.MessageType.MessageCall &&
+					message.callStatus() === "propose",
+			);
+			const isRinging = messages.some(
+				(message) =>
+					message.type === borogove.MessageType.MessageCall &&
+					message.callStatus() === "ringing",
+			);
+			if (isProposal) {
+				proposalStarted();
+				await proposalStoreCanFinish;
+			}
+			if (isRinging) ringStoreStarted = true;
+			const result = await originalStoreMessages(accountId, messages);
+			for (const message of messages) {
+				if (message.type === borogove.MessageType.MessageCall) {
+					storeOrder.push(message.callStatus());
+				}
+			}
+			if (isRinging) ringStoreFinished();
+			return result;
+		};
+
+		try {
+			(client as any).processLiveMessage(
+				borogove.Stanza.parse(`
+					<message xmlns="jabber:client" from="${contact}/phone" to="${account}" type="chat" id="proposal-message-id">
+						<propose xmlns="urn:xmpp:jingle-message:0" id="${sid}">
+							<description xmlns="urn:xmpp:jingle:apps:rtp:1" media="audio"/>
+						</propose>
+						<stanza-id xmlns="urn:xmpp:sid:0" by="${account}" id="race-propose-server-id"/>
+					</message>
+				`),
+				null,
+			);
+
+			await proposalStoreStarted;
+			// Let the real ring path run while the proposal write is still blocked.
+			await new Promise((resolve) => setTimeout(resolve, 10));
+			expect(ringStoreStarted).toBe(false);
+			releaseProposal();
+			// Wait for the ring write instead of assuming it finishes in a fixed time.
+			await ringStoreCanFinish;
+
+			expect(storeOrder).toEqual(["propose", "ringing"]);
+			const stored = await persistence.getMessage(account, contact, null, sid);
+			expect(stored).not.toBeNull();
+			expect(stored?.direction).toBe(borogove.MessageDirection.MessageReceived);
+			expect(stored?.senderId).toBe(contact);
+		} finally {
+			releaseProposal();
+			persistence.storeMessages = originalStoreMessages;
+		}
+	});
+
 }
 
 type TestKeyPair = {