You are right the sterilization process was insufficient.
People avoid this situation by using materials which are not overly contaminated in the source but also the sterilization one day let the material rest and subsequenc day double sterilization.
Source material and sterilization protocols are critical.
Autoclave for one hour in each attempt and use a temperature pressure indicatior to ensure temperatures were achieved.
Also make sure you have no green mold in the spawn source.
If you are growing oyster mushrooms use clean wheat straw and watch not to use over rich supplementations.
@ Muhammad, as Dr. Paul rightly pointed out let the material rest for a day after sterilization and again sterilize after 24 hours. I think, you may manage the contamination. In fact, in case of soil sterilization we are doing 3 consecutive day of sterilization to make the soil free from microbes.