Files
chatmail-core/deltachat-jsonrpc
link2xt 70563867a6 fix(jsonrpc): fix deadlock in get_all_accounts()
`self.accounts.read().await.get_all()` acquires a read lock
and does not release it until the end of `for` loop.
After that, a writer may get into the queue,
e.g. because of the concurrent `add_account` call.
In this case `let context_option = self.accounts.read().await.get_account(id);`
tries to acquire another read lock and deadlocks
because tokio RwLock is write-preferring and will not
give another read lock while there is a writer in the queue.
At the same time, writer never gets a write lock
because the first read lock is not released.

The fix is to get a single read lock
for the whole `get_all_accounts()` call.

This is described in <https://docs.rs/tokio/1.44.1/tokio/sync/struct.RwLock.html#method.read>:
"Note that under the priority policy of RwLock, read locks are not
granted until prior write locks, to prevent starvation. Therefore
deadlock may occur if a read lock is held by the current task, a write
lock attempt is made, and then a subsequent read lock attempt is made by
the current task."
2025-04-01 12:31:18 +00:00
..
2025-03-29 16:40:10 +00:00

deltachat-jsonrpc

This crate provides a JSON-RPC 2.0 interface to DeltaChat.

The JSON-RPC API is exposed in two fashions:

  • A executable deltachat-rpc-server that exposes the JSON-RPC API through stdio.
  • The JSON-RPC API can also be called through the C FFI. It exposes the functions dc_jsonrpc_init, dc_jsonrpc_request, dc_jsonrpc_next_response and dc_jsonrpc_unref. See the docs in the header file for details.

We also include a JavaScript and TypeScript client for the JSON-RPC API. The source for this is in the typescript folder.

Usage

Using the TypeScript/JavaScript client

The package includes a JavaScript/TypeScript client which is partially auto-generated through the JSON-RPC library used by this crate (yerpc). Find the source in the typescript folder.

To use it locally, first install the dependencies and compile the TypeScript code to JavaScript:

cd typescript
npm install
npm run build

The JavaScript client is published on NPM.

A script is included to build autogenerated documentation, which includes all RPC methods:

cd typescript
npm run docs

Then open the typescript/docs folder in a web browser.

Development

Running the example app

Testing

The crate includes both a basic Rust smoke test and more featureful integration tests that use the TypeScript client.

Rust tests

To run the Rust test, use this command:

cargo test

TypeScript tests

cd typescript
npm run test

This will build the deltachat-jsonrpc-server binary and then run a test suite.

The test suite includes some tests that need online connectivity and a way to create test email accounts. To run these tests, set the CHATMAIL_DOMAIN environment variable to your testing email server domain.

CHATMAIL_DOMAIN=ci-chatmail.testrun.org npm run test

Test Coverage

Running npm run test will report test coverage. For the coverage to be accurate the online tests need to be run.

If you are offline and want to see the coverage results anyway (even though they are inaccurate), you can bypass the errors of the online tests by setting the COVERAGE_OFFLINE=1 environment variable.

A summary of the coverage will be reported in the terminal after the test run. Open coverage/index.html in a web browser for a detailed report.