Skip to content

Query macro does not have a timeout at compile time  #4120

@rakshith-ravi

Description

@rakshith-ravi

I have found these related issues/pull requests

From what I've searched I couldn't find a related issue

Description

(Feel free to skip this section if you don't want to read the background of the issue)

Here's my setup and how I found this:
My repo is in a devcontainer. Since I'm compiling on a Mac (I use orbstack), the docker container runs in a VM, and it slows down my compilation (not to mention hogs RAM). So to squeeze more performance out of my CPU, I run a devcontainer in a VSCode window (for the dependencies like Postgres, Redis, etc), port-forward the services' ports (5432, 6379, etc) to my host machine. Then, I run my Rust code on my host machine, connecting to the Postgres instance running in the devcontainer. Now, my DATABASE_URL env is set to my localhost:5432 port.

The bug:
My compilation was taking forever, and never completing. I assumed it had something to do with the fact that I was using nightly rust, and tried multiple different version of nightly, but the issue continued to persist. After a lot of debugging, I found that my devcontainer had stopped (possibly because of something else, but that's irrelevant to the issue at hand). So the query! macro code was trying to connect to Postgres on localhost:5432, and even though my devcontainer was down, the port was still forwarding. VSCode has the habit of accepting TCP connections on forwarded ports even if the source container is down, but the packets just get dropped. So while my code was compiling, it kept trying to connect to Postgres, and the TCP connections were hanging forever. However, since SQLx does not have a timeout on its query! macro, it just kept waiting forever for a response from Postgres, and thus my compilation never completed. I thought my code was taking a while to compile, and since SQLx does not show any debug logs while compiling with query macros, I had no idea what was going on. Took me at least 6 hours in debugging this before I found the root cause.

Suggested fix:
Add a timeout to the query macro, so that if the database does not respond within a certain time (let's say 2 mins), it returns an error. This way, if the database is down or unreachable, the compilation will fail fast instead of hanging forever.

Alternatively, if we can add debug logs to the compilation process that says something along the lines of "No response from the database after 2 mins", that would work too.

Reproduction steps

Have a query! macro connect to a VSCode forwarded port (I believe VSCode uses SSH port-forwarding internally, but I could be wrong about that) that doesn't have a service running internally. This would cause it to reproduce the issue

SQLx version

0.8.6

Enabled SQLx features

ipnetwork, macros, postgres, runtime-tokio, time, tls-rustls, uuid, json

Database server and version

Postgres 16

Operating system

macOS, but ubuntu inside docker

Rust version

nightly

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