Skip to content

feat(client): mechanism for closing and reopening a client and its underlying stores#6495

Merged
stefanceriu merged 8 commits into
mainfrom
stefan/clientPausing
May 12, 2026
Merged

feat(client): mechanism for closing and reopening a client and its underlying stores#6495
stefanceriu merged 8 commits into
mainfrom
stefan/clientPausing

Conversation

@stefanceriu
Copy link
Copy Markdown
Member

@stefanceriu stefanceriu commented Apr 23, 2026

This patch exposes the pause/resume mechanism for SDK stores all the way up to the FFI Client, so apps can temporarily release SQLite resources when moving to the background and re-acquire them on resume.

The main use case is iOS backgrounding, where keeping SQLite file descriptors and locks open can contribute to 0xdead10cc terminations by the operating system.

The main change are limited to the sqlite crate with in memory stores no-oping trough the default trait implementations:

  • a new type called SqliteConnections has been introduced which now holds a store's read connection pools as well as the always on write connection. This lives as an optional on each store's level and gets set to None whenever the store is paused
  • during the pausing phase SqliteConnections (through its pause_connections method) does the following:
    • closes the read pool, directly dropping idle connections
    • waits for in flight writes to finish
    • tries a best effort WAL checkpoint
    • drops the write connection on a blocking thread
    • and waits for in-flight read connections to drain
    • this is all best effort with an eventual timeout
  • resuming connections is also shared between the stores through SqliteConnections and consists in building a new pool with the previous configuration and creating a new write connection

Handles #6366

Event with this in place though the client side isn't entirely straight forward as the sync service, send queue and pause/resume mechanisms still need to be coordinator in the right order. A better solution would be the to incorporate all of those within an app specific service on the rust side (which we've talked about multiple times over the years) and let the SDK coordinate them instead.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 23, 2026

Codecov Report

❌ Patch coverage is 89.62868% with 81 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.94%. Comparing base (7f01cfc) to head (9f70dba).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
crates/matrix-sdk-sqlite/src/media_store.rs 92.85% 11 Missing and 2 partials ⚠️
crates/matrix-sdk-sqlite/src/event_cache_store.rs 92.20% 11 Missing and 1 partial ⚠️
crates/matrix-sdk-base/src/client.rs 42.85% 0 Missing and 8 partials ⚠️
crates/matrix-sdk-crypto/src/store/memorystore.rs 0.00% 8 Missing ⚠️
crates/matrix-sdk-sqlite/src/connection.rs 84.31% 6 Missing and 2 partials ⚠️
crates/matrix-sdk-sqlite/src/crypto_store.rs 96.12% 0 Missing and 5 partials ⚠️
crates/matrix-sdk-sqlite/src/state_store.rs 96.96% 2 Missing and 3 partials ⚠️
...rix-sdk-base/src/event_cache/store/memory_store.rs 0.00% 4 Missing ⚠️
...es/matrix-sdk-base/src/media/store/memory_store.rs 0.00% 4 Missing ⚠️
crates/matrix-sdk-base/src/store/memory_store.rs 0.00% 4 Missing ⚠️
... and 3 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6495      +/-   ##
==========================================
+ Coverage   89.92%   89.94%   +0.01%     
==========================================
  Files         381      381              
  Lines      106886   107620     +734     
  Branches   106886   107620     +734     
==========================================
+ Hits        96119    96798     +679     
- Misses       7110     7161      +51     
- Partials     3657     3661       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@stefanceriu stefanceriu force-pushed the stefan/clientPausing branch from 9963065 to dc9bdd9 Compare April 23, 2026 15:29
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 23, 2026

Merging this PR will not alter performance

✅ 9 untouched benchmarks
⏩ 41 skipped benchmarks1


Comparing stefan/clientPausing (9f70dba) with main (7f01cfc)

Open in CodSpeed

Footnotes

  1. 41 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@stefanceriu stefanceriu force-pushed the stefan/clientPausing branch from dc9bdd9 to 487640f Compare April 23, 2026 15:52
@stefanceriu stefanceriu marked this pull request as ready for review April 23, 2026 16:28
@stefanceriu stefanceriu requested review from a team as code owners April 23, 2026 16:28
@stefanceriu stefanceriu requested review from andybalaam and removed request for a team April 23, 2026 16:28
Copy link
Copy Markdown
Member

@andybalaam andybalaam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks plausible, with one mild suggestion. I don't understand the area well, so feel free to ask someone else for more input if you want it.

Comment thread crates/matrix-sdk-base/src/event_cache/store/traits.rs Outdated
@stefanceriu
Copy link
Copy Markdown
Member Author

Thanks Andy! I'll guess I'll choose... @Hywan as my next victim 😁

@stefanceriu stefanceriu requested a review from Hywan April 24, 2026 10:15
Copy link
Copy Markdown
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! I made a couple of suggestions, nothing huge.

Comment thread crates/matrix-sdk-base/src/store/traits.rs Outdated
Comment thread crates/matrix-sdk-sqlite/src/connection.rs
Comment thread crates/matrix-sdk-sqlite/src/connection.rs Outdated
Comment thread crates/matrix-sdk-sqlite/src/connection.rs Outdated
Comment thread crates/matrix-sdk-sqlite/src/connection.rs Outdated
Comment thread bindings/matrix-sdk-ffi/src/client.rs
Comment thread bindings/matrix-sdk-ffi/src/client.rs
Comment thread crates/matrix-sdk/src/client/mod.rs Outdated
Comment thread crates/matrix-sdk/src/client/mod.rs Outdated
Comment thread crates/matrix-sdk/src/client/mod.rs Outdated
Copy link
Copy Markdown
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving ahead of time to save you the last review roundtrip. Thanks for the discussions!

…he, state and media SQLite stores

This patch implements the main pause/resume logic in the underlying SDK stores.
The in memory stores no-op trough the default trait implementations.

The main change are limited to the sqlite crate:
- a new type called `SqliteConnections` has been introduced which now holds a
store's read connection pools as well as the always on write connection. This
lives as an optional on each store's level and gets set to None whenever the
store is paused
- during the pausing phase `SqliteConnections` (through its `pause_connections`
method) does the following:
    - closes the read pool, directly dropping idle connections
    - waits for in flight writes to finish
    - tries a best effort WAL checkpoint
    - drops the write connection on a blocking thread
    - and waits for in-flight read connections to drain
    - this is all best effort with an eventual timeout
- resuming connections is also shared between the stores through
`SqliteConnections` and consists in building a new pool with the previous
configuration and creating a new write connection

# Conflicts:
#	crates/matrix-sdk-sqlite/src/crypto_store.rs
#	crates/matrix-sdk-sqlite/src/event_cache_store.rs
#	crates/matrix-sdk-sqlite/src/media_store.rs
#	crates/matrix-sdk-sqlite/src/state_store.rs
@stefanceriu stefanceriu force-pushed the stefan/clientPausing branch 2 times, most recently from 64175f0 to 0329514 Compare May 12, 2026 12:58
This patch exposes the pause/resume mechanism for SDK stores all the way up to
the FFI `Client`, so apps can temporarily release SQLite resources when moving
to the background and re-acquire them on resume.

The main use case is iOS backgrounding, where keeping SQLite file descriptors and
locks open can contribute to `0xdead10cc` terminations by the operating system.
@stefanceriu stefanceriu force-pushed the stefan/clientPausing branch from 0329514 to aaa7017 Compare May 12, 2026 13:00
@stefanceriu stefanceriu changed the title feat(client): mechanism for pausing and resuming a client and its underlying stores feat(client): mechanism for closing and reopening a client and its underlying stores May 12, 2026
@stefanceriu stefanceriu force-pushed the stefan/clientPausing branch from b75101e to 9f70dba Compare May 12, 2026 14:10
@stefanceriu stefanceriu enabled auto-merge (rebase) May 12, 2026 14:15
@stefanceriu stefanceriu merged commit 6792416 into main May 12, 2026
49 of 55 checks passed
@stefanceriu stefanceriu deleted the stefan/clientPausing branch May 12, 2026 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants