First, here is a useful page for understanding event lifecycle. http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx#general_page_lifecycle_stages.
When your page renders, the <%=Session["bar"]%> is replaced by the current value. Since your page has not yet "unloaded" this value has not been updated. In other words, this value is NOT read when the javascript code runs, but rather when the Page is rendered. To accomplish what you are attempting, you would need to set your value in the Pre_Render, rather than the Unload Event.
If Pre_Render doesn't work for you, I can imagine a solution where you would use a combination of an Asyncronous Method call and AJAX to get the value into your code. Here's a reference to how this works: http://msdn.microsoft.com/en-us/library/bb398998.aspx. Specifically look at the last section which is doing exactly what you are describing (i.e. getting a session value from client script).
- Add a script manager to the page.
- Set the PageMethods property to true
- Add a flag to session for "still working on it" and "done"
- Call your long running process to get the value using an asyncronous delegate in Pre_Render.
- In the Async callback set your session value and set the new flag to 'completed'
- In page unload call the Async delegate "EndXXX" method.
- Create a public static method that checks the value of the flag and if set to 'completed' returns the Session value you want.