stream: call write callback before end callback

Since v0.9.12 an end callback could be called before a write
callback if end() is called before the write is done. This patch
resolves the issue.