ktrace: fix a race between writes and close
(cherry picked from commit fc369a353b5b5e0f8046687fcbd78a7cd9ad1810)