After iteration, which could change the package configuration, the package cache was not cleared.

That's the culprit with caches - they better stay uptodate, otherwise they cause bug.
In other words, they better be worth it !