1. presume all grains have bugs when purchased
2. freeze grain product (rice, flour etc.) for at least 2 days to kill said bugs
3. Seal grain product in mason jar with good lid, good seal. Make sure seal and lip of jar are clean, dry, with no, say, flour dust at all.
Without freezing any bugs or eggs will grow and flourish in sealed jar. Without seal if any bugs are present in pantry (they are!) they will invade.
I have been able to keep grain products this way for years, long past any good-by date. Not only does this keep bugs out but keeps grain from going rancid.
You can freeze the grain in the jar but that makes the glass harder to clean.
I have tried many many ways and this works. I used to use bay leaves and this seems to help, but is not necessary with this method.