Fix some bugs in middleware/session to make sanity_test run