Close files in a separate thread.

Not ready to merge, this is just a placeholder for now.

Reference #844, #957.