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

Adding password to dsn causes scripts to ignore CTRL-C #898

Closed
richardbann opened this issue Apr 13, 2019 · 5 comments

Comments

@richardbann
Copy link

@richardbann richardbann commented Apr 13, 2019

OS version: Ubuntu 18.04
psycopg2 version: 2.8.1

The following script can be stopped with CTRL-C:

import psycopg2


while True:
    try:
        psycopg2.connect("host=postgres")
    except Exception as e:
        continue
    else:
        break

Adding a password causes the script to ignore CTRL-C:

import psycopg2


while True:
    try:
        psycopg2.connect("host=postgres password=x")
    except Exception as e:
        continue
    else:
        break

Please help, what makes such a big difference?

@dvarrazzo

This comment has been minimized.

Copy link
Member

@dvarrazzo dvarrazzo commented Apr 13, 2019

Funny, it's true.

The command in itself terminates on error (if the password is wrong):

In [4]: psycopg2.connect("host=localhost user=wat dbname=piro password=y")
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-4-f5b524a63be1> in <module>()
----> 1 psycopg2.connect("host=localhost user=wat dbname=piro password=y")

/home/piro/dev/psycopg2/build/lib.2.7/psycopg2/__init__.pyc in connect(dsn, connection_factory, cursor_factory, **kwargs)
    124 
    125     dsn = _ext.make_dsn(dsn, **kwargs)
--> 126     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
    127     if cursor_factory is not None:
    128         conn.cursor_factory = cursor_factory

OperationalError: FATAL:  password authentication failed for user "wat"
FATAL:  password authentication failed for user "wat"

But in a loop it doesn't receive a SIGINT:

In [5]: while True:
   ...:     try:
   ...:         psycopg2.connect("host=localhost user=wat dbname=piro password=y")
   ...:     except Exception as e:
   ...:         continue
   ...:     else:
   ...:         break
   ...:     
^C^C^C^CTerminated

(until killed with a TERM).

If I add a couple of prints, before the connect() and after the except, I can see the command in a tight loop, and pressing ctrl-C the output is "disturbed" by the ^C chars appearing and streaming up. So it doesn't seem a psycopg bug, rather a Python one. Maybe the quick release and acquisition of the GIL doesn't allow the signal handler to operate correctly.

Anyway, if you put a time.sleep(0.01) after the except it will receive and handle the ctrl-c ok.

@dvarrazzo dvarrazzo closed this Apr 13, 2019
@richardbann

This comment has been minimized.

Copy link
Author

@richardbann richardbann commented Apr 14, 2019

Thanks for looking into it.

It is very unlikely it is a python bug as it works as expected with psycopg2 2.7.1 and the described behavior happens starting with 2.7.2.

@richardbann

This comment has been minimized.

Copy link
Author

@richardbann richardbann commented Apr 14, 2019

After some testing it seems to be this commit: 9f160fd

@dvarrazzo

This comment has been minimized.

Copy link
Member

@dvarrazzo dvarrazzo commented Apr 14, 2019

Interesting: probably catching and re-rising the exception does something.

The problem caused is really marginal though: there isn't really a reason to loop so tightly around trying to reconnect. If the code is correct (e.g. it doesn't swallow an exception) I'm not interested to investigate further.

dvarrazzo added a commit that referenced this issue Jun 22, 2019
This avoids the need to juggle with exceptions in order to scrub the
password after a connection error, which may also swallow signals
(see #898).
@dvarrazzo

This comment has been minimized.

Copy link
Member

@dvarrazzo dvarrazzo commented Jun 24, 2019

As discussed in #934, the issue is not related to a tight loop but to the exception swallowing, in order to obfuscate the password but ignore eventual errors in that function in order to avoid clobbering connection errors, deemed more important.

Issue fixed by simply obfuscating the password before attempting the connection, and not tampering with the exception altogether. To be released in 2.8.4.

bors bot added a commit to chronhq/backend that referenced this issue Oct 27, 2019
Merge #167
167: Update psycopg2 requirement from ~=2.8.3 to ~=2.8.4 in /config r=MiklerGM a=dependabot-preview[bot]

Updates the requirements on [psycopg2](https://github.com/psycopg/psycopg2) to permit the latest version.
<details>
<summary>Changelog</summary>

*Sourced from [psycopg2's changelog](https://github.com/psycopg/psycopg2/blob/master/NEWS).*

> Current release
> ---------------
> 
> What's new in psycopg 2.8.4
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> - Fixed building with Python 3.8 (🎫`[#854](psycopg/psycopg2#854).
> - Don't swallow keyboard interrupts on connect when a password is specified
>   in the connection string (🎫`[#898](psycopg/psycopg2#898).
> - Don't advance replication cursor when the message wasn't confirmed
>   (🎫`[#940](psycopg/psycopg2#940).
> - Fixed inclusion of ``time.h`` on linux (🎫`[#951](psycopg/psycopg2#951).
> - Fixed int overflow for large values in `~psycopg2.extensions.Column.table_oid`
>   and `~psycopg2.extensions.Column.type_code` (🎫`[#961](psycopg/psycopg2#961).
> - `~psycopg2.errorcodes` map and `~psycopg2.errors` classes updated to
>   PostgreSQL 12.
> - Wheel package compiled against OpenSSL 1.1.1d and PostgreSQL at least 11.4.
> 
> 
> What's new in psycopg 2.8.3
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> - Added *interval_status* parameter to
>   `~psycopg2.extras.ReplicationCursor.start_replication()` method and other
>   facilities to send automatic replication keepalives at periodic intervals
>   (🎫`[#913](psycopg/psycopg2#913).
> - Fixed namedtuples caching introduced in 2.8 (🎫`[#928](psycopg/psycopg2#928).
> 
> 
> What's new in psycopg 2.8.2
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> - Fixed `~psycopg2.extras.RealDictCursor` when there are repeated columns
>   (🎫`[#884](psycopg/psycopg2#884).
> - Binary packages built with openssl 1.1.1b. Should fix concurrency problems
>   (🎟`[#543](psycopg/psycopg2#543), [#836](psycopg/psycopg2#836).
> 
> 
> What's new in psycopg 2.8.1
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> - Fixed `~psycopg2.extras.RealDictRow` modifiability (🎫`[#886](psycopg/psycopg2#886).
> - Fixed "there's no async cursor" error polling a connection with no cursor
>   (🎫`[#887](psycopg/psycopg2#887).
> 
> 
> What's new in psycopg 2.8
> -------------------------
> 
> New features:
></tr></table> ... (truncated)
</details>
<details>
<summary>Commits</summary>

- See full diff in [compare view](https://github.com/psycopg/psycopg2/commits)
</details>
<br />

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
- `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language
- `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language
- `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language
- `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language
- `@dependabot badge me` will comment on this PR with code to add a "Dependabot enabled" badge to your readme

Additionally, you can set the following in your Dependabot [dashboard](https://app.dependabot.com):
- Update frequency (including time of day and day of week)
- Pull request limits (per update run and/or open at any time)
- Automerge options (never/patch/minor, and dev/runtime dependencies)
- Out-of-range updates (receive only lockfile updates, if desired)
- Security updates (receive only security updates, if desired)



</details>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.