It looks like you can return from loop without closing logfile.
Put
if (logfile.isOpen()) sd.errorHalt("file is open error");
just before the open that fails to check for this problem.
A far bigger problem is that you cannot get any performance if you open and close files in loop. You need a rxFile and a txFile that are opened in setup().