Skip to content

Conversation

@nam2ee
Copy link
Contributor

@nam2ee nam2ee commented Oct 3, 2025

Problem

The SDK uses std::sync::Mutex for global configuration:

pub static WHIRLPOOLS_CONFIG_ADDRESS: Mutex<Pubkey> = Mutex::new(...);

When methods fetch_whirlpools_by_token_pair and fetch_concentrated_liquidity_pool are called inside tokio::spawn, it fails because MutexGuard doesn't implement Send, preventing the async task from being sent between threads.

Error

error[E0277]: `std::sync::MutexGuard<'_, Pubkey>` cannot be sent between threads safely

Reproduction - u can check at https://github.com/nam2ee/custom-whirlpool

Testing the Broken Version (Official SDK)

  1. Edit src/main.rs:

    // Comment out line 3
    // use fixed_orca_whirlpools::{...};
    
    // Uncomment line 4
    use original_orca_whirlpools::{...};
  2. Run:

    cargo check
  3. You'll see the Send trait error.

Testing the Working Version (Reference overalive Fixed)

  1. Edit src/main.rs:

    // Use line 3 (default)
    use fixed_orca_whirlpools::{...};
    
    // Comment line 4
    // use original_orca_whirlpools::{...};
  2. Run:

    cargo run
  3. It compiles and runs successfully. Also tests are all passed at modified ver.

The Fix

In rust-sdk/whirlpool/src/, change:
image

What was the issue?

Mutexguard for WHIRLPOOLS_CONFIG_ADDRESS is alive over after the .await contexts. It doesn't implements Send so that violates thread safety.

Related Issue

See the discussion in the Orca Discord for more context.

NEXT STEP

Basically, I notice operations for WHIRLPOOLS_CONFIG_ADDRESS are read-intensive overall.
So, I think changing Mutex type to RwLock will be helpful for tackling performance issue. Let me measure the performance metric!

Copilot AI review requested due to automatic review settings October 3, 2025 13:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a concurrency issue where MutexGuard from global configuration access was preventing async functions from being used in tokio::spawn due to Send trait violations.

  • Resolved MutexGuard lifetime issues by copying the Pubkey value instead of holding a reference
  • Updated all usages to work with the owned value rather than dereferenced guard references
  • Enables the SDK functions to be used safely in multi-threaded async contexts

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Contributor

@calintje calintje left a comment

Choose a reason for hiding this comment

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

Thank you for this! Nice PR and example.

Copy link
Collaborator

@yugure-orca yugure-orca left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

@yugure-orca
Copy link
Collaborator

In general, a Mutex performs quite fast — and pool creation happens so infrequently compared to things like swap.
So I’m not sure it’s worth switching to an RwLock just for performance. (But if someone shows an impressive benchmark, I might change my mind soon)

@yugure-orca yugure-orca merged commit 1d75ad5 into orca-so:main Oct 7, 2025
7 checks passed
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