Fix tempfile bugs from commit 6978275

Commit 6978275 changed xprofile middleware's usage of mktemp
and moved to using tempfile. But it was clearly never tested,
because the os.close() calls never worked. This patch updates
that previous patch to use a context to open and close the file.

Change-Id: I40ee42e8539551fd8e4dfb353f50146ab40a7847