Skip to content

Refactor worker task#239

Merged
leynos merged 3 commits intomainfrom
codex/refactor-worker_task-for-clarity
Aug 1, 2025
Merged

Refactor worker task#239
leynos merged 3 commits intomainfrom
codex/refactor-worker_task-for-clarity

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Aug 1, 2025

Summary

  • separate panic handling into spawn_connection_task
  • extract accept logic into accept_loop
  • keep worker_task focused on coordination
  • test new helper function

Testing

  • make lint
  • make test

https://chatgpt.com/codex/tasks/task_e_688bf5ca37788322a5e98a3a4d0cab5c

Summary by Sourcery

Refactor worker_task by splitting off connection spawning and panic handling into spawn_connection_task, moving the accept loop into accept_loop, and updating tests to cover shutdown behavior and panic logging

Bug Fixes:

  • Catch and log panics in connection tasks to prevent worker crashes

Enhancements:

  • Move connection spawning and panic handling into spawn_connection_task
  • Extract accept logic into accept_loop and simplify worker_task to delegate to it

Tests:

  • Rename shutdown signal test to use accept_loop
  • Add test verifying spawn_connection_task logs panics without tearing down the worker

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 1, 2025

Summary by CodeRabbit

  • Refactor

    • Improved the handling of incoming TCP connections by modularising connection acceptance, per-connection task management, and shutdown logic for better reliability and maintainability.
  • Tests

    • Renamed a test to better reflect its scope.
    • Added a new test to ensure that panics in connection handlers are correctly logged without affecting overall server stability.

Walkthrough

Refactor the TCP server's worker logic by dividing the monolithic connection acceptance and handling loop into three modular functions: one for accepting connections (accept_loop), one for spawning per-connection tasks with panic handling (spawn_connection_task), and a simplified worker entrypoint (worker_task). Update and add tests to match the new structure and verify panic logging.

Changes

Cohort / File(s) Change Summary
Server Task Refactor
src/server.rs
Split the worker logic into spawn_connection_task, accept_loop, and a simplified worker_task. Centralise panic catching and task spawning. Update function signatures and control flow.
Test Updates
src/server.rs
Rename test_worker_task_shutdown_signal to test_accept_loop_shutdown_signal. Add spawn_connection_task_logs_panic test to ensure panics are logged and do not crash the worker.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant TcpListener
    participant Server (accept_loop)
    participant Task (spawn_connection_task)
    participant App

    Client->>TcpListener: Connect
    loop accept_loop
        TcpListener->>Server (accept_loop): Accept connection
        Server (accept_loop)->>Task (spawn_connection_task): Spawn per-connection task
        Task (spawn_connection_task)->>App: Process connection (with panic handling)
        alt Panic occurs
            Task (spawn_connection_task)->>Task (spawn_connection_task): Log panic with peer address
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

Refactor the worker, split up the task,
Accept and spawn, with panics unmasked.
Each connection now gets its own fate,
With logs for the panics that come far too late.
Modular flows and tests on parade—
The server stands strong, no longer afraid!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f4299e9 and 26a33aa.

📒 Files selected for processing (1)
  • src/server.rs (5 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit Inference Engine (AGENTS.md)

**/*.rs: Clippy warnings MUST be disallowed.
Fix any warnings emitted during tests in the code itself rather than silencing them.
Where a function is too long, extract meaningfully named helper functions adhering to separation of concerns and CQRS.
Where a function has too many parameters, group related parameters in meaningfully named structs.
Where a function is returning a large error consider using Arc to reduce the amount of data returned.
Every module must begin with a module level (//!) comment explaining the module's purpose and utility.
Document public APIs using Rustdoc comments (///) so documentation can be generated with cargo doc.
Prefer immutable data and avoid unnecessary mut bindings.
Handle errors with the Result type instead of panicking where feasible.
Avoid unsafe code unless absolutely necessary and document any usage clearly.
Place function attributes after doc comments.
Do not use return in single-line functions.
Use predicate functions for conditional criteria with more than two branches.
Lints must not be silenced except as a last resort.
Lint rule suppressions must be tightly scoped and include a clear reason.
Prefer expect over allow.
Prefer .expect() over .unwrap().
Use concat!() to combine long string literals rather than escaping newlines with a backslash.
Prefer semantic error enums: Derive std::error::Error (via the thiserror crate) for any condition the caller might inspect, retry, or map to an HTTP status.
Use an opaque error only at the app boundary: Use eyre::Report for human-readable logs; these should not be exposed in public APIs.
Never export the opaque type from a library: Convert to domain enums at API boundaries, and to eyre only in the main main() entrypoint or top-level async task.

Files:

  • src/server.rs

⚙️ CodeRabbit Configuration File

**/*.rs: * Seek to keep the cyclomatic complexity of functions no more than 12.

  • Adhere to single responsibility and CQRS

  • Place function attributes after doc comments.

  • Do not use return in single-line functions.

  • Move conditionals with >2 branches into a predicate function.

  • Avoid unsafe unless absolutely necessary.

  • Every module must begin with a //! doc comment that explains the module's purpose and utility.

  • Comments and docs must follow en-GB-oxendict (-ize / -our) spelling and grammar

  • Lints must not be silenced except as a last resort.

    • #[allow] is forbidden.
    • Only narrowly scoped #[expect(lint, reason = "...")] is allowed.
    • No lint groups, no blanket or file-wide suppression.
    • Include FIXME: with link if a fix is expected.
  • Use rstest fixtures for shared setup and to avoid repetition between tests.

  • Replace duplicated tests with #[rstest(...)] parameterised cases.

  • Prefer mockall for mocks/stubs.

  • Prefer .expect() over .unwrap()

  • Ensure that any API or behavioural changes are reflected in the documentation in docs/

  • Ensure that any completed roadmap steps are recorded in the appropriate roadmap in docs/

  • Files must not exceed 400 lines in length

    • Large modules must be decomposed
    • Long match statements or dispatch tables should be decomposed by domain and collocated with targets
    • Large blocks of inline data (e.g., test fixtures, constants or templates) must be moved to external files and inlined at compile-time or loaded at run-time.

Files:

  • src/server.rs
⏰ Context from checks skipped due to timeout of 240000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build-test
🔇 Additional comments (4)
src/server.rs (4)

375-409: Well-structured panic isolation for connection tasks.

The function correctly extracts peer addresses with proper error logging and catches panics to prevent worker crashes. The panic message extraction handles both string and non-string panic types appropriately.


452-469: Clean delegation pattern implemented correctly.

The function now properly delegates to accept_loop while maintaining backward compatibility. The documentation accurately reflects this architectural change.


896-917: Test correctly updated to match refactored architecture.

The test rename and direct call to accept_loop properly reflect the new code structure while maintaining test coverage for shutdown behavior.


961-1008: Comprehensive test for panic logging behavior.

The test effectively validates that connection task panics are caught and logged with the expected panic message and peer address information.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/refactor-worker_task-for-clarity

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Aug 1, 2025

Reviewer's Guide

This PR refactors the worker task by extracting connection spawning and panic handling into a dedicated helper, moving the accept loop logic into its own async function, and slimming down the main worker_task to a simple delegator. It also adds targeted tests for the new helper and updated shutdown behavior.

Sequence diagram for connection acceptance and handling after refactor

sequenceDiagram
    participant Worker as worker_task
    participant Acceptor as accept_loop
    participant Listener as TcpListener
    participant Spawner as spawn_connection_task
    participant Handler as process_stream
    Worker->>Acceptor: accept_loop(...)
    loop Until shutdown
        Acceptor->>Listener: accept()
        alt Connection accepted
            Acceptor->>Spawner: spawn_connection_task(stream, ...)
            Spawner->>Handler: process_stream(stream, ...)
            Handler-->>Spawner: (may panic)
            Spawner-->>Acceptor: (logs panic if any)
        else Accept error
            Acceptor->>Acceptor: retry with backoff
        end
    end
Loading

Class diagram for refactored worker task and helpers

classDiagram
    class worker_task {
        +async fn worker_task(listener, factory, on_success, on_failure, shutdown, tracker)
    }
    class accept_loop {
        +async fn accept_loop(listener, factory, on_success, on_failure, shutdown, tracker)
    }
    class spawn_connection_task {
        +fn spawn_connection_task(stream, factory, on_success, on_failure, tracker)
    }
    class process_stream {
        +async fn process_stream(stream, factory, on_success, on_failure)
    }
    worker_task --> accept_loop : delegates to
    accept_loop --> spawn_connection_task : spawns per connection
    spawn_connection_task --> process_stream : calls
Loading

Class diagram for test additions for spawn_connection_task

classDiagram
    class tests {
        +async fn spawn_connection_task_logs_panic(factory)
        +async fn test_accept_loop_shutdown_signal(factory)
    }
    tests ..> spawn_connection_task : tests
    tests ..> accept_loop : tests
Loading

File-Level Changes

Change Details Files
Extract connection spawn and panic handling into spawn_connection_task
  • Introduce spawn_connection_task to wrap process_stream with panic catching
  • Move peer address capture and panic logging into the new helper
  • Use tracker.spawn to run the safe-guarded task
src/server.rs
Move accept loop into accept_loop function
  • Extract the loop that accepts incoming connections with exponential backoff
  • Replace inline accept logic in worker_task with a delegation call
src/server.rs
Simplify worker_task to delegate to accept_loop
  • Remove inline accept-and-spawn logic
  • Change worker_task to a thin wrapper calling accept_loop
src/server.rs
Update and add tests for the new helpers and refactoring
  • Rename test_worker_task_shutdown_signal to test_accept_loop_shutdown_signal
  • Adjust test to call accept_loop directly
  • Add spawn_connection_task_logs_panic test to verify panic is logged but does not crash
src/server.rs

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey @leynos - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/server.rs:387` </location>
<code_context>
+    F: Fn() -> WireframeApp + Send + Sync + Clone + 'static,
+    T: Preamble,
+{
+    let peer_addr = stream.peer_addr().ok();
+    tracker.spawn(async move {
+        use futures::FutureExt as _;
</code_context>

<issue_to_address>
Handle peer_addr extraction errors more explicitly.

Logging a warning or error when peer_addr is None would improve debuggability.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
    let peer_addr = stream.peer_addr().ok();
    tracker.spawn(async move {
        use futures::FutureExt as _;
=======
    let peer_addr = match stream.peer_addr() {
        Ok(addr) => Some(addr),
        Err(e) => {
            tracing::warn!("Failed to extract peer_addr from TcpStream: {}", e);
            None
        }
    };
    tracker.spawn(async move {
        use futures::FutureExt as _;
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/server.rs Outdated
@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Aug 1, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 1, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Aug 1, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 1, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Aug 1, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 1, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Repository owner deleted a comment from sourcery-ai Bot Aug 1, 2025
@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Aug 1, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 1, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Aug 1, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 1, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🔭 Outside diff range comments (2)
src/server.rs (2)

443-443: Replace eprintln! with tracing::error! for consistency.

Use the tracing framework for error logging to maintain consistency with the rest of the codebase.

-                    eprintln!("accept error: {e}");
+                    tracing::error!(error = %e, "Failed to accept connection");

512-512: Replace eprintln! with tracing::error! for consistency.

Use the tracing framework for error logging to maintain consistency.

-                eprintln!("preamble callback error: {e}");
+                tracing::error!(error = %e, "Preamble callback failed");
📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2b00ea7 and f4299e9.

📒 Files selected for processing (1)
  • src/server.rs (5 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

⚙️ CodeRabbit Configuration File

**/*.rs: * Seek to keep the cyclomatic complexity of functions no more than 12.

  • Adhere to single responsibility and CQRS

  • Place function attributes after doc comments.

  • Do not use return in single-line functions.

  • Move conditionals with >2 branches into a predicate function.

  • Avoid unsafe unless absolutely necessary.

  • Every module must begin with a //! doc comment that explains the module's purpose and utility.

  • Comments and docs must follow en-GB-oxendict (-ize / -our) spelling and grammar

  • Lints must not be silenced except as a last resort.

    • #[allow] is forbidden.
    • Only narrowly scoped #[expect(lint, reason = "...")] is allowed.
    • No lint groups, no blanket or file-wide suppression.
    • Include FIXME: with link if a fix is expected.
  • Use rstest fixtures for shared setup and to avoid repetition between tests.

  • Replace duplicated tests with #[rstest(...)] parameterised cases.

  • Prefer mockall for mocks/stubs.

  • Prefer .expect() over .unwrap()

  • Ensure that any API or behavioural changes are reflected in the documentation in docs/

  • Ensure that any completed roadmap steps are recorded in the appropriate roadmap in docs/

  • Files must not exceed 400 lines in length

    • Large modules must be decomposed
    • Long match statements or dispatch tables should be decomposed by domain and collocated with targets
    • Large blocks of inline data (e.g., test fixtures, constants or templates) must be moved to external files and inlined at compile-time or loaded at run-time.

Files:

  • src/server.rs
🔇 Additional comments (2)
src/server.rs (2)

375-409: LGTM! Well-structured panic isolation.

The function properly isolates connection panics and provides comprehensive logging with peer address context. The panic message extraction handles both &str and String panic payloads correctly.


897-918: LGTM! Comprehensive test coverage for the refactored functions.

The test rename accurately reflects the refactored function, and the new panic logging test thoroughly verifies that connection panics are caught and logged with peer address context without crashing the worker.

Also applies to: 962-1009

Comment thread src/server.rs Outdated
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@leynos leynos merged commit 5e850f5 into main Aug 1, 2025
5 checks passed
@leynos leynos deleted the codex/refactor-worker_task-for-clarity branch August 1, 2025 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant