Register session close on shutdown at the start of session handling

This is necessary to make sure session data is saved on redirect.