Skip to content

Losing connection to the database results in query! macros returning errors permanently in rust-analyzer #3939

@swlynch99

Description

@swlynch99

I have found these related issues/pull requests

Not related to any other issue that I could find. The closest seems to be #3738 but that is definitely a different issue.

Description

sqlx::query! and friends open a single database connection and use that to talk to the database. If that connection goes away then any later macros will error. This is not a problem for regular compilation, but rust-analyzer will keep the macro library loaded indefinitely. Once the connection is lost then the only way to get rid of the errors is to completely restart rust-analyzer.

I think the correct thing to do in this case is to try and reconnect to the database after receiving a connection/protocol error so that the next query, at least, is able to continue working appropriately.

Reproduction steps

  1. Set up a postgres database.
  2. Configure sqlx to point to this database.
  3. Write a query that uses the database. For example,
    fn do_query(conn: &mut sqlx::PgConnection) {
       let _ = sqlx::query!("SELECT * FROM information_schema.tables").fetch_all(conn).await;
    }
  4. Restart the database (or otherwise break the connection)
  5. Modify the query. You will get the following error
    error communicating with database: expected to read 5 bytes, got 0 bytes at EOF
    
    At this point any attempt to run a sqlx query macro will result in this error, regardless of whether the database is reachable or not.

SQLx version

v0.8.6

Enabled SQLx features

runtime-tokio-native-tls,postgres,uuid,chrono,json,ipnetwork but I suspect this will happen with any database backend

Database server and version

Postgres 15

Operating system

Linux (within WSL2)

Rust version

rustc 1.90.0-nightly (a00149764 2025-07-14)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions