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

Providing negative value for RedisCache.set() throws an exception #1306

Closed
gergelypolonkai opened this issue May 17, 2018 · 3 comments
Closed

Comments

@gergelypolonkai
Copy link

When I provide a negative timeout to RedisCache.set() (except the exact value of -1,) the redis library throws an exception:

  File "/apps/dev/game-app/.venv/lib64/python3.6/site-packages/werkzeug/contrib/cache.py", line 631, in set
    value=dump, time=timeout)
  File "/apps/dev/game-app/.venv/lib64/python3.6/site-packages/redis/client.py", line 2014, in setex
    return self.execute_command('SETEX', name, time, value)
  File "/apps/dev/game-app/.venv/lib64/python3.6/site-packages/redis/client.py", line 573, in execute_command
    return self.parse_response(connection, command_name, **options)
  File "/apps/dev/game-app/.venv/lib64/python3.6/site-packages/redis/client.py", line 585, in parse_response
    response = connection.read_response()
  File "/apps/dev/game-app/.venv/lib64/python3.6/site-packages/redis/connection.py", line 582, in read_response
    raise response
redis.exceptions.ResponseError: invalid expire time in setex

The exact value of -1 (which is also returned by self._normalize_timeout if the timeout value is None) is handled correctly; in this case, no timeout is set.

I think if the provided timeout value is negative, RedisCache (and for that matter, any cache backend) should not cache the value at all (and maybe issue a warning).

I’m using Werkzeug 0.14.1, but the same issue is also present on master.

@davidism
Copy link
Member

The problem with this is is broadens the semantics of the argument. If for some reason we give another meaning to -2, that's a breaking change if we previously handled all negative numbers the same. What are you doing that requires passing something besides -1?

@lepture
Copy link
Contributor

lepture commented May 20, 2018

BTW, we are going to remove contrib.cache from werkzeug. See #1249

@gergelypolonkai
Copy link
Author

This was a result of a bug in my code where I calculated the expiry by diffing now() and a database timestamp; when the timestamp, expected to be in the future, was instead in the past, the result was negative. If it happened to be -1, it would have been cached indefinitely (it may or may not happened; but that’s my fault.)

I still think setting any negative value should be treated specially, not just -1:

if time == -1:
    do_something()
elif time < 0:
    warning('Timeout set to a negative value, won’t cache')
    return

do_the_caching()

This allows you to later use other negative values.

But again, I debate -1 is the correct “special value” here, although I can’t come up with a better one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants