Skip to content

Fix termination bug #57

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

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tracing-subscriber = "0.3"

[package]
name = "qorb"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
description = "Connection Pooling"
license = "MPL-2.0"
Expand Down
110 changes: 109 additions & 1 deletion src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ mod test {
use async_trait::async_trait;
use std::collections::BTreeMap;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};

#[derive(Clone)]
struct TestResolver {
Expand Down Expand Up @@ -802,4 +802,112 @@ mod test {
Error::Terminated,
));
}

struct SlowConnector {
delay_ms: AtomicU64,
}

impl SlowConnector {
fn new() -> Self {
Self {
delay_ms: AtomicU64::new(1),
}
}

async fn go_slow(&self) {
let delay_ms = self.delay_ms.load(Ordering::SeqCst);
tokio::time::sleep(tokio::time::Duration::from_millis(delay_ms)).await;
}
}

#[async_trait]
impl Connector for SlowConnector {
type Connection = ();

async fn connect(&self, _backend: &Backend) -> Result<Self::Connection, backend::Error> {
self.go_slow().await;
Ok(())
}

async fn is_valid(&self, _: &mut Self::Connection) -> Result<(), backend::Error> {
self.go_slow().await;
Ok(())
}

async fn on_acquire(&self, _: &mut Self::Connection) -> Result<(), backend::Error> {
self.go_slow().await;
Ok(())
}
}

fn setup_tracing_subscriber() {
use tracing_subscriber::fmt::format::FmtSpan;
tracing_subscriber::fmt()
.with_thread_names(true)
.with_span_events(FmtSpan::ENTER)
.with_max_level(tracing::Level::TRACE)
.with_test_writer()
.init();
}

#[tokio::test]
async fn test_terminate_with_slow_active_claim() {
setup_tracing_subscriber();

let resolver = Box::new(TestResolver::new());
let connector = Arc::new(SlowConnector::new());
let address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

resolver.replace(BTreeMap::from([(
backend::Name::new("aaa"),
Backend::new(address),
)]));

let pool = Pool::new(resolver, connector.clone(), Policy::default());
let _handle = pool.claim().await.expect("Failed to get claim");

// This delay is enormous, but the point is that termination should not
// get stuck behind any ongoing operations that might happen.
connector.delay_ms.store(99999999, Ordering::SeqCst);

pool.terminate().await.unwrap();
assert!(matches!(
pool.terminate().await.unwrap_err(),
Error::Terminated,
));
assert!(matches!(
pool.claim().await.map(|_| ()).unwrap_err(),
Error::Terminated,
));
}

#[tokio::test]
async fn test_terminate_with_slow_setup() {
setup_tracing_subscriber();

let resolver = Box::new(TestResolver::new());
let connector = Arc::new(SlowConnector::new());
let address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

resolver.replace(BTreeMap::from([(
backend::Name::new("aaa"),
Backend::new(address),
)]));

// This delay is enormous, but the point is that termination should not
// get stuck behind any ongoing operations that might happen.
connector.delay_ms.store(99999999, Ordering::SeqCst);

let pool = Pool::new(resolver, connector.clone(), Policy::default());

pool.terminate().await.unwrap();
assert!(matches!(
pool.terminate().await.unwrap_err(),
Error::Terminated,
));
assert!(matches!(
pool.claim().await.map(|_| ()).unwrap_err(),
Error::Terminated,
));
}
}
15 changes: 13 additions & 2 deletions src/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ impl<Conn: Connection> SetWorker<Conn> {
match work {
Work::DoConnect => {
let span = span!(Level::TRACE, "Slot worker connecting", slot_id);
async {
let connected = async {
if !slot
.loop_until_connected(
&config,
Expand All @@ -590,19 +590,30 @@ impl<Conn: Connection> SetWorker<Conn> {
{
// The slot was instructed to exit
// before it connected. Bail.
return;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In short: The bug is here.

event!(
Level::TRACE,
slot_id,
"Terminating instead of connecting"
);
return false;
}
interval.reset_after(interval.period().add_spread(config.spread));
true
}
.instrument(span)
.await;

if !connected {
return;
}
}
Work::DoMonitor => {
tokio::select! {
biased;
_ = &mut terminate_rx => {
// If we've been instructed to bail out,
// do that immediately.
event!(Level::TRACE, slot_id, "Terminating while monitoring");
return;
},
_ = interval.tick() => {
Expand Down