Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Raise room floors in id order when deleting messages

Petr Bauch9474c40b3b07
diff --git a/core/switch_core/db/stores/message_store.py b/core/switch_core/db/stores/message_store.py
index 4845b2e09..d43762d51 100644
--- a/core/switch_core/db/stores/message_store.py
+++ b/core/switch_core/db/stores/message_store.py
@@ -255,6 +255,12 @@ class MessageStore:
Each room's `seq_floor` is raised to the highest live position deleted
from it, in the same transaction, so `_next_seq` never reissues one.
+ This takes no room advisory lock and needs none: `_ROOM_HEAD` reads
+ the newest row and the floor in one statement, so a sender sees the
+ deletion whole or not at all, and the floor's UPDATE takes a row lock
+ that a sender's foreign-key check does not wait on. The rooms are
+ updated in id order, so two deletions at once (each replica runs
+ retention) lock them in the same order and cannot deadlock.
The bytes behind the attachments are not touched: a blob may be quoted
by a message that is kept. The result names them, and
`RetentionStore.delete_unreferenced_media` finds the ones nothing
@@ -294,7 +300,7 @@ class MessageStore:
floors: dict[str, int] = {}
for row in rows:
floors[row.room_id] = max(floors.get(row.room_id, 0), row.seq)
- for room_id, seq in floors.items():
+ for room_id, seq in sorted(floors.items()):
if seq > 0:
await session.execute(
update(Room)
diff --git a/core/tests/switch_core/retention/test_service.py b/core/tests/switch_core/retention/test_service.py
index fb8874684..5ba839dbd 100644
--- a/core/tests/switch_core/retention/test_service.py
+++ b/core/tests/switch_core/retention/test_service.py
@@ -13,7 +13,7 @@ import uuid
from datetime import UTC, datetime, timedelta
import pytest
-from sqlalchemy import func, select
+from sqlalchemy import event, func, select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from switch_core.attachments import ATTACHMENT_GROUP_KEY
@@ -343,6 +343,69 @@ class TestNumbering:
assert refreshed.seq_floor == 0
assert await store.head_seq(session, room.id) == 0
+ async def test_a_sender_does_not_wait_on_a_deletion_and_numbers_above_it(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ """A deletion of the room's newest row, uncommitted, holds the room's
+ row lock; a send to the room goes ahead meanwhile, and neither
+ reissues a position."""
+ store = MessageStore()
+ async with session_factory() as session:
+ room = await _room(session, "busy")
+ await _message(session, room, "$newest", age_days=0)
+ await session.commit()
+ room_id = room.id
+
+ async with (
+ tenant_session(session_factory, TENANT_ZERO_ID) as deleting,
+ session_factory() as sending,
+ ):
+ await store.delete_sent_by(
+ deleting, transport_user_id="@sender:test", limit=10
+ )
+ async with asyncio.timeout(5):
+ sent = await _message(sending, room, "$during", age_days=0)
+ await sending.commit()
+ await deleting.commit()
+
+ async with session_factory() as session:
+ after = await _message(session, room, "$after", age_days=0)
+ await session.commit()
+ seqs = (
+ await session.execute(
+ select(Message.seq).where(Message.room_id == room_id)
+ )
+ ).scalars()
+ assert sent.seq == 2
+ assert after.seq == 3
+ assert sorted(seqs) == [2, 3]
+
+ async def test_rooms_are_locked_in_id_order_whatever_order_the_messages_came(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ async with session_factory() as session:
+ first, second = await _room(session, "one"), await _room(session, "two")
+ later, earlier = sorted([first, second], key=lambda r: r.id, reverse=True)
+ await _message(session, later, "$older", age_days=90)
+ await _message(session, earlier, "$newer", age_days=60)
+ await session.commit()
+ updated: list[str] = []
+
+ def record(conn, cursor, statement, parameters, context, executemany): # type: ignore[no-untyped-def]
+ if statement.lstrip().upper().startswith("UPDATE ROOMS"):
+ updated.extend(p for p in parameters if p in {first.id, second.id})
+
+ async with tenant_session(session_factory, TENANT_ZERO_ID) as session:
+ engine = session.bind.sync_engine # type: ignore[union-attr]
+ event.listen(engine, "before_cursor_execute", record)
+ try:
+ await MessageStore().delete_sent_before(session, NOW, limit=10)
+ finally:
+ event.remove(engine, "before_cursor_execute", record)
+ await session.rollback()
+
+ assert updated == [earlier.id, later.id]
+
class TestMedia:
async def test_only_old_files_nothing_refers_to_are_deleted(