Skip to content

Smol+async global executor 1.80 dev #3791

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

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fb3f254
Sqlx-core: rename async_io dependency for async-std
martin-kolarik Mar 2, 2025
31553e7
Sqlx-core: simplify TimeoutError
martin-kolarik Mar 2, 2025
0c12744
Sqlx-core: code for async-global-executor
martin-kolarik Mar 2, 2025
6c324b1
Sqlx: integrate async-global-executor feature
martin-kolarik Mar 2, 2025
46f8400
Merge with upstream
martin-kolarik Mar 2, 2025
40f3d8d
Note to unsafe
martin-kolarik Mar 2, 2025
375b493
Step up MSRV as async-global-executor needs it
martin-kolarik Mar 2, 2025
aef441b
Sqlx-core: fix of unix socket build
martin-kolarik Mar 2, 2025
f5e374a
Unsafe fixes, smol executor added
martin-kolarik Mar 16, 2025
7de5718
Workflow fix
martin-kolarik Mar 16, 2025
b462f43
Changes outside sqlx_rt
martin-kolarik Mar 16, 2025
4f851b0
Cleanup conditional rt compilation
martin-kolarik Mar 17, 2025
4eadb24
Warning
martin-kolarik Mar 17, 2025
dd053e1
Merge branch 'temp'
martin-kolarik Mar 17, 2025
aa29a3e
Add executors to test matrix
martin-kolarik Mar 17, 2025
89faf0e
Fix of skipping code sqlite due to mismatch in cargo feature names
martin-kolarik Mar 17, 2025
8cc0f5c
Merge remote-tracking branch 'origin/sqlx-cli-sqlite-create-WAL-db-fix'
martin-kolarik Mar 17, 2025
ddcf4ed
Smol executor isolated
martin-kolarik Mar 17, 2025
653152a
Fix, reduce number of tests, remove async_std
martin-kolarik Mar 17, 2025
d1e91f6
Fix of test_block_on, regression
martin-kolarik Mar 17, 2025
5627ca8
Format fixes
martin-kolarik Mar 17, 2025
c7fa793
async-global-executor added
martin-kolarik Mar 17, 2025
580b8df
Merge branch 'launchbadge:main' into main
martin-kolarik Apr 13, 2025
1698c3b
Merge branch 'main' into smol+async-global-executor-1.80-dev
martin-kolarik Apr 13, 2025
8d6377b
async-std changed to 1.13
martin-kolarik Apr 13, 2025
5e529a9
litemap, zerofrom requires rustc 1.81
martin-kolarik Apr 13, 2025
e668ece
Fix of missing _sqlite in cargo.toml
martin-kolarik Apr 13, 2025
246bfd3
Clippy lints
martin-kolarik Apr 13, 2025
9b0cebb
Clean up
martin-kolarik Apr 13, 2025
c3e8f61
Remove features combinations
martin-kolarik Apr 13, 2025
2d41986
Merge branch 'main' into smol+async-global-executor-1.80-dev
martin-kolarik Apr 28, 2025
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
Prev Previous commit
Next Next commit
Changes outside sqlx_rt
  • Loading branch information
martin-kolarik committed Mar 16, 2025
commit b462f438a30ec4bc2e8b83e8be1fbd8d908ad7d1
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ json = ["serde", "serde_json"]
_rt-async-global-executor = [
"async-global-executor",
"async-io-global-executor",
"async-lock",
"async-net",
]
_rt-async-std = ["async-std", "async-io-std"]
Expand Down Expand Up @@ -62,6 +63,7 @@ uuid = { workspace = true, optional = true }

async-io-global-executor = { package = "async-io", version = "2.2", optional = true }
async-io-std = { package = "async-io", version = "1.9.0", optional = true }
async-lock = { version = "3.4.0", optional = true }
async-net = { package = "async-net", version = "2.0.0", optional = true }
base64 = { version = "0.22.0", default-features = false, features = ["std"] }
bytes = "1.1.0"
Expand Down
30 changes: 27 additions & 3 deletions sqlx-core/src/net/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,16 @@ pub async fn connect_tcp<Ws: WithSocket>(
return Ok(with_socket.with_socket(stream).await);
}

#[cfg(feature = "_rt-async-global-executor")]
#[cfg(any(feature = "_rt-async-global-executor", feature = "_rt-smol"))]
{
#[cfg(feature = "_rt-async-global-executor")]
use async_io_global_executor::Async;
#[cfg(feature = "_rt-async-global-executor")]
use async_net::resolve;
#[cfg(feature = "_rt-smol")]
use smol::Async;
#[cfg(feature = "_rt-smol")]
use smol::net::resolve;
use std::net::TcpStream;

let mut last_err = None;
Expand Down Expand Up @@ -270,7 +276,11 @@ pub async fn connect_tcp<Ws: WithSocket>(
};
}

#[cfg(not(all(feature = "_rt-async-global-executor", feature = "_rt-async-std")))]
#[cfg(not(all(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
)))]
#[allow(unreachable_code)]
{
crate::rt::missing_rt((host, port, with_socket))
Expand Down Expand Up @@ -315,7 +325,21 @@ pub async fn connect_uds<P: AsRef<Path>, Ws: WithSocket>(
return Ok(with_socket.with_socket(stream).await);
}

#[cfg(not(all(feature = "_rt-async-global-executor", feature = "_rt-async-std")))]
#[cfg(feature = "_rt-smol")]
{
use smol::Async;
use std::os::unix::net::UnixStream;

let stream = Async::<UnixStream>::connect(path).await?;

return Ok(with_socket.with_socket(stream).await);
}

#[cfg(not(all(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
)))]
#[allow(unreachable_code)]
{
crate::rt::missing_rt((path, with_socket))
Expand Down
149 changes: 129 additions & 20 deletions sqlx-core/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
// We'll generally lean towards Tokio's types as those are more featureful
// (including `tokio-console` support) and more widely deployed.

#[cfg(all(feature = "_rt-async-global-executor", not(feature = "_rt-tokio")))]
pub use async_lock::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};

#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
pub use async_std::sync::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};

#[cfg(all(feature = "_rt-smol", not(feature = "_rt-tokio")))]
pub use smol::lock::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};

#[cfg(feature = "_rt-tokio")]
pub use tokio::sync::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};

pub struct AsyncSemaphore {
// We use the semaphore from futures-intrusive as the one from async-std
// We use the semaphore from futures-intrusive as the one from async-lock
// is missing the ability to add arbitrary permits, and is not guaranteed to be fair:
// * https://github.com/smol-rs/async-lock/issues/22
// * https://github.com/smol-rs/async-lock/issues/23
Expand All @@ -20,7 +26,14 @@ pub struct AsyncSemaphore {
// and there are some soundness concerns (although it turns out any intrusive future is unsound
// in MIRI due to the necessitated mutable aliasing):
// https://github.com/launchbadge/sqlx/issues/1668
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
#[cfg(all(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
not(feature = "_rt-tokio")
))]
inner: futures_intrusive::sync::Semaphore,

#[cfg(feature = "_rt-tokio")]
Expand All @@ -30,12 +43,24 @@ pub struct AsyncSemaphore {
impl AsyncSemaphore {
#[track_caller]
pub fn new(fair: bool, permits: usize) -> Self {
if cfg!(not(any(feature = "_rt-async-std", feature = "_rt-tokio"))) {
if cfg!(not(any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol",
feature = "_rt-tokio"
))) {
crate::rt::missing_rt((fair, permits));
}

AsyncSemaphore {
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
#[cfg(all(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
not(feature = "_rt-tokio")
))]
inner: futures_intrusive::sync::Semaphore::new(fair, permits),
#[cfg(feature = "_rt-tokio")]
inner: {
Expand All @@ -46,18 +71,39 @@ impl AsyncSemaphore {
}

pub fn permits(&self) -> usize {
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
#[cfg(all(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
not(feature = "_rt-tokio")
))]
return self.inner.permits();

#[cfg(feature = "_rt-tokio")]
return self.inner.available_permits();

#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
#[cfg(not(any(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
feature = "_rt-tokio"
)))]
crate::rt::missing_rt(())
}

pub async fn acquire(&self, permits: u32) -> AsyncSemaphoreReleaser<'_> {
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
#[cfg(all(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
not(feature = "_rt-tokio")
))]
return AsyncSemaphoreReleaser {
inner: self.inner.acquire(permits as usize).await,
};
Expand All @@ -73,12 +119,26 @@ impl AsyncSemaphore {
.expect("BUG: we do not expose the `.close()` method"),
};

#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
#[cfg(not(any(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
feature = "_rt-tokio"
)))]
crate::rt::missing_rt(permits)
}

pub fn try_acquire(&self, permits: u32) -> Option<AsyncSemaphoreReleaser<'_>> {
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
#[cfg(all(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
not(feature = "_rt-tokio")
))]
return Some(AsyncSemaphoreReleaser {
inner: self.inner.try_acquire(permits as usize)?,
});
Expand All @@ -88,18 +148,39 @@ impl AsyncSemaphore {
inner: self.inner.try_acquire_many(permits).ok()?,
});

#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
#[cfg(not(any(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
feature = "_rt-tokio"
)))]
crate::rt::missing_rt(permits)
}

pub fn release(&self, permits: usize) {
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
#[cfg(all(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
not(feature = "_rt-tokio")
))]
return self.inner.release(permits);

#[cfg(feature = "_rt-tokio")]
return self.inner.add_permits(permits);

#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
#[cfg(not(any(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
feature = "_rt-tokio"
)))]
crate::rt::missing_rt(permits)
}
}
Expand All @@ -114,30 +195,58 @@ pub struct AsyncSemaphoreReleaser<'a> {
// and there are some soundness concerns (although it turns out any intrusive future is unsound
// in MIRI due to the necessitated mutable aliasing):
// https://github.com/launchbadge/sqlx/issues/1668
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
#[cfg(all(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
not(feature = "_rt-tokio")
))]
inner: futures_intrusive::sync::SemaphoreReleaser<'a>,

#[cfg(feature = "_rt-tokio")]
inner: tokio::sync::SemaphorePermit<'a>,

#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
#[cfg(not(any(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
feature = "_rt-tokio"
)))]
_phantom: std::marker::PhantomData<&'a ()>,
}

impl AsyncSemaphoreReleaser<'_> {
pub fn disarm(self) {
#[cfg(feature = "_rt-tokio")]
#[cfg(all(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
not(feature = "_rt-tokio")
))]
{
self.inner.forget();
let mut this = self;
this.inner.disarm();
}

#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
#[cfg(feature = "_rt-tokio")]
{
let mut this = self;
this.inner.disarm();
self.inner.forget();
}

#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
#[cfg(not(any(
any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol"
),
feature = "_rt-tokio"
)))]
crate::rt::missing_rt(())
}
}
17 changes: 15 additions & 2 deletions sqlx-macros-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,24 @@ where
TOKIO_RT.block_on(f)
}

#[cfg(all(feature = "_rt-async-std", not(feature = "tokio")))]
#[cfg(all(
any(feature = "_rt-async-global-executor", feature = "_rt-smol"),
not(feature = "_rt-tokio")
))]
{
sqlx_core::rt::test_block_on(f)
}

#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
{
async_std::task::block_on(f)
}

#[cfg(not(any(feature = "_rt-async-std", feature = "tokio")))]
#[cfg(not(any(
feature = "_rt-async-global-executor",
feature = "_rt-async-std",
feature = "_rt-smol",
feature = "_rt-tokio"
)))]
sqlx_core::rt::missing_rt(f)
}