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

Panic when network connection is lost #1940

Closed
basbossink-ds opened this issue Jun 30, 2022 · 3 comments · Fixed by #1989
Closed

Panic when network connection is lost #1940

basbossink-ds opened this issue Jun 30, 2022 · 3 comments · Fixed by #1989
Labels
bug db:postgres Related to PostgreSQL E-easy good first issue Good for newcomers

Comments

@basbossink-ds
Copy link

While network maintenance was being performed the following error popped-up:

 thread 'main' panicked at 
'called `Option::unwrap()` on a `None` value': 
...../sqlx-core-0.5.13/src/postgres/listener.rs:165

Unfortunately I was unable to reproduce this issue in a lab environment. Looking at the code however, I would imagine the culprit is:

self.connection = None;

@abonander abonander added bug good first issue Good for newcomers db:postgres Related to PostgreSQL E-easy labels Jun 30, 2022
@abonander
Copy link
Collaborator

It's likely due to any one of these methods being called immediately after a disconnect: https://github.com/launchbadge/sqlx/blob/main/sqlx-core/src/postgres/listener.rs#L95-L145

An easy fix would look like this:

  • for .listen() and .listen_all(), be sure to call .reconnect_if_needed().await? first
  • for .unlisten() and .unlisten_all(), use if let Some(connection) = &mut self.connection instead of self.connection()
    • They should still delete from self.channels though

@abonander
Copy link
Collaborator

Oh, and this should probably be fixed in the Executor methods too: https://github.com/launchbadge/sqlx/blob/main/sqlx-core/src/postgres/listener.rs#L328-L374

.fetch_many() could probably do something like

use futures_util::TryFutureExt;

Box::pin(async move {
    self.reconnect_if_needed().await?;

    Ok(self.connection().fetch_many(query))
}.try_flatten_stream())

@Anastasia1510

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug db:postgres Related to PostgreSQL E-easy good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants