Repository navigation
Conversation
`std::time::Instant::now()` panics on `wasm32-unknown-unknown` ("time not
implemented on this platform"), and `QueryLogger::new` calls it for every
query, so no driver can run a query there, even over a transport the host
provides.
Add `sqlx_core::rt::Instant`: `std::time::Instant` everywhere else, and
`web_time::Instant`, which reads the host's `performance.now()`, on that
target. Use it in the query logger and the Postgres, MySQL and SQLite
migrators. `web-time` is only a dependency on that target; nothing changes
elsewhere.
sqlx opens its own sockets through `sqlx_core::net::connect_tcp`, which needs a supported async runtime; without one it panics in `missing_rt`. Hosts that hand out their own socket type, such as Cloudflare Workers (`wasm32-unknown-unknown`, with `connect()` sockets), have no way to connect. `connect_with_socket` takes any `sqlx_core::net::Socket` the caller has already connected. The host, port and socket path in the options are ignored; everything else, including `ssl_mode`, applies as it does for `connect_with`. `establish` now goes through the same path after opening its own socket.
Collaborator
|
I don't want to support I recommend trying CloudFlare's experimental support for WASI, which might work without other code changes. As for accepting external sockets, there's already several other open PRs that I have to review. |
Themezv
added a commit
to MemeBattle/monorepo
that referenced
this pull request
Oct 18, 2026
…e-hd (#794) * build: pin sqlx to the MemeBattle fork (connect_with_socket, wasm Instant) sqlx 0.9.0 cannot open a Postgres connection on wasm32-unknown-unknown: PgStream::connect only knows TCP and unix sockets, and std::time::Instant::now() traps there. MemeBattle/sqlx tag v0.9.0-memebattle.1 is v0.9.0 plus the two commits upstream refused in transact-rs/sqlx#4444 (PgConnection::connect_with_socket and a wasm-safe Instant). The only lockfile changes are the sqlx crates' source and web-time, which the fork's sqlx-core uses on wasm32. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * build: vendor sqlx-cloudflare-hd from sqlx-contrib/sqlx-cloudflare#16 Copied from branch feat/sqlx-cloudflare-hd at 9499455d (unpublished, MIT). It opens a Worker socket to a Hyperdrive binding and hands it to sqlx's own Postgres driver through PgConnection::connect_with_socket. Sources, README and LICENSE are verbatim; only Cargo.toml is rewritten to stand alone outside the upstream workspace. VENDOR.md records the source and every change. worker 0.8.6+ needs wasm-bindgen 0.2.128+, so the lockfile moves the wasm-bindgen family (wasm-bindgen 0.2.129, js-sys/web-sys 0.3.106, wasm-bindgen-futures 0.4.79); those only build on wasm targets. oxfmt now skips vendor/ so the copied Markdown stays byte for byte. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * ci(cas): run the CAS workflow on vendor changes Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * ci(cas): compile the vendored Hyperdrive adapter for wasm32 Adding vendor/** to the path filter only decided when the workflow runs; every job still built apps/cas alone, and CAS does not depend on sqlx-cloudflare-hd, so a regression in the adapter or in the wasm-only code of the sqlx fork could merge with every check green. A dedicated job now checks and lints the crate with its postgres feature for wasm32-unknown-unknown, the target it exists for. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Does your PR solve an issue?
fixes #4426
This is option 1 from the discussion there: socket-only, Postgres only. A caller-supplied TLS upgrade, which a MySQL
connect_with_socketwould need, is left for a follow-up if this direction is accepted.Two commits, each usable on its own. Happy to split them into separate PRs if that's easier to review.
fix(core): use a wasm-safe Instant on wasm32-unknown-unknown.QueryLogger::newcallsstd::time::Instant::now()for every query, and that panics onwasm32-unknown-unknownwithtime not implemented on this platform. This addssqlx_core::rt::Instant, which isstd::time::Instanteverywhere else andweb_time::Instant(the host'sperformance.now()) on that target. It's used in the query logger and the Postgres, MySQL and SQLite migrators.web-timeis a dependency on that target only.feat(postgres): add PgConnection::connect_with_socket. This takes anysqlx_core::net::Socketthe caller has already connected. The host, port and socket path in the options are ignored. Everything else, includingssl_mode, applies as it does forconnect_with.establishnow goes through the same path after opening its own socket, so existing connections are unchanged.The motivating host is Cloudflare Workers, which hands out its own TCP sockets (e.g. to reach Postgres through Hyperdrive) and has no tokio. TLS doesn't need a hook for Postgres: a caller whose host upgrades the socket itself can send the
SSLRequest, upgrade, and pass the result in withssl_mode = Disable.Tests
it_connects_with_socketintests/postgres/postgres.rs: a tokioTcpStreamopened by the test, thenconnect_with_socket.tests/postgres/postgres.rs(58 passed, 2 ignored) andtests/postgres/migrate.rs(4 passed).wasm32-unknown-unknown, a Worker underwrangler dev --localagainst Postgres 17 with SCRAM auth: connect, bound parameters and scalar types, 2,000 rows over many socket reads, unique-violation mapping, rollback and commit, prepared-statement reuse,sqlx::migrate!(), ping and close. Reverting only theInstantcommit makes the same Worker panic.A downstream crate built on this branch, with those scenarios in CI: sqlx-contrib/sqlx-cloudflare#16 (
sqlx-cloudflare-hd).Is this a breaking change?
No. Both changes only add public items (
PgConnection::connect_with_socket,sqlx_core::rt::Instant).sqlx_core::rt::Instantisstd::time::Instanton every target exceptwasm32-unknown-unknown, whereInstant::now()panicked before. Existingconnect_withbehaviour is unchanged; it now delegates to the same code path after opening its socket.