-
-
Notifications
You must be signed in to change notification settings - Fork 127
refactor: codebase quality audit — safety, tests, DRY, dep hygiene #404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
f7c39f4
f7f90f2
6d727bd
fe9bbc7
9edba90
02d309e
501a2a7
bd075ef
7d2cfdd
c3c126c
74b0bd1
f376414
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,11 +37,15 @@ impl Client { | |
| // During offline sync, permits=1 serialized all message processing. | ||
| // Replace with a new semaphore with 64 permits for concurrent processing. | ||
| // Old workers holding the previous semaphore Arc will finish normally. | ||
| *self | ||
| .message_processing_semaphore | ||
| .lock() | ||
| .expect("message_processing_semaphore poisoned") = | ||
| std::sync::Arc::new(async_lock::Semaphore::new(64)); | ||
| { | ||
| self.message_semaphore_generation | ||
| .fetch_add(1, std::sync::atomic::Ordering::SeqCst); | ||
| let mut guard = match self.message_processing_semaphore.lock() { | ||
| Ok(g) => g, | ||
| Err(poisoned) => poisoned.into_inner(), | ||
| }; | ||
| *guard = std::sync::Arc::new(async_lock::Semaphore::new(64)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep the new generation paired with the new 64-permit semaphore. This has the same atomicity bug as Proposed fix {
- self.message_semaphore_generation
- .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let mut guard = match self.message_processing_semaphore.lock() {
Ok(g) => g,
Err(poisoned) => poisoned.into_inner(),
};
*guard = std::sync::Arc::new(async_lock::Semaphore::new(64));
+ self.message_semaphore_generation
+ .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| self.offline_sync_notifier.notify(usize::MAX); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -493,11 +493,33 @@ impl Client { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Acquire global processing permit. During offline sync (1 permit), this serializes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ALL message processing globally, matching WA Web's allChatQueue pattern. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // After offline sync completes, permits are increased for parallel processing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let semaphore = self.message_processing_semaphore.lock().unwrap().clone(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Acquire global processing permit (1 during offline sync, N after). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Generation check rejects stale Arc clones from a previous connection. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let generation = self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .message_semaphore_generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .load(std::sync::atomic::Ordering::SeqCst); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let semaphore = match self.message_processing_semaphore.lock() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(guard) => guard.clone(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Err(poisoned) => poisoned.into_inner().clone(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| != self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .message_semaphore_generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .load(std::sync::atomic::Ordering::SeqCst) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log::debug!("Stale semaphore generation, skipping message batch"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let _global_permit = semaphore.acquire_arc().await; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Post-acquire recheck: generation could have changed during the .await | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| != self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .message_semaphore_generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .load(std::sync::atomic::Ordering::SeqCst) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log::debug!("Semaphore generation changed during acquire, dropping stale permit"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+496
to
+522
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Retry generation mismatches instead of dropping the batch.
🔁 Suggested fix- let generation = self
- .message_semaphore_generation
- .load(std::sync::atomic::Ordering::SeqCst);
- let semaphore = match self.message_processing_semaphore.lock() {
- Ok(guard) => guard.clone(),
- Err(poisoned) => poisoned.into_inner().clone(),
- };
- if generation
- != self
- .message_semaphore_generation
- .load(std::sync::atomic::Ordering::SeqCst)
- {
- log::debug!("Stale semaphore generation, skipping message batch");
- return;
- }
- let _global_permit = semaphore.acquire_arc().await;
- // Post-acquire recheck: generation could have changed during the .await
- if generation
- != self
- .message_semaphore_generation
- .load(std::sync::atomic::Ordering::SeqCst)
- {
- log::debug!("Semaphore generation changed during acquire, dropping stale permit");
- return;
- }
+ let _global_permit = loop {
+ let generation = self
+ .message_semaphore_generation
+ .load(std::sync::atomic::Ordering::SeqCst);
+ let semaphore = match self.message_processing_semaphore.lock() {
+ Ok(guard) => guard.clone(),
+ Err(poisoned) => poisoned.into_inner().clone(),
+ };
+
+ if generation
+ != self
+ .message_semaphore_generation
+ .load(std::sync::atomic::Ordering::SeqCst)
+ {
+ log::debug!("Semaphore generation changed while cloning, retrying");
+ continue;
+ }
+
+ let permit = semaphore.acquire_arc().await;
+
+ if generation
+ == self
+ .message_semaphore_generation
+ .load(std::sync::atomic::Ordering::SeqCst)
+ {
+ break permit;
+ }
+
+ log::debug!("Semaphore generation changed during acquire, retrying");
+ };📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log::debug!( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Starting PASS 1: Processing {} session establishment messages (pkmsg/msg)", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.