Make sure we always close the state on read and write

Fix #827