﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
9084	File-based session does not store any data on Windows	mizutori	Joeri Bekker	"Django 1.0 (or trunk r9029) has a bug that does not store any session data using file-based sessions on Windows system.
[[BR]]
[[BR]]
[1] Sample code[[BR]]

To use file-based sessions, set the SESSION_ENGINE setting to ""django.contrib.sessions.backends.file"". On Windows system, the following code does not work properly. The session data for ""sess_key"" will never be passed to the next index2 session.

{{{
---[ views.py ]---
def index(request):
    request.session['sess_key'] = ""yes""
    request.session.modified = True
    return HttpResponseRedirect('index2.html')

def index2(request):
    print ""session key=""+repr(request.session['sess_key'])
    return HttpResponseRedirect('index3.html')
---
}}}

[2] My analysis[[BR]]

This is a bug of save() function of django.contrib.sessions.backends.file.SessionStore class. On Windows, os.rename function of Python will raise OSError if the destination file already exists. Whereas on Unix, it will be removed silently. To fix this problem, remove the existing file before renaming if the current operating system is Windows.
[[BR]]
[[BR]]
[3] My workaround[[BR]]

Apply unlink() function before rename() if the operating system is Windows to fix the save() function of file.SessionStore class.

{{{
---[ django/contrib/sessions/backends/file.py ]---
    def save(self, must_create=False):
        ...
        try:
            ...
#00122:
+           if os.name == ""nt"":
+               os.unlink(session_file_name)
            os.rename(output_file_name, session_file_name)
            renamed = True
---
# added patch code with ""+"" mark
}}}
"	Bug	closed	contrib.sessions	1.5	Normal	fixed	sprint2013	wiser0+djangoproject@…	Ready for checkin	1	0	0	1	0	0
