The question was just when this was going to happen. In Stalin’s mind the non-aggression pact was the best way at the time to postpone the war with Germany until such a time that Russia was better equipped to handle it.
Firstly he was not ready at the time for war, and an agreement with Britain and France would mean the probable defeat of Germany at war, but at the possibility that Britain and France might leave Russia to fight Germany with little help from themselves (as their negotiations suggested), which Russia could not face in their current state of unpreparedness for war. The fact that Stalin was reasonably assured that Hitler was going to attack the west first (Hitler was definitely not going to want to fight a war on two fronts, if he was going to attack Poland, which he was definitely going to do at the time, Britain and France would have to come to Poland’s aid (they had treaties with Poland) if Russia was not going to aid Poland. Hitler having Russia’s promise not to attack him, would therefore have his hands free to deal with the west.), combined with the fact that he also gained territory and much needed trade agreements through the pact, made the non-aggression pact much more attractive than the alliance.
He would therefore have time for as long as it takes Germany to deal with the west to prepare his own country for war. This is however where he miscalculated as Germany dealt with the west much quicker than was anticipated. .