Avoid leaking file descriptors in tests and examples