Analysis, debugging tools, and a proposed fix for the issue where searching for a user opens a group DM instead of a 1:1 conversation.
Matrix is a federated messaging protocol. Understanding two concepts is key to this bug:
rooms and the m.direct account data.
Every conversation in Matrix is a room—whether it's a 1:1 chat, a group DM,
or a public channel. Rooms have a unique ID like !abcdef:matrix.org and
track their own membership list. A room doesn't inherently "know" whether it's a DM; that
distinction is layered on top by clients.
m.direct Account Data
Matrix stores a per-account blob called m.direct that tells clients which rooms
the user considers to be "direct" conversations. It's a JSON object mapping
user IDs to arrays of room IDs:
{
"@alice:matrix.org": ["!room1:matrix.org", "!room2:matrix.org"],
"@bob:matrix.org": ["!room3:matrix.org"]
}
This is client-managed—the server doesn't enforce it. Clients write
to it when creating DMs, but the data can easily become inconsistent. For instance, a group
DM created with Alice and Bob might only be recorded under Alice's entry, depending on
which client created it and how that client chose to update m.direct.
A room is a 1:1 DM when both of these are true:
m.direct)A room can be flagged as "direct" but still have 3+ members—that's a group DM. The distinction matters, and the bug is about getting it wrong.
When you search for a user and tap their name, the app goes through a specific code path. Here's the chain, simplified:
startDM()
In StartDM.kt, the logic first tries to find an existing DM.
If one is found, it's reused. Otherwise a new room is created:
suspend fun MatrixClient.startDM(
userId: UserId,
createIfDmDoesNotExist: Boolean,
): StartDMResult {
return findDM(userId)
.fold(
onSuccess = { existingDM ->
if (existingDM != null) {
StartDMResult.Success(existingDM, isNew = false)
} else if (createIfDmDoesNotExist) {
createDM(userId).fold(...)
} else { ... }
}, ...
)
}
getDmRoom()
The findDM() call in Kotlin delegates to the Rust SDK's get_dm_room():
// Rust: crates/matrix-sdk/src/client/mod.rs
pub fn get_dm_room(&self, user_id: &UserId) -> Option<Room> {
let rooms = self.joined_rooms();
// Find the room we share with the `user_id` and only with `user_id`
rooms.into_iter().find(|r| {
let targets = r.direct_targets();
targets.len() == 1 && targets.contains(user_id)
})
}
This checks that direct_targets (derived from m.direct) has
exactly one entry matching the requested user. But it does not check
how many people are actually in the room.
The Rust SDK's direct_targets comes from the m.direct account data—it
records which user IDs the room is filed under, not how many people are in
the room. These are two different things.
m.direct under only one user
(e.g. Alice). This is common—m.direct is an imprecise client-managed signal.
m.direct and sets dm_targets = {Alice}
for that room. Length is 1.
getDmRoom("Alice") scans your rooms, finds the group DM where
targets.len() == 1 && targets.contains(Alice) — match!
The codebase already has a correct DM-checking function in RoomIsDmCheck.kt:
fun isDm(isDirect: Boolean, activeMembersCount: Int): Boolean {
return isDirect && activeMembersCount <= 2
}
This check is used elsewhere in the app (e.g. in the recent conversations list), but it is
not applied in findDM(). The findDM() method
blindly trusts whatever the Rust SDK returns.
The fix adds an activeMembersCount check to the Kotlin findDM()
method. If the room returned by the Rust SDK has more than 2 active members, it's not a
true 1:1 DM, so null is returned instead—causing the app to create a
proper new DM room:
@@ libraries/matrix/impl/.../RustMatrixClient.kt @@
override suspend fun findDM(userId: UserId): Result<RoomId?> = ...{
runCatchingExceptions {
- innerClient.getDmRoom(userId.value)?.use { RoomId(it.id()) }
+ innerClient.getDmRoom(userId.value)?.use { room ->
+ // getDmRoom can return group DMs where the user is a member
+ // but the room has more than 2 active participants. Only
+ // treat the room as a true 1-on-1 DM when the active member
+ // count is at most 2 (the current user + the target user).
+ val activeMemberCount = room.activeMembersCount().toLong()
+ if (activeMemberCount <= 2) RoomId(room.id()) else null
+ }
}
}
You can review the complete patch file: fix-findDM-group-dm.patch
We provide a shell script that queries the Matrix API to inspect your m.direct
data and cross-references it with the actual membership of each room. It will flag any
rooms that would trigger the bug.
bash, curl, and python3 (present on most Linux/macOS systems)@you:matrix.org)In Element X Android: Settings → Advanced settings → Session token. You may need to enable Developer options first in Settings.
# Option 1: Read the token into a variable without echoing it
read -rs TOKEN
./inspect-mdirect.sh @you:matrix.org "$TOKEN"
unset TOKEN
# Option 2: Prefix with a space (works if your shell has
# HISTCONTROL=ignorespace or HIST_IGNORE_SPACE set)
./inspect-mdirect.sh @you:matrix.org YOUR_TOKEN_HERE
# Download
curl -O https://<this-server>/files/inspect-mdirect.sh
chmod +x inspect-mdirect.sh
# Run
./inspect-mdirect.sh @you:matrix.org YOUR_ACCESS_TOKEN
The script performs only read-only GET requests against the Matrix API. It does not modify any data. Here is exactly what it does, step by step:
:) and fetches
https://<domain>/.well-known/matrix/client to find the
actual homeserver URL. Falls back to https://<domain> if
.well-known is unavailable.
m.direct.
Calls GET /_matrix/client/v3/user/<your_id>/account_data/m.direct.
This returns the JSON object mapping user IDs to room IDs.
m.direct, calls
GET /_matrix/client/v3/rooms/<room_id>/joined_members
to get the actual member list.
m.direct (the "dm_targets" count the Rust SDK sees)dm_targets == 1 but actual members > 2 are flagged
as *** BUG TRIGGER.
# This is a true 1:1 DM — no problem
OK (true 1:1 DM): !abc:matrix.org
m.direct targets: 1
Actual members: 2
Members: @you:matrix.org, @alice:matrix.org
# This triggers the bug!
*** BUG TRIGGER: !xyz:matrix.org
m.direct target: @alice:matrix.org
^ Search for this user in Element X to reproduce the bug
Actual members: 3 (this is really a group DM!)
Members: @you:matrix.org, @alice:matrix.org, @bob:matrix.org
If you see any *** BUG TRIGGER lines, your account is affected.
The script tells you exactly which user to search for in Element X to reproduce
the bug—it's the m.direct target line. Searching for that user
in the unpatched app will open the group DM instead of a 1:1 conversation.
The build runs entirely inside a Docker container so you don't need to install the Android SDK or JDK on your host machine. All you need is Docker (or Podman).
You need three files, all in the same directory:
# Download all three into one directory
mkdir element-x-fix && cd element-x-fix
curl -O https://<this-server>/files/Dockerfile
curl -O https://<this-server>/files/fix-findDM-group-dm.patch
curl -O https://<this-server>/files/build-patched-apk.sh
chmod +x build-patched-apk.sh
fix-findDM-group-dm.patch)This is the only change to Element X's source code. It modifies one method in one file. You can read it in full—it's 21 lines including context:
findDM() calls getDmRoom() and returns
whatever room the Rust SDK provides, without any validation.
findDM() calls room.activeMembersCount()
on the returned room. If the count is > 2, the room is a group DM, so null
is returned instead. This causes startDM() to create a new 1:1 room.
The Dockerfile builds the APK in these stages (each is a separate layer you can inspect):
| Step | What it does | Why |
|---|---|---|
pacman -S jdk21-openjdk ... |
Installs JDK 21, git, curl, unzip | Build toolchain requirements |
curl ... commandlinetools |
Downloads Android SDK command-line tools from Google | Needed to install SDK components |
sdkmanager ... |
Installs Android platform 36, build-tools 35 & 36 | Compile targets required by the project |
git clone ... element-x-android |
Clones the official Element X Android repo (shallow) | The source code to build |
git apply fix-findDM-group-dm.patch |
Applies our 7-line fix | The bug fix |
./gradlew :app:assembleGplayDebug |
Compiles the app into debug APKs | Produces installable APKs |
The build skips lint, tests, detekt, and ktlint (-x lint -x test -x detekt -x ktlintCheck)
to save time. These are code quality checks, not compilation steps.
build-patched-apk.sh)
This is a thin wrapper that runs docker build, creates a temporary container,
copies the APKs out of it to an output/ directory, and cleans up. You can
read it in under a minute—it's 25 lines.
./build-patched-apk.sh
This will take 10–20 minutes depending on your internet speed and CPU.
When done, the APKs will be in output/:
| File | Size | Use for |
|---|---|---|
app-gplay-arm64-v8a-debug.apk | ~142 MB | Most modern phones (2017+) |
app-gplay-armeabi-v7a-debug.apk | ~117 MB | Older 32-bit ARM phones |
app-gplay-x86_64-debug.apk | ~148 MB | x86_64 emulators |
app-gplay-universal-debug.apk | ~350 MB | Any architecture (if unsure) |
app-gplay-arm64-v8a-debug.apk.
io.element.android.x.debug), so it installs alongside the
production app. Your existing Element X installation is not affected.
*** BUG TRIGGER entries.
Each one includes an m.direct target line showing the specific user
to search for (e.g. @alice:matrix.org).
After testing, you can uninstall the debug build normally from Android settings. Remember to:
Affected project: element-hq/element-x-android.
Bug location: libraries/matrix/impl/.../RustMatrixClient.kt line 344.
Upstream Rust SDK function: matrix-rust-sdk/crates/matrix-sdk/src/client/mod.rs get_dm_room().