Skip to content

Fix port collision crash#602

Merged
lucksus merged 8 commits intodevfrom
feature/fix-port-collision-crash
Apr 29, 2025
Merged

Fix port collision crash#602
lucksus merged 8 commits intodevfrom
feature/fix-port-collision-crash

Conversation

@lucksus
Copy link
Member

@lucksus lucksus commented Apr 28, 2025

Tests revealed crashes on some machines where our default ports (1337, 2000) are used by other services. The JS lib get-port in the remaining JS executor implementation doesn't work (anymore?) and instead throws when the preferred port is used. This does free port scanning in Rust.

Summary by CodeRabbit

  • New Features

    • Automatic assignment of available ports for server components if not already configured.
  • Bug Fixes

    • Fixed crashes caused by default ports being in use by other processes.
    • Improved error handling and logging when no free ports are found.
  • Chores

    • Updated dependencies to improve port selection reliability.
    • Streamlined port selection logic for better maintainability.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 28, 2025

Walkthrough

This change centralizes the logic for finding available network ports by moving the find_port function from the UI layer to the Rust executor layer. The function is now publicly exported from the executor's utilities module, and the UI imports it from there instead of maintaining its own copy. The executor's configuration logic is updated to assign default ports using this shared function when necessary. Dependency declarations are adjusted accordingly, with the portpicker crate added to the executor and removed from the UI. Additionally, error handling for port finding is improved with explicit logging and panics. The JavaScript initialization removes redundant port checks and adds an asynchronous call to keep the event loop alive.

Changes

File(s) Change Summary
rust-executor/Cargo.toml Added portpicker = "0.1.1" dependency.
rust-executor/src/lib.rs Made utils module public; imported find_port; added find_and_set_port helper; used it to assign default ports in run function.
rust-executor/src/utils.rs Added public find_port function to search for a free port in a range.
ui/src-tauri/Cargo.toml Removed portpicker = "0.1.1" dependency.
ui/src-tauri/src/lib.rs Replaced local find_port import with import from rust_executor::utils; added explicit error handling with logging and panic.
ui/src-tauri/src/util.rs Removed local find_port implementation entirely.
executor/src/main.ts Removed explicit port assignments and checks; added async call to getPort(50000) to keep JS event loop alive; cleaned unused variables.
rust-executor/src/js_core/mod.rs Fixed formatting by removing extra space before .await.

Sequence Diagram(s)

sequenceDiagram
    participant UI
    participant RustExecutor
    participant Utils

    UI->>RustExecutor: Start initialization with config
    RustExecutor->>Utils: find_and_set_port for gql_port (4000-40000)
    RustExecutor->>Utils: find_and_set_port for hc_admin_port (2000-40000)
    RustExecutor->>Utils: find_and_set_port for hc_app_port (1337-40000)
    Utils-->>RustExecutor: Return available ports or error
    RustExecutor-->>UI: Continue startup with assigned ports
Loading

Poem

🐇 Hopping through ports, a quest anew,
From UI to Rust, the code we flew.
One function to find where free spots hide,
No more duplicates, no need to divide.
Ports assigned with care and grace,
Keeping loops alive, no stalled race!
🌿✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e9d380f and a135d8b.

📒 Files selected for processing (1)
  • executor/src/main.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • executor/src/main.ts
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

Copy link
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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1eda330 and 6ac6dec.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (6)
  • rust-executor/Cargo.toml (1 hunks)
  • rust-executor/src/lib.rs (3 hunks)
  • rust-executor/src/utils.rs (1 hunks)
  • ui/src-tauri/Cargo.toml (0 hunks)
  • ui/src-tauri/src/lib.rs (1 hunks)
  • ui/src-tauri/src/util.rs (0 hunks)
💤 Files with no reviewable changes (2)
  • ui/src-tauri/Cargo.toml
  • ui/src-tauri/src/util.rs
🧰 Additional context used
🧬 Code Graph Analysis (1)
ui/src-tauri/src/lib.rs (1)
rust-executor/src/utils.rs (1)
  • find_port (9-20)
🔇 Additional comments (7)
rust-executor/Cargo.toml (1)

119-119: Successfully added portpicker dependency.

The portpicker crate has been correctly added to support the port-scanning functionality implemented in the utils module.

ui/src-tauri/src/lib.rs (2)

14-14: Import correctly updated to use centralized implementation.

The find_port import has been correctly updated to use the implementation from rust_executor::utils instead of the local implementation, supporting the PR's goal of centralizing this functionality.


190-190: Function call adjusted to use imported implementation.

Line 190 correctly uses the imported find_port function with a reasonable port range (12000-13000), which should avoid conflicts with common system services.

rust-executor/src/utils.rs (1)

3-3: Import correctly added for the portpicker crate.

The portpicker import has been correctly added to support the new port scanning functionality.

rust-executor/src/lib.rs (3)

12-12: Module correctly made public.

Making the utils module public allows external crates like the UI to import and use the find_port function, supporting the centralization objective.


37-37: Import added for the find_port function.

The function is correctly imported into the main crate scope to be used in the run function.


128-139: Default port assignment using find_port.

These conditional blocks correctly implement the PR objective by ensuring all required ports are assigned values by scanning for available ports, addressing the port collision crash issue.

However, if the find_port function is updated to return a Result as suggested, this code would need to be updated to handle potential errors rather than propagating panics.

When updating this code to handle a Result return type from find_port, consider a pattern like:

if config.gql_port.is_none() {
    config.gql_port = Some(find_port(4000, 40000).unwrap_or_else(|err| {
        error!("Failed to find free port for GraphQL: {}", err);
        // Provide a fallback or exit gracefully
        std::process::exit(1);
    }));
}

Copy link
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: 0

🧹 Nitpick comments (3)
executor/src/main.ts (3)

8-8: Consider updating or removing the 'get-port' dependency.

The PR description states that the get-port library "no longer functions correctly and throws errors when the preferred port is already in use." While you're still using it here for the event loop hack, it might be better to:

  1. Replace it with the new Rust-based port finding solution, or
  2. Add a comment explaining why this dependency is still needed despite its issues.

This would avoid potential confusion for future developers.


89-100: The workaround for the JS event loop should be temporary.

The current implementation is a good short-term solution to keep the JS event loop active. However:

  1. The comment clearly explains the workaround nature of this code, which is excellent.
  2. The try/catch block properly prevents crashes if port 50000 is already in use.

Consider adding a TODO comment with a ticket number to track when this workaround can be removed during the mentioned upcoming refactoring of the remaining JS code to Rust.

// So this here is a hack that works for now.
+// TODO: Remove this hack when the remaining JS code is moved to Rust (issue #XXX)
// Putting it in a try/catch block to avoid the process from crashing if the port is already in use.

102-102: Remove commented-out delay code.

The commented-out delay line should be removed as it's no longer needed and might confuse future developers.

-    //await new Promise(resolve => setTimeout(resolve, 1000));
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5f0b240 and e9d380f.

📒 Files selected for processing (6)
  • CHANGELOG (1 hunks)
  • executor/src/main.ts (1 hunks)
  • rust-executor/src/js_core/mod.rs (1 hunks)
  • rust-executor/src/lib.rs (4 hunks)
  • rust-executor/src/utils.rs (1 hunks)
  • ui/src-tauri/src/lib.rs (2 hunks)
✅ Files skipped from review due to trivial changes (2)
  • rust-executor/src/js_core/mod.rs
  • CHANGELOG
🚧 Files skipped from review as they are similar to previous changes (3)
  • ui/src-tauri/src/lib.rs
  • rust-executor/src/utils.rs
  • rust-executor/src/lib.rs
🔇 Additional comments (1)
executor/src/main.ts (1)

86-87: LGTM: Correctly removed unused parameters.

Good cleanup - removing the unused destructured variables (mocks, gqlPort, runDappServer, dAppPort) from the function parameters since port management has been moved to the Rust side.

Co-authored-by: James <34165044+jhweir@users.noreply.github.com>
@lucksus lucksus merged commit 00d6bf7 into dev Apr 29, 2025
3 checks passed
@lucksus lucksus deleted the feature/fix-port-collision-crash branch August 22, 2025 12:01
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.

2 participants