Skip to content

fix: fix handling of floating point values for connect_timeout#796

Merged
dvarrazzo merged 1 commit intomasterfrom
fix-conninfo-float
Apr 20, 2024
Merged

fix: fix handling of floating point values for connect_timeout#796
dvarrazzo merged 1 commit intomasterfrom
fix-conninfo-float

Conversation

@dvarrazzo
Copy link
Copy Markdown
Member

The connect_timeout parameter is specified as integer.

It seems that certain libpq versions handle correctly a floating point connect timeout:

$ psql connect_timeout=5.0
psql (11.22 (Debian 11.22-0+deb10u2), server 16.2)
WARNING: psql major version 11, server major version 16.
         Some psql features might not work.
Type "help" for help.

others don't

$ psql --version
psql (PostgreSQL) 16.2 (Ubuntu 16.2-1.pgdg22.04+1)
$ psql "connect_timeout=5.0"
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: invalid integer value "5.0" for connection option "connect_timeout"

Psycopg doesn't, but mostly by accident, and in the recent version where the connection timeout and the multiple attempts are taken more care of:

$ python -c "import psycopg; psycopg.connect('connect_timeout=5.0')"
Traceback (most recent call last):
  File "/home/piro/dev/LiveSurface/BackSurface/.venv/lib/python3.10/site-packages/psycopg/conninfo.py", line 141, in timeout_from_conninfo
    timeout = int(value)
ValueError: invalid literal for int() with base 10: '5.0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/piro/dev/LiveSurface/BackSurface/.venv/lib/python3.10/site-packages/psycopg/connection.py", line 727, in connect
    timeout = timeout_from_conninfo(params)
  File "/home/piro/dev/LiveSurface/BackSurface/.venv/lib/python3.10/site-packages/psycopg/conninfo.py", line 143, in timeout_from_conninfo
    raise e.ProgrammingError(f"bad value for connect_timeout: {value!r}")
psycopg.ProgrammingError: bad value for connect_timeout: '5.0'

it seems safe to parse a floating point error too.

@dvarrazzo dvarrazzo force-pushed the fix-conninfo-float branch from e3b770e to f8975d4 Compare April 18, 2024 01:52
@dvarrazzo
Copy link
Copy Markdown
Member Author

To be clearer, this can be considered a regression, because in previous implementations of psycopg a float was accepted ok. I found the problem because a program of mine failed to start after a libraries upgrade.

@dvarrazzo dvarrazzo force-pushed the fix-conninfo-float branch from f8975d4 to 34f9cba Compare April 20, 2024 21:38
@dvarrazzo dvarrazzo merged commit 34f9cba into master Apr 20, 2024
@dvarrazzo dvarrazzo deleted the fix-conninfo-float branch April 20, 2024 21:52
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