Skip to content
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

Compilation fails with “attempted to communicate with a crashed background worker” #1642

Closed
arm32x opened this issue Jan 19, 2022 · 5 comments

Comments

@arm32x
Copy link

arm32x commented Jan 19, 2022

Under certain conditions an sqlx::query! macro causes a compilation error with the message “attempted to communicate with a crashed background worker” (see steps to reproduce below).

Basic information

  • SQLx version is 0.5.10
  • Enabled features are runtime-tokio-native-tls, sqlite, and macros
  • sqlite3 --version is 3.37.0 2021-11-27 14:13:22 bd41822c7424d393a30e92ff6cb254d25c26769889c1499a18a0b9 f5dalt1

Steps to reproduce

First, create an SQLite database with the following schema using sqlite3 test.db < schema.sql:

CREATE TABLE things(
    thing_id INTEGER PRIMARY KEY,
    thing_name TEXT NOT NULL UNIQUE
);
CREATE TABLE thing_widgets(
    thing_id INTEGER NOT NULL UNIQUE,
    thing_widget INTEGER NOT NULL UNIQUE
);

Then, et the DATABASE_URL environment variable:

export DATABASE_URL=sqlite:test.db

Finally, try to compile the following code:

use sqlx::SqlitePool;

#[tokio::main]
async fn main() -> sqlx::Result<()> {
    let pool = SqlitePool::connect("sqlite:test.db").await?;

    let data = sqlx::query!(r"
        SELECT
            things.thing_id, thing_widget
            FROM things
            LEFT OUTER JOIN thing_widgets
                ON things.thing_id = thing_widgets.thing_id
            WHERE thing_name = ?
    ", "bob").fetch_optional(&pool).await?;
    
    Ok(())
}

Compilation should fail with the following error message (or something similar):

The error message

thread 'sqlx-sqlite-worker-0' panicked at 'no entry found for key', /home/arm32x/.ca
rgo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.10/src/sqlite/connection/
explain.rs:217:34
stack backtrace:
   0: rust_begin_unwind
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/pani
cking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/pan
icking.rs:107:14
   2: core::panicking::panic_display
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/pan
icking.rs:63:5
   3: core::option::expect_failed
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/opt
ion.rs:1637:5
   4: core::option::Option<T>::expect
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/opt
ion.rs:709:21
   5: <std::collections::hash::map::HashMap<K,V,S> as core::ops::index::Index<&Q>>::
index
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/coll
ections/hash/map.rs:1158:9
   6: <ahash::hash_map::AHashMap<K,V,S> as core::ops::index::Index<&Q>>::index
             at /home/arm32x/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0
.7.6/src/hash_map.rs:249:9
   7: sqlx_core::sqlite::connection::explain::explain
             at /home/arm32x/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-co
re-0.5.10/src/sqlite/connection/explain.rs:217:34
   8: sqlx_core::sqlite::connection::describe::describe
             at /home/arm32x/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-co
re-0.5.10/src/sqlite/connection/describe.rs:41:51
   9: sqlx_core::sqlite::connection::worker::ConnectionWorker::establish::{{closure}
}::{{closure}}
             at /home/arm32x/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-co
re-0.5.10/src/sqlite/connection/worker.rs:132:37
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtra
ce.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new
?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.58.0 (02072b482 2022-01-11) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type
 bin

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: attempted to communicate with a crashed background worker
  --> src/main.rs:7:16
   |
7  |       let data = sqlx::query!(r"
   |  ________________^
8  | |         SELECT
9  | |             things.thing_id, thing_widget
10 | |             FROM things
...  |
13 | |             WHERE thing_name = ?
14 | |     ", "bob").fetch_optional(&pool).await?;
   | |_____________^
   |
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` (i
n Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `sqlx-bug` due to previous error
@abonander
Copy link
Collaborator

The first error in the stack backtrace is the actual error. I suspect this is related to or a duplicate of #1515

@enaut
Copy link

enaut commented Mar 20, 2022

Hey all, is there a solution to this problem?I think my query has the same problem although I absolutely do not understand which. I realized for my failed query that it compiles and runs fine using the query function instead of the macro. However this comes with the drawback of runtime instead of compiletime checking and string fromatting.

@enaut
Copy link

enaut commented Mar 21, 2022

It seems to be the left join that is not working as expected.

Select borrows.*, 1 as returned from borrows join returns where borrows.item = ? works vs.

Select borrows.*, 1 as returned from borrows left join returns where borrows.item = ? does not.

@tobymurray
Copy link
Contributor

I ran into the same style of issue here (with a LEFT JOIN) rust-lang/rust#97067

@abonander
Copy link
Collaborator

I believe #1816 might have fixed this which is released as 0.6.0.

If anyone can reproduce this crash on 0.6.0, please feel free to open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants