Skip to content

Remove starknet mutex when fetching config#871

Merged
3alpha merged 2 commits intomainfrom
optimizing-mutex-locks
Oct 13, 2025
Merged

Remove starknet mutex when fetching config#871
3alpha merged 2 commits intomainfrom
optimizing-mutex-locks

Conversation

@3alpha
Copy link
Copy Markdown
Collaborator

@3alpha 3alpha commented Oct 8, 2025

Usage related changes

Development related changes

Closes #869.

Checklist:

  • Checked out the contribution guidelines
  • Applied formatting - ./scripts/format.sh
  • No linter errors - ./scripts/clippy_check.sh
  • No unused dependencies - ./scripts/check_unused_deps.sh
  • No spelling errors - ./scripts/check_spelling.sh
  • Performed code self-review
  • Rebased to the latest commit of the target branch (or merged it into my branch)
    • Once you make the PR reviewable, please avoid force-pushing
  • Updated the docs if needed - ./website/README.md
  • Linked the issues resolvable by this PR - linking info
  • Updated the tests if needed; all passing - execution info

Summary by CodeRabbit

  • Refactor

    • Centralized configuration access across API, JSON-RPC handler, and endpoints for consistent behavior.
    • Simplified dump/load path handling and removed unnecessary locking during configuration reads.
    • Streamlined constructors and internal wiring without changing external behavior.
  • Performance

    • Minor responsiveness improvements in RPC and dump/load operations due to reduced locking.
  • Chores

    • Minor code cleanups and path simplifications for improved maintainability.
  • Reliability

    • More robust shutdown and configuration-driven behavior via unified config access.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Oct 8, 2025

Walkthrough

The PR centralizes configuration access by adding Arc-backed config and server_config fields to Api and refactoring server components to read configs through Api instead of holding their own copies or locking Starknet. JsonRpcHandler drops its public config fields and updates its constructor to accept only Api. Call sites in main.rs and various API endpoint modules are updated accordingly. A method in StarknetState is made non-mutating by changing &mut self to &self. A minor path simplification replaces std::sync::Arc with Arc in a calldata construction.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The pull request description includes the required section headings and a fully checked checklist, but the Usage related changes section is left empty with only the template placeholder, and the Development related changes section only contains a “Closes #869” reference without any explanation of how the changes impact developers. Please add substantive content under the Usage related changes section describing how users will be affected by this update, and expand the Development related changes section to explain how the modifications impact development workflows, tools, testing, or CI/CD processes.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly captures the core change of eliminating the Starknet mutex when retrieving configuration without including extraneous details, making it easy to understand at a glance that the PR focuses on simplifying config access.
Docstring Coverage ✅ Passed Docstring coverage is 81.25% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch optimizing-mutex-locks

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@3alpha
Copy link
Copy Markdown
Collaborator Author

3alpha commented Oct 8, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Oct 8, 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

@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)
crates/starknet-devnet-server/src/api/write_endpoints.rs (1)

126-135: Avoid holding dumpable_events lock during file I/O

Clone events, drop the lock, then write. Prevents blocking other RPCs while dumping.

Apply:

-        let dumpable_events = self.api.dumpable_events.lock().await;
+        let dumpable_events = { self.api.dumpable_events.lock().await.clone() };

         if !path.is_empty() {
-            dump_events(&dumpable_events, &path)
+            dump_events(&dumpable_events, &path)
                 .map_err(|err| ApiError::DumpError { msg: err.to_string() })?;
             return Ok(DevnetResponse::DevnetDump(None).into());
         }

-        Ok(DevnetResponse::DevnetDump(Some(dumpable_events.clone())).into())
+        Ok(DevnetResponse::DevnetDump(Some(dumpable_events)).into())
crates/starknet-devnet/src/main.rs (2)

294-296: Shadowing mutability is unnecessary

let starknet_config = starknet_config; is a no-op aside from removing mutability. Safe to keep; can be dropped for brevity.


404-407: Don’t hold dumpable_events lock during file I/O; signal handlers separation intact

Clone events, drop the lock, then dump. Keeps shutdown responsive. Separation from the block-interval SIGINT handler remains as intended. Based on learnings

-    if let (Some(DumpOn::Exit), Some(dump_path)) = (api.config.dump_on, &api.config.dump_path) {
-        let events = api.dumpable_events.lock().await;
-        dump_events(&events, dump_path).expect("Failed to dump.");
+    if let (Some(DumpOn::Exit), Some(dump_path)) = (api.config.dump_on, &api.config.dump_path) {
+        let events = { api.dumpable_events.lock().await.clone() };
+        dump_events(&events, dump_path).expect("Failed to dump.");
     }
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b1f0252 and 3bbf3c8.

📒 Files selected for processing (8)
  • crates/starknet-devnet-core/src/starknet/mod.rs (1 hunks)
  • crates/starknet-devnet-core/src/state/mod.rs (1 hunks)
  • crates/starknet-devnet-server/src/api/endpoints.rs (1 hunks)
  • crates/starknet-devnet-server/src/api/endpoints_ws.rs (1 hunks)
  • crates/starknet-devnet-server/src/api/json_rpc_handler.rs (6 hunks)
  • crates/starknet-devnet-server/src/api/mod.rs (2 hunks)
  • crates/starknet-devnet-server/src/api/write_endpoints.rs (3 hunks)
  • crates/starknet-devnet/src/main.rs (4 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-09-18T12:57:27.220Z
Learnt from: FabijanC
PR: 0xSpaceShard/starknet-devnet#849
File: crates/starknet-devnet/src/main.rs:345-346
Timestamp: 2025-09-18T12:57:27.220Z
Learning: In crates/starknet-devnet/src/main.rs, there are intentionally two separate signal handlers: one in `create_block_interval()` for terminating the block generation subprocess, and one in `shutdown_signal()` for handling dump-on-exit functionality. These serve distinct purposes and should not be consolidated.

Applied to files:

  • crates/starknet-devnet/src/main.rs
🔇 Additional comments (15)
crates/starknet-devnet-core/src/starknet/mod.rs (1)

721-721: LGTM!

Good simplification using the existing Arc import instead of the fully qualified path. This improves code consistency with other Arc usages in the file.

crates/starknet-devnet-core/src/state/mod.rs (1)

196-201: LGTM! Good refactor to remove unnecessary mutability.

Changing from &mut self to &self is correct since the method only reads state via is_contract_deployed. This enables callers to use shared references instead of exclusive locks, which aligns with the PR's goal of reducing mutex contention.

crates/starknet-devnet-server/src/api/endpoints.rs (1)

703-704: LGTM! This is the key optimization.

Replacing self.api.starknet.lock().await.config with (*self.api.config) eliminates mutex contention for config reads. This directly supports the PR objective of reducing mutex locks when fetching configuration.

crates/starknet-devnet-server/src/api/endpoints_ws.rs (1)

147-149: LGTM! Consistent with the config access optimization.

Accessing fork_config directly via self.api.config instead of through a locked Starknet instance follows the same optimization pattern as in endpoints.rs, reducing mutex contention.

crates/starknet-devnet-server/src/api/mod.rs (1)

34-50: Approve config centralization in Api.

All Api::new call sites include the server_config parameter.

crates/starknet-devnet-server/src/api/write_endpoints.rs (2)

114-125: Good: config read without locking Starknet

Accessing dump_on/dump_path via self.api.config removes unnecessary Starknet lock. Matches PR goal.


139-145: LGTM: load uses Api.config

Using self.api.config.dump_on aligns with centralizing config and avoids Starknet lock.

crates/starknet-devnet-server/src/api/json_rpc_handler.rs (6)

7-7: Import OK

DumpOn import matches new usage.


27-27: Import OK

rpc_core import supports Id::Null usage.


71-76: Correct: use config.uses_pre_confirmed_block()

Pre-confirmed block gating now reads from Api.config; no Starknet lock needed.


151-161: Constructor simplified to Api-only

JsonRpcHandler::new(api) and origin_caller init via api.config look good; fewer moving parts.


537-544: Method restriction via Api.server_config

Switch to self.api.server_config.restricted_methods is consistent with centralization.


581-601: Dump logic centralized and correct

  • Block mode writes immediately to dump_path; Request/Exit modes buffer to dumpable_events.
  • DumpOn derives Copy, so matching by value is safe.
crates/starknet-devnet/src/main.rs (2)

317-319: LGTM: Api/JsonRpcHandler wiring

Api::new(starknet, server_config) and JsonRpcHandler::new(api.clone()) align with new API surface.


333-333: serve_http_json_rpc signature verified
serve_http_json_rpc takes &ServerConfig; passing &api.server_config avoids unnecessary clones.

@3alpha 3alpha merged commit adc2cb7 into main Oct 13, 2025
2 checks passed
@3alpha 3alpha deleted the optimizing-mutex-locks branch October 13, 2025 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract StarknetConfig from Starknet struct

1 participant