Skip to content

driver: serialize /solve requests in background task#4187

Open
MartinquaXD wants to merge 4 commits intomainfrom
serialize-driver-solver-request-in-blocking-task
Open

driver: serialize /solve requests in background task#4187
MartinquaXD wants to merge 4 commits intomainfrom
serialize-driver-solver-request-in-blocking-task

Conversation

@MartinquaXD
Copy link
Contributor

Description

Moves the serialization of the driver's /solve request to a blocking background task to not block any executor threads too long.

@MartinquaXD MartinquaXD requested a review from a team as a code owner February 19, 2026 21:44
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request moves the serialization of /solve requests to a background task using tokio::task::spawn_blocking to prevent blocking executor threads. While the overall approach is sound, the error handling in the solve method is problematic as it uses .expect() on the background task's result, which can lead to process-wide panics on serialization failures. This review focuses on improving the robustness of this error handling to align with the repository's technical standards for logic and reliability.

Comment on lines 327 to 336
let body = tokio::task::spawn_blocking(move || {
// pre-allocate a big enough buffer to avoid re-allocating memory
// as the request gets serialized
const BYTES_PER_ORDER: usize = 1_300;
let mut buffer = Vec::with_capacity(auction_dto.orders.len() * BYTES_PER_ORDER);
serde_json::to_writer(&mut buffer, &auction_dto).unwrap();
bytes::Bytes::from(buffer)
})
.await
.expect("errors bubble up");
Copy link
Contributor

Choose a reason for hiding this comment

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

How long does it really take if it needs to use a separate thread pool?

Copy link
Contributor

Choose a reason for hiding this comment

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

@jmg-duarte shared an interesting article https://ryhl.io/blog/async-what-is-blocking/, which explains why spawn_blocking might be suboptimal for non i/o jobs like CPU computations with no sleeps.

IIUC, for CPU-bound tasks, the best throughput is usually when the number of worker threads ~ number of CPU cores. While the blocking thread pool has up to 500 worker threads, which exceeds the number of CPU cores, this can lead to performance issues.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can merge this with the blocking thread pool right now and add a rayon one later on for these tasks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Serializing the request currently takes ~90ms.
Uploading Screenshot 2026-02-20 at 20.42.25.png…

Copy link
Contributor

@jmg-duarte jmg-duarte left a comment

Choose a reason for hiding this comment

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

Ilya raises a valid point with the expect/unwrap

Other than that, this looks good

jmg-duarte

This comment was marked as duplicate.

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