emmett-framework / emmett

The web framework for inventors
BSD 3-Clause "New" or "Revised" License
1.06k stars 71 forks source link

Exception when switching to Redis for sessions #287

Closed ianshk closed 4 years ago

ianshk commented 4 years ago

I tried using redis for sessions instead of the default cookie sessions and I get an exception.

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/ian/Documents/Projects/shopifyapps/project/.venv/lib/python3.7/site-packages/emmett/asgi/handlers.py", line 281, in dynamic_handler http = await self.router.dispatch() File "/Users/ian/Documents/Projects/shopifyapps/project/.venv/lib/python3.7/site-packages/emmett/routing/router.py", line 217, in dispatch http_cls, output = await route.dispatcher.dispatch(request, reqargs) File "/Users/ian/Documents/Projects/shopifyapps/project/.venv/lib/python3.7/site-packages/emmett/routing/dispatchers.py", line 79, in dispatch await self._parallel_flow(self.flow_close) File "/Users/ian/Documents/Projects/shopifyapps/project/.venv/lib/python3.7/site-packages/emmett/routing/dispatchers.py", line 30, in _parallel_flow raise task.exception() File "/Users/ian/Documents/Projects/shopifyapps/project/.venv/lib/python3.7/site-packages/emmett/sessions.py", line 137, in close_request self._save_session(expiration) File "/Users/ian/Documents/Projects/shopifyapps/project/.venv/lib/python3.7/site-packages/emmett/sessions.py", line 226, in _save_session expiration) File "/Users/ian/Documents/Projects/shopifyapps/project/.venv/lib/python3.7/site-packages/redis/client.py", line 1822, in setex return self.execute_command('SETEX', name, time, value) File "/Users/ian/Documents/Projects/shopifyapps/project/.venv/lib/python3.7/site-packages/redis/client.py", line 901, in execute_command return self.parse_response(conn, command_name, **options) File "/Users/ian/Documents/Projects/shopifyapps/project/.venv/lib/python3.7/site-packages/redis/client.py", line 915, in parse_response response = connection.read_response() File "/Users/ian/Documents/Projects/shopifyapps/project/.venv/lib/python3.7/site-packages/redis/connection.py", line 756, in read_response raise response redis.exceptions.ResponseError: value is not an integer or out of range

gi0baro commented 4 years ago

@ianshk before I proceed into investigation, are you able to use the same redis connection outside of sessions?

ianshk commented 4 years ago

@ianshk before I proceed into investigation, are you able to use the same redis connection outside of sessions?

Edit: I did a quick test and it works outside of sessions.

I did a test with the counter example on the website and still get the same exception, so it should be easy for you to replicate

from redis import Redis
from emmett import App, session
from emmett.sessions import SessionManager
from emmett.orm import Database

app = App(__name__)
app.config_from_yaml('env.yml', 'env')

from .models.data import Customer, Setting

db = Database(app, auto_migrate=True)
db.define_models(Customer, Setting)

red = Redis(host='127.0.0.1', port=6379)

app.pipeline = [
    #SessionManager.cookies(app.config.env.SESSION_KEY),
    SessionManager.redis(red),
    db.pipe
]

from .shopifyapi import views, webhook
from .controllers import main, webhook, api

# main
@app.route("/counter")
async def count():
  session.counter = (session.counter or 0) + 1
  return "This is your %d visit" % session.counter
gi0baro commented 4 years ago

@ianshk I confirm this. redis-py changed order of arguments in setex command in version 3.x

Gonna build a patch release later today, will keep this open until that.

gi0baro commented 4 years ago

This is fixed in 2.0.2 and current master