Fix some broken task IDs on stdout/err log events

 - flush system buffers whenever we may have changed
   the best guess task ID so we don't have output
   with wrong ID
 - unregister IDs when done, so we don't end up
   falling back to an ID that's no longer alive