WWII began in September 1939 when Germany assaulted Poland.
The Soviet Union saw no escape from signing this agreement (the “Molotov-Ribbentrop Pact”) to defer from being a target of Nazi Germany’s aggression, as soon as two major European countries – Britain and France – refused to form an alliance with the USSR to stop Nazi Germany’s attacks on European countries.