Ref T4292. Small fixes:
- There was a bug with the first write, where we'd write 1 but expect 0. Fix this.
- Narrow the window where we hold the isWriting lock: we don't need to wait for the client to finish.
- Release the lock even if something throws.
- Use a more useful variable name.