Fix session issues when local and database time differs (#1486132)
Improve performance by executing session gc on script shutdown, also call session_write_close() ASAP