So, as it turns out, I found what my problem is: I was trying to close a set of accounts in a loop that contained other instructions, and a last instruction outside of it, which messed things up.
If instead of doing that, I get the sum of the rents, and apply it at the very end, instead of updating the payer account at each step of the loop it works. So it looks something like this:
let rents = 0;
for account in accounts {
instruction_whatever(account)?;
let lamports = account.lamports();
**account.try_borrow_mut_lamports()? = 0;
rents += lamports;
}
another_instruction()?;
**payer.try_borrow_mut_lamports()? += rents;