title: Why do some Flask session values disappear from the session after closing the browser window, but then reappear later without me adding them?
body: <p>So my understanding of Flask sessions is that I can use it like a dictionary and add values to a session by doing:</p>
  
  <p>session['key name'] = 'some value here'</p>
  
  <p>And that works fine.</p>
  
  <p>On a route I have the client call using AJAX post, I assign a value to the session. And it works fine. I can click on various pages of my site and the value stays in the session. If I close the browser window however, and then go back to my site, the session value I had in there is gone.</p>
  
  <p>So that's weird and you would think the problem is the session isn't permanent. I also implemented Flask-Openid and that uses the session to store information and that <em>does</em> persist if I close the browser window and open it back up again. I also checked the cookie after closing the browser window, but before going back to my site, and the cookie is indeed still there.</p>
  
  <p>Another odd piece of behaviour (which may be related) is that some values I have written to the session for testing purposes will go away when I access the AJAX post route and assign the correct value. So that is odd, but what is truly weird is that when I then close the browser window and open it up again, and have thus lost the value I was trying to retain, the ones that I lost previously actually return! They aren't being reassigned because there's no code in my Python files to reassign those values.</p>
  
  <p>Here is some outputs to helper make it clearer. They are all outputed from a route for a real page, and not the AJAX post route I mentioned above.</p>
  
  <p>This is the output after I have assigned the value I want to store in the session. The value key is 'userid' - all the other values are dummy ones I have added in trying to solve this problem. 'userid': 8 will stay in the session as long as I don't close the browser window. I can access other routes and the value will stay there just like it should.</p>
  
  <pre><code>['session.=', &lt;SecureCookieSession {'userid': 8, 'test_variable_num': 102, 'adding using before request': 'hi', '_permanent': True, 'test_variable_text': 'hi!'}&gt;]
  </code></pre>
  
  <p>If I do close the browser window, and go back into the site, but without redoing the AJAX post request, I get this output:</p>
  
  <pre><code>['session.=', &lt;SecureCookieSession {'adding using before request': 'hi', '_permanent': True, 'yo': 'yo'}&gt;]
  </code></pre>
  
  <p>The 'yo' value was not in the first first output. I don't know where it came from. I searched my code for 'yo' and there is no instances of me assigning that value anywhere. I think I may have added it to the session days ago. So it seems like it is persisting, but being hidden when the other values are written.</p>
  
  <p>And this last one is me accessing the AJAX post route again, and then going to the page that prints out the keys using debug. Same output as the first output I pasted above, which you would expect, and the 'yo' value is gone again (but it will come back if I close the browser window)</p>
  
  <pre><code>['session.=', &lt;SecureCookieSession {'userid': 8, 'test_variable_num': 102, 'adding using before request': 'hi', '_permanent': True, 'test_variable_text': 'hi!'}&gt;]
  </code></pre>
  
  <p>I tested this in both Chrome and Firefox.</p>
  
  <p>So I find this all weird and I am guessing it stems from a misunderstanding of how sessions work. I think they're dictionaries and I can write dictionary values into them and retrieve them days later as long as I set the session to permanent and the cookie doesn't get deleted. </p>
  
  <p>Any ideas why this weird behaviour is happening?</p>