[GH-ISSUE #2331] [Issue]: Bulk ST import SSE route runs synchronous work via tokio::spawn instead of spawn_blocking, starving the server runtime #1159

Closed
opened 2026-10-21 05:28:07 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Romuromylus on GitHub (Jun 5, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2331

Originally assigned to: @Xelvanis on GitHub.

Summary

Severity: Low — Concurrency (server runtime). On the Axum server runtime, POST /api/import/st-bulk/run spawns the fully-synchronous bulk importer with plain tokio::spawn instead of spawn_blocking. The importer does fs reads, JSON/PNG decode, and storage writes for potentially hundreds of files with zero .await, so it blocks the tokio worker thread it lands on for the entire (multi-second to multi-minute) import, starving other async tasks. The sibling profile-import route uses spawn_blocking correctly, so this is an inconsistency.

Expected behavior

Synchronous import work should run inside tokio::task::spawn_blocking (as profile_import_upload_stream does), forwarding SSE events through the channel, so it does not block an async worker thread.

Actual behavior

src-tauri/src/http_server.rs:901-908 (import_st_bulk_run_stream):

    tokio::spawn(async move {
        let started = Instant::now();
        request_log("import_st_bulk_run_stream started");
        let result =
            imports::import_stream_callback(&state.app, &["st-bulk", "run"], body, |event| {
                tx.send(Ok(Event::default().data(event.to_string())))
                    .map_err(|error| AppError::new("sse_stream_error", error.to_string()))
            });

import_stream_callback (imports/service.rs:500) is a plain non-async fn → bulk_imports::run_st_bulk_import_*, which has zero .await/spawn_blocking and does fs::read/JSON/image decode + storage writes scaling with file count. marinara-server.rs uses multi-threaded #[tokio::main], so the blocked closure ties up one worker for the import's full duration; genuinely-async peers (e.g. llm_stream) can be starved. The sibling profile_import_upload_stream (http_server.rs:662) correctly wraps its importer in tokio::task::spawn_blocking.

Steps to reproduce

  1. Server runtime; admin client calls POST /api/import/st-bulk/run on a large SillyTavern data dir.
  2. Other clients issue concurrent /api requests during the import → degraded/stalled until it finishes.

Impact area

  • Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime

Environment

  • Branch or commit: refactor @ b0e5561d
  • Run mode: remote/Docker runtime (Axum server) — not the primary Tauri desktop path
  • OS + version: not relevant (static source audit)
  • Node + pnpm versions: not relevant
  • Rust version, if relevant: not relevant
  • Remote Runtime URL configured? yes (server-only)
  • Provider, model, or integration, if relevant: bulk ST import

Logs, screenshots, or video

Static source audit; cited to file:line above.

Regression notes

Found via static audit of the refactor branch; not verified against main. Admin-gated, server-only (opt-in), and on a multi-core box only 1 worker is blocked; transient availability degradation, no data loss, hence low.

Additional context

Suggested fix: run the synchronous body inside tokio::task::spawn_blocking, forwarding emitted events through the existing unbounded channel from within the blocking closure — mirroring profile_import_upload_stream at line 662. Keep the channel send / keep-alive on the async side.

Template check

Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2331 Originally assigned to: @Xelvanis on GitHub. ## Summary **Severity: Low — Concurrency (server runtime).** On the Axum server runtime, `POST /api/import/st-bulk/run` spawns the fully-synchronous bulk importer with plain `tokio::spawn` instead of `spawn_blocking`. The importer does fs reads, JSON/PNG decode, and storage writes for potentially hundreds of files with zero `.await`, so it blocks the tokio worker thread it lands on for the entire (multi-second to multi-minute) import, starving other async tasks. The sibling profile-import route uses `spawn_blocking` correctly, so this is an inconsistency. ## Expected behavior Synchronous import work should run inside `tokio::task::spawn_blocking` (as `profile_import_upload_stream` does), forwarding SSE events through the channel, so it does not block an async worker thread. ## Actual behavior `src-tauri/src/http_server.rs:901-908` (`import_st_bulk_run_stream`): ```rust tokio::spawn(async move { let started = Instant::now(); request_log("import_st_bulk_run_stream started"); let result = imports::import_stream_callback(&state.app, &["st-bulk", "run"], body, |event| { tx.send(Ok(Event::default().data(event.to_string()))) .map_err(|error| AppError::new("sse_stream_error", error.to_string())) }); ``` `import_stream_callback` (`imports/service.rs:500`) is a plain non-async `fn` → `bulk_imports::run_st_bulk_import_*`, which has zero `.await`/`spawn_blocking` and does fs::read/JSON/image decode + storage writes scaling with file count. `marinara-server.rs` uses multi-threaded `#[tokio::main]`, so the blocked closure ties up one worker for the import's full duration; genuinely-async peers (e.g. `llm_stream`) can be starved. The sibling `profile_import_upload_stream` (`http_server.rs:662`) correctly wraps its importer in `tokio::task::spawn_blocking`. ## Steps to reproduce 1. Server runtime; admin client calls `POST /api/import/st-bulk/run` on a large SillyTavern data dir. 2. Other clients issue concurrent `/api` requests during the import → degraded/stalled until it finishes. ## Impact area - [x] Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime ## Environment - Branch or commit: refactor @ b0e5561d - Run mode: remote/Docker runtime (Axum server) — not the primary Tauri desktop path - OS + version: not relevant (static source audit) - Node + pnpm versions: not relevant - Rust version, if relevant: not relevant - Remote Runtime URL configured? yes (server-only) - Provider, model, or integration, if relevant: bulk ST import ## Logs, screenshots, or video Static source audit; cited to file:line above. ## Regression notes Found via static audit of the refactor branch; not verified against main. Admin-gated, server-only (opt-in), and on a multi-core box only 1 worker is blocked; transient availability degradation, no data loss, hence low. ## Additional context Suggested fix: run the synchronous body inside `tokio::task::spawn_blocking`, forwarding emitted events through the existing unbounded channel from within the blocking closure — mirroring `profile_import_upload_stream` at line 662. Keep the channel send / keep-alive on the async side. ## Template check Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:28:07 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1159
No description provided.