Skip to content

fix: keep the password and the session settings when reconnecting - #478

Open
LemonCondor wants to merge 1 commit into
dalibo:masterfrom
LemonCondor:fix/reconnect-session
Open

LemonCondor wants to merge 1 commit into
dalibo:masterfrom
LemonCondor:fix/reconnect-session

Conversation

@LemonCondor

Copy link
Copy Markdown

After a lost connection, Data.try_reconnect() opens a new one from dsn_parameters only. Two things are missing:

  1. The password. libpq never reports it, so when it was typed at the prompt (or given in the connection string), every reconnection attempt fails and pg_activity stays on "trying to reconnect".
  2. The session settings. --hide-queries-in-logs no longer applies (the tool's queries are logged again), and intervalstyle is not set. On a server whose default is iso_8601, the next refresh raises NotImplementedError: can't parse interval with IntervalStyle 'iso_8601'.

All three symptoms reproduce against PostgreSQL 17 with password authentication (terminating the backend, then reconnecting).

The change keeps the password supplied by the user (excluded from repr) and hide_queries_in_logs on the Data object, and moves the session setup into one method used by both connection paths. Passwords coming from PGPASSWORD or .pgpass are still read by libpq as before.

The new tests in tests/test_data.py fail on master and pass with the change. On a fork, the Tests and Lint workflows pass (103 passed, 1 skipped on Python 3.10 and 3.14). The psycopg2 code path was not run.

try_reconnect() only reused the parameters reported by libpq, which never include the password, and did not set up the new session again.
@blogh

blogh commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Hi !, thanks for contributing.

I love the pg_setup_session() part, thanks for catching this.

For the password, while the current state is not satisfactory, I am not fond of keeping the
password in our state data. I need to think about it.

@LemonCondor

Copy link
Copy Markdown
Author

Thanks for the review. I can split this, so that pg_setup_session() and the session settings go in on their own, and the password is handled separately.

For the password, one option which avoids storing it in Data is to read it from the lost connection (self.pg_conn.info.password) inside try_reconnect(), as that object should still hold it. I have not tested this, in particular with psycopg2. Another is to prompt again when reconnecting fails because a password is needed, but that blocks an unattended screen.

Tell me which you prefer, or if you would rather think about it first.

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

Successfully merging this pull request may close these issues.

2 participants