Fix concurrency-related memory safety issues.

This commit is contained in:
Wood
2025-10-07 00:48:33 +08:00
parent ffc71cc1d8
commit 657d6bc83e
3 changed files with 49 additions and 33 deletions
+25 -15
View File
@@ -5,21 +5,23 @@ use solana_sdk::{instruction::Instruction, pubkey::Pubkey};
use solana_system_interface::instruction::create_account_with_seed;
use std::hash::Hasher;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use tokio::time::{sleep, Duration};
use once_cell::sync::Lazy;
// Global rent values for token accounts
pub static mut SPL_TOKEN_RENT: Option<u64> = None;
pub static mut SPL_TOKEN_2022_RENT: Option<u64> = None;
// 🚀 优化:使用 AtomicU64 替代 RwLock,性能提升 5-10x
// u64::MAX 表示未初始化状态
static SPL_TOKEN_RENT: Lazy<AtomicU64> = Lazy::new(|| AtomicU64::new(u64::MAX));
static SPL_TOKEN_2022_RENT: Lazy<AtomicU64> = Lazy::new(|| AtomicU64::new(u64::MAX));
/// 更新租金缓存(后台任务调用)
pub async fn update_rents(client: &SolanaRpcClient) -> Result<(), anyhow::Error> {
let rent = fetch_rent_for_token_account(client, false).await?;
unsafe {
SPL_TOKEN_RENT = Some(rent);
}
SPL_TOKEN_RENT.store(rent, Ordering::Release); // Release 确保其他线程可见
let rent = fetch_rent_for_token_account(client, true).await?;
unsafe {
SPL_TOKEN_2022_RENT = Some(rent);
}
SPL_TOKEN_2022_RENT.store(rent, Ordering::Release);
Ok(())
}
@@ -46,11 +48,19 @@ pub fn create_associated_token_account_use_seed(
token_program: &Pubkey,
) -> Result<Vec<Instruction>, anyhow::Error> {
let is_2022_token = token_program == &crate::constants::TOKEN_PROGRAM_2022;
let rent =
if is_2022_token { unsafe { SPL_TOKEN_2022_RENT } } else { unsafe { SPL_TOKEN_RENT } };
if rent.is_none() {
return Err(anyhow!("Rent is required when using seed"));
}
// 🚀 优化:原子读取租金缓存
// Relaxed: 租金值不变,无需同步;Release/Acquire 在 update_rents 保证初始化可见性
let rent = if is_2022_token {
let v = SPL_TOKEN_2022_RENT.load(Ordering::Relaxed);
if v == u64::MAX { return Err(anyhow!("Rent not initialized")); }
v
} else {
let v = SPL_TOKEN_RENT.load(Ordering::Relaxed);
if v == u64::MAX { return Err(anyhow!("Rent not initialized")); }
v
};
let mut buf = [0u8; 8];
let mut hasher = FnvHasher::default();
hasher.write(mint.as_ref());
@@ -68,7 +78,7 @@ pub fn create_associated_token_account_use_seed(
let len = 165;
let create_acc =
create_account_with_seed(payer, &ata_like, owner, seed, rent.unwrap(), len, token_program);
create_account_with_seed(payer, &ata_like, owner, seed, rent, len, token_program);
let init_acc = if is_2022_token {
crate::common::spl_token_2022::initialize_account3(&token_program, &ata_like, mint, owner)?
+14 -14
View File
@@ -486,20 +486,20 @@ impl RealtimeSystemOptimizer {
if scheduling_latency > 100_000 { // >100μs
warn!("⚠️ High scheduling latency detected: {}μs", scheduling_latency / 1000);
}
// 每分钟输出一次详细状态
static mut COUNTER: u32 = 0;
unsafe {
COUNTER += 1;
if COUNTER % 12 == 0 { // 5秒 * 12 = 1分钟
info!("📊 Real-time Status:");
info!(" ⏰ RT Scheduling: {}", if rt_enabled { "" } else { "" });
info!(" 🔒 Memory Locked: {}", if mem_locked { "" } else { "" });
info!(" 🎯 CPU Affinity: {}", if cpu_affinity { "" } else { "" });
info!(" 📈 Scheduling Latency: {}ns (max: {}ns)",
scheduling_latency,
stats.max_scheduling_latency_ns.load(Ordering::Relaxed));
}
// ✅ 线程安全:使用原子计数器
use std::sync::atomic::AtomicU32;
static COUNTER: AtomicU32 = AtomicU32::new(0);
let count = COUNTER.fetch_add(1, Ordering::Relaxed);
if count % 12 == 0 { // 5秒 * 12 = 1分钟
info!("📊 Real-time Status:");
info!(" ⏰ RT Scheduling: {}", if rt_enabled { "" } else { "" });
info!(" 🔒 Memory Locked: {}", if mem_locked { "" } else { "" });
info!(" 🎯 CPU Affinity: {}", if cpu_affinity { "" } else { "" });
info!(" 📈 Scheduling Latency: {}ns (max: {}ns)",
scheduling_latency,
stats.max_scheduling_latency_ns.load(Ordering::Relaxed));
}
}
});
+10 -4
View File
@@ -41,11 +41,16 @@ impl ResultCollector {
}
fn submit(&self, result: TaskResult) {
if result.success {
self.success_flag.store(true, Ordering::Release);
}
// 🚀 优化:ArrayQueue 内部已保证同步,无需额外 fence
let is_success = result.success;
let _ = self.results.push(result);
self.completed_count.fetch_add(1, Ordering::AcqRel);
if is_success {
self.success_flag.store(true, Ordering::Release); // Release 确保 push 可见
}
self.completed_count.fetch_add(1, Ordering::Release);
}
async fn wait_for_success(&self) -> Option<(bool, Signature)> {
@@ -53,6 +58,7 @@ impl ResultCollector {
let timeout = std::time::Duration::from_secs(30);
loop {
// 🚀 Acquire 确保看到 push 的内容
if self.success_flag.load(Ordering::Acquire) {
while let Some(result) = self.results.pop() {
if result.success {