Fix write session bugs