implement lazy session flushing

@babf, I think this is all we need for "fast" sessions.

I haven't written any tests for it (though existing tests pass), so I won't merge it without @yanick 's approval.

David