[noticket] This is a cherry-pick of https://github.com/okfn/ckan/commit/52f0efda25361aebc99db818c5b8322d767ab2ec which should reduce memory leakage