diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a141a535..367c5cbc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ ### Changes ### Fixes +- deltachat-rpc-server: do not block stdin while processing the request. #4041 + deltachat-rpc-server now reads the next request as soon as previous request handler is spawned. ### API-Changes - Remove `MimeMessage::from_bytes()` public interface. #4033 diff --git a/deltachat-jsonrpc/src/api/mod.rs b/deltachat-jsonrpc/src/api/mod.rs index 9df681cb3..49ed900c3 100644 --- a/deltachat-jsonrpc/src/api/mod.rs +++ b/deltachat-jsonrpc/src/api/mod.rs @@ -85,6 +85,11 @@ impl CommandApi { #[rpc(all_positional, ts_outdir = "typescript/generated")] impl CommandApi { + /// Test function. + async fn sleep(&self, delay: f64) { + tokio::time::sleep(std::time::Duration::from_secs_f64(delay)).await + } + // --------------------------------------------- // Misc top level functions // --------------------------------------------- diff --git a/deltachat-rpc-client/tests/test_something.py b/deltachat-rpc-client/tests/test_something.py index 83a783f67..47b4cf8e3 100644 --- a/deltachat-rpc-client/tests/test_something.py +++ b/deltachat-rpc-client/tests/test_something.py @@ -1,6 +1,7 @@ from unittest.mock import MagicMock import pytest +import asyncio from deltachat_rpc_client import EventType, events from deltachat_rpc_client.rpc import JsonRpcError @@ -13,6 +14,17 @@ async def test_system_info(rpc) -> None: assert "deltachat_core_version" in system_info +@pytest.mark.asyncio() +async def test_sleep(rpc) -> None: + """Test that long-running task does not block short-running task from completion.""" + sleep_5_task = asyncio.create_task(rpc.sleep(5.0)) + sleep_3_task = asyncio.create_task(rpc.sleep(3.0)) + done, pending = await asyncio.wait([sleep_5_task, sleep_3_task], return_when=asyncio.FIRST_COMPLETED) + assert sleep_3_task in done + assert sleep_5_task in pending + sleep_5_task.cancel() + + @pytest.mark.asyncio() async def test_email_address_validity(rpc) -> None: valid_addresses = [ diff --git a/deltachat-rpc-server/src/bin/deltachat-rpc-server/main.rs b/deltachat-rpc-server/src/bin/deltachat-rpc-server/main.rs index 503fed392..78ee49c37 100644 --- a/deltachat-rpc-server/src/bin/deltachat-rpc-server/main.rs +++ b/deltachat-rpc-server/src/bin/deltachat-rpc-server/main.rs @@ -51,7 +51,10 @@ async fn main() -> Result<()> { let mut lines = BufReader::new(stdin).lines(); while let Some(message) = lines.next_line().await? { log::trace!("RPC recv {}", message); - session.handle_incoming(&message).await; + let session = session.clone(); + tokio::spawn(async move { + session.handle_incoming(&message).await; + }); } log::info!("EOF reached on stdin"); Ok(())